我正在使用create-react-app
建立一个网站,并且刚刚为其安装了eslint
。
由于某种原因,本应显示为拖欠警告的内容显示为错误,并导致npm run start
失败。
如何绕过此问题,并像以前一样将其显示为警告?
我的.eslintrc.js
env: {
browser: true,
es6: true,
jest: true,
},
extends: [
'airbnb-typescript',
'plugin:@typescript-eslint/recommended',
'prettier/react',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json',
},
plugins: ['react', '@typescript-eslint'],
rules: {
'class-methods-use-this': 'off',
'additional-rule': 'warn',
},
ignorePatterns: ['**/node_modules/*', '**/build/*', 'config-overrides.js'],
};
我的package.json
"name": "device-protection-renewal-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.5",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.19.3",
"@types/react": "^16.9.55",
"@types/react-dom": "^16.9.9",
"babel-polyfill": "^6.26.0",
"core-js": "^3.6.5",
"i18next": "^19.8.3",
"react": "^17.0.1",
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.1",
"react-i18next": "^11.7.3",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all",
"ie >= 9"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
"ie >= 9"
]
},
"devDependencies": {
"@babel/plugin-transform-arrow-functions": "^7.12.1",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.11.0",
"eslint-config-airbnb-typescript": "^9.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"prettier": "^2.1.2",
"typescript": "^4.0.5"
}
}```
[1]: https://i.stack.imgur.com/WUKcz.png
答案 0 :(得分:0)
对不起。我不允许发表评论,但我真的想为您提供帮助。
我假设您已经使用npm install eslint --save-dev
安装了eslint,并使用node_modules/.bin/eslint --init
回答了提示中的问题定义了默认配置。
我注意到在您的.eslintrc.js
文件中,扩展选项中缺少eslint设置:
extends: [
'eslint:recommended',
'airbnb-typescript',
'plugin:@typescript-eslint/recommended',
'prettier/react',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
还建议eslint在package.json
中使用您自己的脚本,并使用npm run lint
运行该脚本,并在您最喜欢的代码编辑器中将其与eslint插件结合使用:
{
"scripts": {
"start": "react-scripts start",
// ...
"lint": "eslint ."
},
}
可能稍后会构建您的应用程序,因此您应该创建一个.eslintignore
文件,并在其中添加build
,因为在运行命令时会检查build目录中的文件。>
来源:https://fullstackopen.com/en/part3/validation_and_es_lint#lint