1.0.0 • Published 2 years ago

@digital-rd/eslint-config-conventional v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

eslint-config-conventional

一份适用于JavaScript/TypeScript/Vue项目的ESLint配置规范。

快速开始

根据对应的技术栈选择对应的配置:

使用方法

基础依赖

在开始使用本配置前,请先确认以下基础依赖的版本,不同配置的依赖有所差异。

  • eslint >=8.12.0
  • @babel/core >= 7.18.0
  • @babel/eslint-parser >= 7.17.0
  • @typescript-eslint/eslint-plugin >= 5.25.0
  • @typescript-eslint/parser >= 5.25.0
  • eslint-plugin-vue >= 9.0.1
  • typescript >= 4.6.4
  • vue-eslint-parser >= 9.0.2

Base(built-in)

适用于原生JavaScript项目,使用ESLint的规则,parser使用的是@babel/eslint-parser ,是本规范的默认配置。

依赖

yarn add -D eslint @babel/eslint-parser eslint-config-conventional

配置

// .eslintrc.js
module.exports = {
  extends: [
    'conventional',
  ],
  env: {
    // 你的环境变量(包含多个预定义的全局变量)
    //
    // browser: true,
    // node: true,
    // mocha: true,
    // jest: true,
    // jquery: true
  },
  globals: {
    // 你的全局变量(设置为 false 表示它不允许被重新赋值)
    //
    // myGlobal: false
  },
  rules: {
    // 自定义你的规则
  },
};

Vue

适用于 JS Vue2 的项目,继承了Base配置,并启用了 eslint-plugin-vue 插件的规则,parser使用了 vue-eslint-parser

依赖

yarn add -D eslint @babel/eslint-parser vue-eslint-parser eslint-plugin-vue eslint-config-conventional

配置

// .eslintrc.js
module.exports = {
  extends: [
    'conventional',
    'conventional/vue'
  ],
  env: {
    // 你的环境变量(包含多个预定义的全局变量)
    //
    // browser: true,
    // node: true,
    // mocha: true,
    // jest: true,
    // jquery: true
  },
  globals: {
    // 你的全局变量(设置为 false 表示它不允许被重新赋值)
    //
    // myGlobal: false
  },
  rules: {
    // 自定义你的规则
  },
};

TypeScript

适用于 TypeScript 项目,继承了Base配置,并启用了 @typescript-eslint/eslint-plugin 插件的规则,parser使用了@typescript-eslint/parser

依赖

yarn add -D eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-conventional

配置

// .eslintrc.js
module.exports = {
  extends: [
    'conventional',
    'conventional/typescript'
  ],
  env: {
    // 你的环境变量(包含多个预定义的全局变量)
    //
    // browser: true,
    // node: true,
    // mocha: true,
    // jest: true,
    // jquery: true
  },
  globals: {
    // 你的全局变量(设置为 false 表示它不允许被重新赋值)
    //
    // myGlobal: false
  },
  rules: {
    // 自定义你的规则
  },
};

Vue TypeScript

适用于 TS Vue2 项目,继承了 Base Vue的配置,并启用了 @typescript-eslint/eslint-plugin 插件的规则,parser使用了 @typescript-eslint/parser

依赖

yarn add -D eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-vue eslint-config-conventional

配置

// .eslintrc.js
module.exports = {
  extends: [
    'conventional',
    'conventional/vue',
    'conventional/typescript'
  ],
  env: {
    // 你的环境变量(包含多个预定义的全局变量)
    //
    // browser: true,
    // node: true,
    // mocha: true,
    // jest: true,
    // jquery: true
  },
  globals: {
    // 你的全局变量(设置为 false 表示它不允许被重新赋值)
    //
    // myGlobal: false
  },
  rules: {
    // 自定义你的规则
  },
};

Vue3 TypeScript

适用于 TS Vue3 项目,继承了 Base plugin:vue3的配置,对于Vue3新增的规则继承插件 eslint-plugin-vue的vue3-recommended ,并启用了 @typescript-eslint/eslint-plugin 插件的规则。parser使用了 @typescript-eslint/parser

依赖

yarn add -D eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-vue eslint-config-conventional

配置

// .eslintrc.js
module.exports = {
  extends: [
    'conventional',
    'conventional/vue3',
    'conventional/typescript'
  ],
  env: {
    // 你的环境变量(包含多个预定义的全局变量)
    //
    // browser: true,
    // node: true,
    // mocha: true,
    // jest: true,
    // jquery: true
  },
  globals: {
    // 你的全局变量(设置为 false 表示它不允许被重新赋值)
    //
    // myGlobal: false
  },
  rules: {
    // 自定义你的规则
  },
};

Prettier

本规范已经不包含所有样式相关的规则,故不需要引入 eslint-config-prettier。只需要安装 prettier 及相关 VSCode 插件即可。

依赖

  • eslint-config-conventional

配置

// .prettierrc.js
module.exports = {
  ...require('@eslint-config-conventional/prettier')
}

常见问题

在 VSCode 中使用

在 VSCode 中,默认 ESLint 并不能识别 .vue.ts.tsx 文件,需要在「文件 => 首选项 => 设置」里做如下配置:

{
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "vue",
    "typescript",
    "typescriptreact"
  ]
}

保存时自动修复 ESLint 错误

如果想要开启「保存时自动修复」的功能,你需要配置 .vscode/settings.json

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

VSCode 中的 autoFixOnSave 没有效果

如果需要针对 .vue.ts.tsx 文件开启 ESLint 的 autoFix,则需要配置成:

{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    {
      "language": "vue",
      "autoFix": true
    },
    {
      "language": "typescript",
      "autoFix": true
    },
    {
      "language": "typescriptreact",
      "autoFix": true
    }
  ]
}

迁移指南

  1. 按需选择适合的配置
  2. 根据选择配置的说明文档安装对应的依赖
  3. 添加/修改项目的eslint配置文件,原则上尽量减少自定义配置
// .eslintrc.js
module.exports = {
  // 按需选择对应的配置
  extends: [
    'conventional',
  ],
  // 以下配置酌情处理
  env: {
    // 你的环境变量(包含多个预定义的全局变量)
    //
    // browser: true,
    // node: true,
    // mocha: true,
    // jest: true,
    // jquery: true
  },
  globals: {
    // 你的全局变量(设置为 false 表示它不允许被重新赋值)
    //
    // myGlobal: false
  },
  rules: {
    // 自定义你的规则
  },
};
  1. 新增/修改项目的prettier配置文件
  2. 重启eslint、ts等服务

参考