ESLint

ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误。

英文文档地址:https://eslint.org/ 中文文档地址:https://eslint.bootcss.com/

安装

  1. 新建项目,并在目录下初始化npm(如果没有):

    1. npm init -y
  2. 安装ESLint(推荐本地安装)

    1. # 本地安装
    2. npm install eslint --save-dev
    3. # 或全局安装:
    4. npm install eslint --global
  3. 初始化配置文件

    1. ./node_modules/.bin/eslint --init # Windows下路径需要使用反斜杠
    2. # 或
    3. eslint --init

配置选项:

  1. How would you like to use ESLint? · problems
  2. What type of modules does your project use? · esm
  3. Which framework does your project use? · none
  4. Does your project use TypeScript? · No
  5. Where does your code run? · browser, node
  6. What format do you want your config file to be in? · JavaScript

生成.eslintrc.js配置文件:

  1. module.exports = {
  2. "env": {
  3. "browser": true,
  4. "es2021": true,
  5. "node": true
  6. },
  7. "extends": "eslint:recommended",
  8. "parserOptions": {
  9. "ecmaVersion": 12,
  10. "sourceType": "module"
  11. },
  12. "rules": {
  13. }
  14. };

如果之前在初始化时未指定node环境,则需要在env属性中定义node的全局变量和作用域,或者指定module为全局变量,否则eslint:recommended的默认规则会报错:'module' is not defined no-undef

  1. module.exports = {
  2. "env": {
  3. // ...
  4. "node": true
  5. },
  6. // ...
  7. // 或
  8. "globals": {
  9. "module": true
  10. }
  11. };

也可以自行创建和配置.eslintrc.js文件。

运行

首先,创建src目录,并创建脚本文件src/index.js

  1. console.log('eslint');; // 多加一个分号

package.jsonscripts中配置脚本命令:

  1. {
  2. // ...
  3. "scripts": {
  4. // ...
  5. "eslint": "eslint ./**" // 检查当前路径下的文件
  6. // 或
  7. "eslint": "eslint ./src/**" // 只检查src路径下的文件
  8. // 或
  9. "eslint": "eslint --ext .js,.vue src" // 只检查src目录下的.js文件和.vue文件
  10. }
  11. }

