【发布时间】:2019-10-15 06:45:58
【问题描述】:
我的代码库中充满了.js 文件,我正在对特定文件运行eslint。
我收到以下错误:
1:8 error 'React' is defined but never used
no-unused-vars
1:27 error Trailing spaces not allowed
no-trailing-spaces
2:1 error 'prop-types' should be listed in the project's dependencies. Run
'npm i -S prop-types' to add it import/no-extraneous-dependencies
7:13 error Arrow function used ambiguously with a conditional expression
no-confusing-arrow
11:3 error Prop type `array` is forbidden
react/forbid-prop-types
我的.eslintrc.js
module.exports = {
env: {
browser: true,
commonjs: true,
es6: true,
},
extends: [
'airbnb',
"plugin:react/recommended"
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
},
plugins: [
'react'
],
rules: {
"react/jsx-uses-react": 1,
},
};
我的Package.json
"devDependencies": {
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"babel-cli": "6.18.0",
"babel-plugin-dynamic-import-node": "1.0.0",
"babel-plugin-react-intl": "2.2.0",
"babel-plugin-react-transform": "2.0.2",
"babel-plugin-transform-es2015-modules-commonjs": "6.18.0",
"babel-plugin-transform-react-constant-elements": "6.9.1",
"babel-plugin-transform-react-inline-elements": "6.8.0",
"babel-plugin-transform-react-remove-prop-types": "0.2.11",
"babel-preset-latest": "6.16.0",
"babel-preset-react-hmre": "1.1.1",
"circular-dependency-plugin": "2.0.0",
"coveralls": "2.11.15",
"css-loader": "^0.23.1",
"enzyme": "^2.7.0",
"eslint": "3.11.1",
"eslint-config-airbnb": "13.0.0",
"eslint-config-airbnb-base": "10.0.1",
"eslint-import-resolver-webpack": "0.8.0",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "2.2.3",
"eslint-plugin-react": "6.7.1",
"eslint-plugin-redux-saga": "0.1.5",
"eventsource-polyfill": "0.9.6",
"exports-loader": "0.6.3",
"file-loader": "^0.9.0",
"html-loader": "0.4.4",
"html-webpack-plugin": "2.24.1",
"husky": "^3.0.5",
"lint-staged": "^3.2.1",
"ngrok": "2.2.4",
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"standard-version": "^7.0.0",
"webpack-dev-middleware": "^1.11.0",
"webpack-hot-middleware": "^2.18.0"
}
我尝试了以下各种解决方案:
/*eslint-disable no-unused-vars*/
var React = require('react');
/*eslint-enable no-unused-vars*/
还有:
var React = require('react'); // eslint-disable-line no-unused-vars
但几乎无法处理 300-500 个文件。
期望解决.js文件中抛出的所有错误。
提前致谢。
【问题讨论】:
-
想要的结果到底是什么?您希望错误显示在 .jsx 文件上吗?或者您是否需要帮助修复您的 .js 文件中的 eslint 错误?
-
@Luze 期望解决所有在 .js 文件中抛出的错误。
-
但是是因为.js扩展名吗?
-
我不知道这是 .js 还是 .jsx 之间的问题。在功能上没有区别,两种文件类型应该是完全可互换的。尽管 eslint 有可能不喜欢 react 组件上的 .js 文件扩展名,因为对于不是严格 javascript 的文件不使用 .js 扩展名是一个常见规则。但我不能告诉你这是这里的问题。
-
那么我们如何处理这些js文件中的错误呢?
标签: reactjs eslint eslint-config-airbnb