我想在我的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'
好的,我找到了this并更新了.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:模块。似乎它们是由
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文件?
最佳答案
您似乎在其内部嵌套了parserOptions
:
"parserOptions": {
"parserOptions": { "sourceType": "module" }
},
删除一层嵌套,它应该起作用:
"parserOptions": {
"sourceType": "module"
},
为什么要创建其他eslint文件?
他们在
node_modules
下吗?他们将来自您的依赖关系。您无需担心它们。