1. 程式人生 > >eslint配置注意事項

eslint配置注意事項

1). 4:1 error Parsing error: The keyword 'import' is reserved
確保.eslintrc檔案存在,並且內容為

{
  "extends": "airbnb",
  "plugins": ["react"]
}

2). Value "data["0"].VariableDeclarator" has additional properties.

ERROR in ./src/index.js
Module build failed: Error: /home/suwu150/WebStormProject/electron-desktop
-tools/node_modules/eslint-config-airbnb-base/rules/es6.js: Configuration for rule "prefer-destructuring" is invalid: Value "data["0"].VariableDeclarator" has additional properties. Value "data["0"].AssignmentExpression" has additional properties. Referenced from: /home/suwu150/WebStormProject/electron-desktop
-tools/node_modules/eslint-config-airbnb-base/index.js Referenced from: airbnb Referenced from: /home/suwu150/WebStormProject/electron-desktop-tools/.eslintrc at validateRuleOptions (/home/suwu150/WebStormProject/electron-desktop-tools/node_modules/eslint/lib/config/config-validator.js:109:15)

3). - Unexpected top-level property "comma-dangle".

ERROR in ./src/index.js
Module build failed: Error: ESLint configuration in /home/suwu150/WebStormProject/electron-desktop-tools/.eslintrc is invalid:
        - Unexpected top-level property "comma-dangle".

    at validateConfigSchema (/home/suwu150/WebStormProject/electron-desktop-tools/node_modules/eslint/lib/config/config-validator.js:214:15)
    at Object.validate (/home/suwu150/WebStormProject/electron-desktop-tools/node_modules/eslint/lib/config/config-validator.js:231:5)

Unexpected top-level property,我的原因是沒有將規則寫對地方,
錯誤的寫法如下所示:

{
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    }
  },
  "rules": {
    "semi": 2
  },
  "comma-dangle": [1,"always-multiline"],
  "plugins": ["react"],
  "extends": "airbnb"
}

正確的寫法如下所示:

{
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "experimentalObjectRestSpread": true
    }
  },
  "rules": {
    "semi": 2,
    "comma-dangle": ["off"]
  },
  "plugins": ["react"],
  "extends": "airbnb"
}

4). Parsing error: Unexpected token : 或者 Parsing error: Unexpected token =

 34:52  error  Parsing error: Unexpected token :

✖ 1 problem (1 error, 0 warnings)

 @ ./src/store/configureStore.js 6:19-58
 @ ./src/index.js
 @ multi webpack-hot-middleware/client?path=http://localhost:3000/__webpack_hmr babel-polyfill ./src/index.js

Use the babel-eslint parser in your ESLint configuration.
也就是在.eslintrc中新增"parser": "babel-eslint", 即可
安裝依賴包:npm install babel-eslint –save

{
  "parser": "babel-eslint",
  "plugins": ["react"],
  ...
}

5).error: Expected linebreaks to be ‘LF’ but found ‘CRLF’
我們從github上拉別人專案的時候。有時候會遇到這個報錯:
error: Expected linebreaks to be ‘LF’ but found ‘CRLF’
這就是eslint的報錯了,可能是原作者用的是linux系統。
只需在eslintrc檔案裡面將
linebreak-style: [“error”, “unix”]
改成
linebreak-style: [“error”, “windows”](我用的是windows)即可