Skip to content
On this page

eslint

TIP

eslint 检验你的代码规范,咱们使用airbnb制定的代码规范。

相关依赖下载

bash
yarn add eslint @babel/eslint-parser eslint-config-airbnb-base eslint-import-resolver-alias eslint-plugin-import -D

配置

TIP

新建.eslintrc文件规则配置如下(需要什么规则自己配置rules,下面rules个人规则配置):

json
{
  "extends": [
    "airbnb-base"
  ],
  "env": {
    "es6": true,
    "browser": true
  },
  "parserOptions": {
    "parser": "@babel/eslint-parser",
    "ecmaVersion": "latest"
  },
  "settings": {
    "import/ignore": [".js"],
    "import/resolver": {
      "alias": {
        "map": [
          [
            "@",
            "./src"
          ]
        ],
        "extensions": [
          ".js"
        ]
      }
    }
  },
  "rules": {
    "no-new": 0,
    "prefer-rest-params": 0,
    "no-restricted-syntax": [
      "error",
      "ForInStatement",
      "LabeledStatement",
      "WithStatement",
      "BinaryExpression[operator='in']"
    ],
    "global-require": 0,
    "import/prefer-default-export": 0,
    "no-plusplus": 0,
    "no-continue": 0,
    "import/extensions": 0,
    "max-len": 0,
   "no-param-reassign": ["error", { "props": true, "ignorePropertyModificationsFor": ["state"] }]
  }
}

lint自动修复

TIP

再创建一个.vscode文件夹里面再创建一个settings.json文件如下(这样再ctrl + s保存的时候一些常规语法错误就能修复了):

json
{
  "eslint.validate": [
    "javascript",
    "javascriptreact",
  ],
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
  },
}

Released under the MIT License.