保存时autofix

在VSCode的Setting里设置即可

  1. "editor.codeActionsOnSave": {
  2. "source.fixAll.eslint": true
  3. }

未定义变量的报错

.eslintrv.js里配置即可

  1. rules: {
  2. "no-undef": "error",
  3. }

问题一:
在配置好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即可
image.png

问题二:
create-react-app创建项目后,eslint在控制台会有报错,但VSCode里并没有提示报错。安装eslint,重新配置覆盖原有的eslint规则,但还是没有报错。
解决:
在VSCode的setting里配置

  1. "eslint.validate": [
  2. "javascript",
  3. "javascriptreact",
  4. "html",
  5. "typescriptreact"
  6. ],

问题三:
async函数不能识别=>,报错:Parsing error: Unexpected token =>
解决:eslint配置

  1. parserOptions: {
  2. ecmaVersion: 8
  3. },

问题四:
无法识别扩展运算符,比如
const { children, …other } = props
会报错Parsing error: Unexpected token …
解决:eslint配置

  1. parserOptions: {
  2. ecmaFeatures: {
  3. experimentalObjectRestSpread: true
  4. }
  5. }

问题五:
配置了‘no-undef’后,全局变量也会报错,如console、alert、document等。
解决:
1.需要配置环境

  • browser:alert、setTimeout、console
  • node:require
  • es6:Promise、Unit8Array、Float64Array

2.配置全局变量
有webpack的DefinePlugin里或其他地方配置的全局常量,在.eslintrc里配置即可

  1. env: {
  2. browser: true,
  3. node: true,
  4. es6: true
  5. },
  6. global: {
  7. ENV_MODE: true,
  8. PRAMETERS: true
  9. }