sku*_*Kim 59 eslint eslint-config-airbnb eslintrc babel-eslint prettier-eslint
我尝试在 WebStrom 中使用 ESLint,但它不起作用并显示错误:
ESLint:解析错误:此实验性语法需要启用以下解析器插件之一:“jsx、flow、typescript”(2:9)。
这是我的.eslintrc和package.json设置。我应该做什么来修复错误?
包.json
{
"name": "raonair-frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"eslint-config-react-app": "^6.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prepare": "husky install"
},
"parser": "babel-eslint",
"eslintConfig": {
"extends": [
"react-app",
"airbnb",
"plugin:flowtype/recommended"
],
"plugins": [
"flowtype"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/eslint-parser": "^7.15.0",
"@babel/eslint-plugin": "^7.14.5",
"@babel/plugin-syntax-jsx": "^7.14.5",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-flowtype": "^5.9.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^7.0.1",
"prettier": "^2.3.2"
}
}
Run Code Online (Sandbox Code Playgroud)
.eslintrc
{
"env": {
"browser": true,
"node": true
},
"extends": [
"airbnb",
"airbnb/hooks",
"prettier"
],
"parser": "@babel/eslint-parser",
"parserOptions": {
"sourceType": "module",
"allowImportExportEveryWhere": false,
"ecmaFeatures": {
"globalReturn": false,
"jsx": true
},
"ecmaVersion": 2020,
"babelOptions": {
"configFile": "./babel.config.js"
}
},
"plugins": [
"jsx-a11y",
"react-hooks",
"@babel/",
"flowtype",
"import"
],
"rules": {
"import/no-anonymous-default-export": "off",
"import/no-extraneous-dependencies": "off",
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling"
],
"newlines-between": "always",
"alphabetize": {
"order": "asc",
"caseInsensitive": false
}
}
],
"import/prefer-default-export": "off",
"indent": [
"error",
2,
{
"SwitchCase": 1
}
],
"jsx-a11y/anchor-is-valid": "off",
"no-console": "error",
"no-unused-vars": "error",
"react/jsx-props-no-spreading": "off",
"react/react-in-jsx-scope": "off"
},
"settings": {
"import/resolver": {
"typescript": {}
},
"insert_final_newline": true
}
}
Run Code Online (Sandbox Code Playgroud)
小智 55
安装@babel/preset-react在开发依赖项中。
将其添加到 .eslintrc 文件中
...
“解析器”:“@babel/eslint-parser”,
“解析器选项”:{
...
“巴贝尔选项”:{
“预设”:[“@babel/preset-react”]
},
}
...
And*_*rei 43
扩展塞罗伊的答案(来源):
yarn add @babel/preset-react -D
或者
npm install --save-dev @babel/preset-react
.babelrc项目根目录下的文件:
{
"presets": [
"@babel/preset-react"
]
}
Run Code Online (Sandbox Code Playgroud)
.eslintrc.json(这样你的控制台和 VSCode 将使用相同的解析器) - 完整的 eslintrc 如下所示:{
"parser": "@babel/eslint-parser",
"settings": {
"react": {
"version": "detect"
}
},
"env": {
"browser": true,
"es2021": true
},
"extends": [
"plugin:react/recommended",
"standard",
"plugin:react/jsx-runtime",
"plugin:testing-library/react",
"plugin:jest/all"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {}
}
Run Code Online (Sandbox Code Playgroud)
注意:.eslintrc.json也可以通过npm init @eslint/config或使用 VSCode 中的 ESLint 扩展来创建。init 将为您提供更多选项可供选择(例如 TypeScript 支持)
在控制台中尝试一下:npx eslint --fix .
检查 VSCode(使用 ESLint 扩展)
奖金:
VSCode 的 ESLint 插件设置(保存时自动修复、Jest 感知、babel 解析器等 - 在全局 VSCode settings.json 中):
{
"presets": [
"@babel/preset-react"
]
}
Run Code Online (Sandbox Code Playgroud)
满的package.json:
{
"name": "okta-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^12.1.0",
"@testing-library/user-event": "^13.2.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router": "^5.2.1",
"react-router-dom": "^5.3.0",
"react-scripts": "4.0.3",
"web-vitals": "^2.1.0"
},
"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"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@babel/eslint-parser": "^7.15.7",
"@babel/preset-react": "^7.14.5",
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-babel": "^5.3.1",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jsx": "^0.1.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-react": "^7.26.0"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57927 次 |
| 最近记录: |