保存时autofix
在VSCode的Setting里设置即可
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
未定义变量的报错
.eslintrv.js里配置即可
rules: {
"no-undef": "error",
}
问题一:
在配置好eslint后,console会有黄线报错:
ESLint is disabled since its execution has not been approved or denied yet. Use the light bulb menu to open the approval dialog.
解决:
是因为没有把VSCode的Eslint校验打开,选择成Always allow即可
问题二:
create-react-app创建项目后,eslint在控制台会有报错,但VSCode里并没有提示报错。安装eslint,重新配置覆盖原有的eslint规则,但还是没有报错。
解决:
在VSCode的setting里配置
"eslint.validate": [
"javascript",
"javascriptreact",
"html",
"typescriptreact"
],
问题三:
async函数不能识别=>,报错:Parsing error: Unexpected token =>
解决:eslint配置
parserOptions: {
ecmaVersion: 8
},
问题四:
无法识别扩展运算符,比如
const { children, …other } = props
会报错Parsing error: Unexpected token …
解决:eslint配置
parserOptions: {
ecmaFeatures: {
experimentalObjectRestSpread: true
}
}
问题五:
配置了‘no-undef’后,全局变量也会报错,如console、alert、document等。
解决:
1.需要配置环境
- browser:alert、setTimeout、console
- node:require
- es6:Promise、Unit8Array、Float64Array
2.配置全局变量
有webpack的DefinePlugin里或其他地方配置的全局常量,在.eslintrc
里配置即可
env: {
browser: true,
node: true,
es6: true
},
global: {
ENV_MODE: true,
PRAMETERS: true
}