ESLint

ESLint

https://wohugb.gitbooks.io/javascript/content/tool/lintmd.html

用處:用來檢查code的語法

1.安裝

npm install -g eslint

2.配置

於根目錄新增   .eslintrc.json
{
  "globals": {
    // Put things like jQuery, etc
    "jQuery": true,
    "$": true
  },
  "env": {
    // I write for browser
    "browser": true,
    // in CommonJS
    "node": true
  },
  // To give you an idea how to override rule options:
  "rules": {
    // Tons of rules you can use, for example...
    "quotes": [1, "double"]
  }
}

看到上面的rules的數字,對照下表,以上面為例,意思是對所有出現quotes(引號)的地方要使用double(兩個引號),強制性為1(只產生警告訊息)

其他rules可參考官網

http://eslint.org/docs/rules/

3.使用parser去解析ES6

之後在.eslintrc.json加上

如何執行?

其他規則

使用sublime+ESLint

進入sublime的package manage(需先安裝)

輸入

參考至 http://jonathancreamer.com/setup-eslint-with-es6-in-sublime-text/

Eslint React

然後安裝 vscode 的 eslint plugin

如果在vscode React 還是沒有顯示 syntax error線,可安裝

如果還是在editor沒顯示紅線,則可開啟output查看錯誤

解法:

  1. 假設你有server跟frontend folder 開啟vscode時先進入client folder再開啟vscode,這樣才找得到路徑https://github.com/microsoft/vscode-eslint/issues/759#issuecomment-534958665

  2. 不要同時在local跟client都裝eslint 跟plugin

  3. 如果是mono repo 進入到你要做的專案在開啟VSCode

Last updated

Was this helpful?