Node JS
  • SetUp Node&NVM
  • Folder Structure
  • Setup NodeJS Project
  • Library
    • Awilix
    • Swagger
    • Express
    • Jest
      • Supertest
    • Prettier
    • Eslint & Tslint
      • Tslint & Prettier
      • Tslint Plugin Prettier
      • Config With Airbnb
    • Husky And Friends
    • Sentry
  • INFRASTRUCTURE
    • Docker
      • Docker image
      • Docker container
      • Docker Volume
      • Docker command
      • Docker Compose
      • Problem & Solution
    • SonarQube
      • How to use in Nodejs
    • NGinX
    • ดูเพิ่มเติม
  • Note
    • .env declare type
    • Learn Link
Powered by GitBook
On this page
  • Install
  • เรียกใช้งาน
  • CLI Usage
  • Example
  1. Library

Eslint & Tslint

Code linting เป็นเครื่องมือที่ช่วยการวิเคราะห์โครงสร้างของ Code ซึ่งมักใช้เพื่อค้นหารูปแบบของปัญหาที่อาจะเกิดขึ้น หรือ Code ที่เป็นปัญหาซึ่งไม่เป็นไปตาม Style Guidelines

PreviousPrettierNextTslint & Prettier

Last updated 3 years ago

ตัวอย่างการใช้ lint เช่น การประกาศตัวแปรออกมาแล้วไม่มีการใช้ หรือการ console log ใว้ debug แล้วลืมลบ

ทุกอย่างใน Lint สามารถใส่เพิ่มเติมได้ คุณสามารถเพิ่ม Rules ได้ (ไม่ใส่ Rules และ Formatter เข้าไปก็ได้) และทุกๆ Linting Rule ที่คุณเพิ่มเข้าไปเป็นแบบ Stand alone สามารถเปิด-ปิดได้ และแต่ละ Rule สามารถ Set ให้แสดง Warning หรือ Error ได้ นอกจากนี้การใช้ ESLint จะทำให้คุณสามารถปรับแต่ง Style Guide ได้ตามที่คุณต้องการ โดยปัจจุบันมี 2 Style Guide ที่เป็นที่นิยมคือ และ

Install

สำหรับ typescript

npm i -g tslint
# Generate a basic configuration file
tslint --init 

เรียกใช้งาน

#check
tslint -c tslint.json src/**/*.ts

#fix
tslint -c tslint.json src/**/*.ts --fix

CLI Usage

Usage: tslint [options] [file ...]

-v, --version                          output the version number
-c, --config [config]                  configuration file
-e, --exclude <exclude>                exclude globs from path expansion
--fix                                  fixes linting errors for select rules (this may overwrite linted files)
--force                                return status code 0 even if there are lint errors
-i, --init                             generate a tslint.json config file in the current working directory
-o, --out [out]                        output file
--outputAbsolutePaths                  whether or not outputted file paths are absolute
--print-config                         print resolved configuration for a file
-r, --rules-dir [rules-dir]            rules directory
-s, --formatters-dir [formatters-dir]  formatters directory
-t, --format [format]                  output format (json, stylish, verbose, pmd, prose, msbuild, checkstyle, vso, fileslist, codeFrame)
-q, --quiet                            hide non "error" severity linting errors from output
--test                                 test that tslint produces the correct output for the specified directory
-p, --project [project]                tsconfig.json file
--type-check                           (deprecated) check for type errors before linting the project
-h, --help                             output usage information

Example

จะเห็นว่าเพื่อรัน tslint จะมี error ออกมาเพราะไม่ได้เป็นไปตาม config ที่ทำไว้

Google JavaScript Style Guide
Airbnb JavaScript Style Guide
Code
Result