创建.eslintignore文件,指定ESLint检查时忽略的文件和目录(如果有需要):

  1. /dist/
  2. /*.js
  3. /*.json

除此之外,为了指定ESLint检查和忽略的文件和目录,可以在eslint命令中设置,详见官方ESLint命令行文档:https://eslint.bootcss.com/docs/user-guide/command-line-interface

运行命令:

  1. npm run eslint

运行结果会报错,提示有不必要的分号:

  1. error Unnecessary semicolon no-extra-semi

如果需要eslint运行的时候自动修复(能够被修复的)问题,可以在package.json中添加命令:

  1. {
  2. // ...
  3. "scripts": {
  4. // ...
  5. "eslint": "eslint ./**"
  6. "eslint:fix" "eslint ./** --fix" // --fix表示修复问题
  7. }
  8. }

运行命令:

  1. npm run eslint:fix

原本代码中多余的分号被删除。

VS Code安装ESLint插件

编写完代码,再运行命令检查语法问题是繁琐的,而且有些问题应该在编码阶段就被发现和修复。对此,可以在VS Code中搜索和安装ESLint插件,该插件会检测和应用项目中的.eslintrc.js,并且对不符合规则的代码作出提示,还能对错误进行修改。

img

img

如果需要在保存文件的时候自动根据规则修复,可以在settings.json文件中添加:

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

打开settings.json:File->Preferences->Settings->Text Editor->Code Actions On Save-> Edit in settings.json

配置项

参考官方文档的配置项:https://eslint.bootcss.com/docs/user-guide/configuring

之前的配置文件:

  1. module.exports = {
  2. "env": {
  3. "browser": true,
  4. "es2021": true,
  5. "node": true
  6. },
  7. "extends": "eslint:recommended",
  8. "parserOptions": {
  9. "ecmaVersion": 12,
  10. "sourceType": "module"
  11. },
  12. "rules": {
  13. }
  14. };

env

env定义了预定义的全局变量,例如:"node": true表示使用Node的全局变量,更多可用的环境参考官方文档。

extends

extends表示ESLint应用的规则的集合,值可以是字符串,也可以是字符串的数组,扩展的来源可以是三种:

  1. ESLint内置的扩展。例如:eslint:recommendedeslint:all,其中,eslint:recommended是官方推荐的规则;
  2. 可共享的扩展。通过npm提供的一些共享的包,是一些流行的风格指南/编码规范,例如standardairbnb,包名一般带eslint-config-前缀。可以手动通过npm安装,安装后会提示安装其他相关的包,按提示安装即可:
    1. npm install eslint-config-standard --save-dev
    2. npm install eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard --save-dev
    修改配置文件:
  1. module.exports = {
  2. // ...
  3. "extends": [
  4. "standard"
  5. ]
  6. };

除此之外,在运行eslint --init初始化配置文件时,可以直接从几个流行风格中指定和安装其中一个:

  1. How would you like to use ESLint? · style
  2. # ...
  3. How would you like to define a style for your project? · guide
  4. Which style guide do you want to follow? · standard
  1. 插件。参考下面要讲的插件属性。

plugins

虽然有多种官方的扩展可以选择,但是默认的扩展只能检查JavaScript语法,不能解析JSX或者Vue文件,对此,我们需要安装相关的插件。

  1. npm install eslint-plugin-vue --save-dev

在配置文件中指定插件以及使用的扩展规则:

  1. module.exports = {
  2. // ...
  3. "plugins": [
  4. "vue"
  5. ],
  6. "extends": [
  7. "standard",
  8. "plugin:vue/essential"
  9. // 或
  10. "plugin:vue/recommended"
  11. ]
  12. };

也可以在eslint --init初始化配置文件时配置:

  1. How would you like to use ESLint? · style
  2. # ...
  3. Which framework does your project use? · vue

验证插件是否安装成功,首先创建并初始化src/test.vue文件:

  1. <template>
  2. <div></div>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. test: "double"
  9. }
  10. }
  11. }
  12. </script>
  13. <style scoped>
  14. </style>

运行npm run eslint进行检查,或者已经在VS Code上安装了ESLint的插件,可以看到 报了若干错误,说明插件安装成功。

解析器配置:

  1. module.exports = {
  2. // ...
  3. "parserOptions": {
  4. // es版本号,6表示2015,12表示2021
  5. "ecmaVersion": 12,
  6. // 代码类型,"script"(默认)或"module"
  7. "sourceType": "module"
  8. // 语言特性
  9. "ecmaFeatures": {
  10. "globalReturn": true, // 允许在全局作用域下使用return语句
  11. "impliedStrict": true, // 启用全局 strict mode
  12. "jsx": true // 启用 JSX
  13. }
  14. }
  15. };

解析器

rules

有时扩展中的规则不是我们想要的,可以通过rules修改规则。ESLint中附带大量的规则,参考官方文档:https://cn.eslint.org/docs/rules/。要改变一个规则设置,你必须将规则 ID 设置为下列值之一:

  • “off” 或 0 - 关闭规则
  • “warn” 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
  • “error” 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)

例:

  1. module.exports = {
  2. // ...
  3. "rules": {
  4. "eqeqeq": "off", // 关闭全等检查
  5. "quotes": ["error", "double"] // 强制使用双引号,否则报错
  6. }
  7. };

在 Webpack 中使用 ESLint

  1. 安装相关的包
  1. npm install eslint eslint-loader eslint-friendly-formatter --save-dev
  1. 修改 webpack 配置文件 webpack.common.js
  1. const path = require('path')
  2. module.exports = {
  3. module: {
  4. rules: [
  5. {
  6. test: /\.js$/,
  7. loader: 'eslint-loader',
  8. enforce: 'pre',
  9. include: path.resolve(__dirname, 'src'),
  10. options: {
  11. formatter: require('eslint-friendly-formatter')
  12. }
  13. }
  14. ]
  15. }
  16. }
  1. 创建 .eslintrc.js 配置文件:
  1. module.exports = {
  2. root: true,
  3. parserOptions: {
  4. parser: 'babel-eslint',
  5. sourceType: 'module',
  6. ecmaVersion: 11
  7. },
  8. env: {
  9. browser: true
  10. },
  11. extends: ['eslint:recommended'],
  12. rules: {
  13. }
  14. }

如果需要使用流行风格指南 standard,需要安装以下包:

  1. npm install --save-dev "eslint-config-standard eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard

然后修改 .eslintrc.jsextends 选项:

  1. module.exports = {
  2. // ...
  3. extends: ['standard'],
  4. }
  1. 创建 .eslintignore 文件,部分路径和文件不启用 eslint 检查:
  1. /dist/
  2. /*.js

根目录下的配置相关的文件夹也应添加到该文件中

检查 Vue 文件及语法