首页 > 解决方案 > 我应该如何将 *.eslintrc* 更新为 ESLint 查看更改?

问题描述

我想在我的 JavaScript 项目中使用 ESLint。我使用 Project Reactor、Spring Boot、ReactJS 和 Webpack。我在 pom.xml 附近的根文件夹中创建了.eslintrc文件。

似乎 ESLint 有效。当我申请

eslint src\main\js\components

它说

1:1 error Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

好的,我找到了这个并更新了我的.eslintrc

{
"env": {
    "browser": true,
    "commonjs": true,
    "node": true,
    "es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
    "parserOptions": { "sourceType": "module" }
},
"plugins": [
    "react"
],
"rules": {
    "indent": [
        "error",
        "tab"
    ],
    "linebreak-style": [
        "error",
        "unix"
    ],
    "quotes": [
        "error",
        "double"
    ],
    "semi": [
        "error",
        "always"
    ],
    "import":"true",
    "keyword-spacing": 2
}
}

但这无济于事。

我在我的项目目录中搜索了 .eslintrc文件,发现可能有 .eslintrc.eslintrc.json.eslintrc.yml文件,但它们没有sourceType: module。似乎它们是由npm install命令生成的。其中之一:

{
"rules": {
    "id-length": 0,
    "max-lines": 0,
    "max-statements-per-line": [2, { "max": 3 }],
    "max-nested-callbacks": [2, 3],
    "max-statements": 0,
    "no-implicit-coercion": [1],
    "no-invalid-this": [1]
}
}

我试图重建项目,删除除我之外的所有.eslintrc

我的packages.json包含:

"devDependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.7",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"css-loader": "^0.26.4",
"eslint": "^5.0.0",
"eslint-plugin-react": "^7.10.0",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^1.1.10",
"image-webpack-loader": "^4.1.0",
"react-redux": "^5.0.7",
"style-loader": "^0.13.2",
"url-loader": "^0.6.2",
"webpack": "^2.2.1"
}

我应该如何将 .eslintrc 更新为ESLint查看更改?为什么要创建额外的 eslint 文件?

标签: javascriptspring-booteslintproject-reactor

解决方案


您似乎已经嵌套parserOptions在自身内部:

"parserOptions": {
    "parserOptions": { "sourceType": "module" }
},

删除一层嵌套,它应该可以工作:

"parserOptions": {
    "sourceType": "module"
},

为什么要创建额外的 eslint 文件?

他们在node_modules吗?它们将来自您的依赖项。你不应该担心他们。


推荐阅读