--- tags: 前端工具 --- # [筆記] Eslint [參考](https://pjchender.dev/webdev/note-sharable-eslint-config) [參考安裝](https://ithelp.ithome.com.tw/articles/10215259) ## 安裝 - package.json ``` <!-- 第一次安裝時會用到 --> "eslint": "eslint --init", ``` ``` "lint": "eslint \"src/**/*.{js,jsx}\"", ``` ### env - 讓eslint知道全域變數 ``` env: { browser: true, 'jest/globals': true, }, ``` - 允許node全域變數 ``` 'env': { 'browser': true, 'es2021': true, "node": true, }, ``` ### parserOptions - js語法版本 ### rules `error` `warn` `off` ### plugins [react規則](https://github.com/yannickcr/eslint-plugin-react/tree/master/lib/rules) [規則](https://eslint.org/docs/rules/) - 只會載入規則,使用方式要在 `rules` 設定 ### extends - 載入其它開發者的ESLint 設定檔 # options說明 - 檢查props有否正確傳入 ``` "react/prop-types": "warn", ``` ----- ``` 'react/prop-types': 'off', ``` ------ ## 常用 ```javascript= 'rules': { 'no-unused-vars': 'warn', 'no-extend-native': 'off', 'prefer-promise-reject-errors': 'off', 'standard/no-callback-literal': 'off', 'react/react-in-jsx-scope': 'off', 'generator-star-spacing': 'off', 'comma-dangle': [ 'error', 'always-multiline', ], 'line-comment-position': [ 'error', { position: 'above', }, ], 'react/prop-types': 'off', 'comma-spacing': 'error', 'arrow-spacing': [ 'error', { 'before': true, 'after': true, }, ], 'indent': ['warn', 2, { SwitchCase: 1 }], 'key-spacing': ['error', { 'beforeColon': false }], 'no-multiple-empty-lines': 'error', 'quotes': ['error', 'single'], 'space-infix-ops': ['error', { 'int32Hint': false }], semi: ['error', 'never'], 'eol-last': ['error', 'always'], 'object-curly-spacing': ['error', 'always'], 'no-trailing-spaces': 'error', 'no-multi-spaces': 'error', 'no-var': 'error', 'prefer-const': 'error', }, ```