vite 脚手架使用教程(七)-eslint

231 阅读3分钟

这是我参与「掘金日新计划 · 8 月更文挑战」的第10天,点击查看活动详情

首先论证一个问题,eslint 是用来干什么的?这边引入一段官方的解释

ESLint 是一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码。

那么实际上的意义就更大,不管是多人合作,还是个人完成项目,代码的规范,可以极大避免代码的出错性,那么格式的规范,就可以极大的增加代码的可读性,当你使用 vscode+eslint 的时候就会发现,当 不符合ESLint规则的地方,同时还会做一些简单的自我修正。

packjson 的指令配置

    "lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx,.json --max-warnings 0"

我们可以通过发布平台校验,当前指令是否正确,来判断这个代码是否可以成为正式的代码。

eslint

安装 eslint 插件

 npm i -D vite-plugin-eslint

这里可以理解为 eslint 规范的不同的库,同归这些规则的插件配置,可以更好的执行 eslint 的作用。

"eslint": "~8.20.0",
"eslint-config-prettier": "~8.5.0", // 本质上这个工具其实就是禁用掉了一些不必要的以及和 Prettier 相冲突的 ESLint 规则
"eslint-define-config": "~1.5.1", // 配置规则
"eslint-plugin-import": "~2.26.0", // import语句规则
"eslint-plugin-prettier": "~4.2.1",
"eslint-plugin-vue": "~9.3.0" // vue语句的规则

eslintrc.js

包配置文档

"plugin:vue/base"... 启用正确 ESLint 解析的设置和规则。

使用 Vue.js 3.x 的配置。

  • "plugin:vue/vue3-essential"... base,以及防止错误或意外行为的规则。
  • "plugin:vue/vue3-strongly-recommended"... 上面,加上大大提高代码可读性和/或开发体验的规则。
  • "plugin:vue/vue3-recommended"... 上面,加上强制执行主观社区默认值的规则,以确保一致性。

使用 Vue.js 2.x 的配置。

  • "plugin:vue/essential"... base,以及防止错误或意外行为的规则。
  • "plugin:vue/strongly-recommended"... 上面,加上大大提高代码可读性和/或开发体验的规则。
  • "plugin:vue/recommended"...以上,加上强制执行主观社区默认值以确保一致性的规则

eslintrc配置

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
    es6: true,
  },
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    ecmaVersion: 2020,
    sourceType: 'module',
    jsxPragma: 'React',
    ecmaFeatures: {
      jsx: true,
      tsx: true,
    },
  },
  plugins: ['@typescript-eslint', 'prettier', 'import'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:vue/vue3-recommended',
    'prettier',
  ],
  overrides: [
    {
      files: ['*.ts', '*.tsx', '*.vue'],
      rules: {
        'no-undef': 'off',
      },
    },
  ],
  rules: {
    // js/ts
    // 'no-console': ['warn', { allow: ['error'] }],
    'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
    camelcase: ['error', { properties: 'never' }],

    'no-var': 'error',
    'no-empty': ['error', { allowEmptyCatch: true }],
    'no-void': 'error',
    'prefer-const': ['warn', { destructuring: 'all', ignoreReadBeforeAssign: true }],
    'prefer-template': 'error',
    'object-shorthand': ['error', 'always', { ignoreConstructors: false, avoidQuotes: true }],
    'block-scoped-var': 'error',
    'no-constant-condition': ['error', { checkLoops: false }],

    'no-redeclare': 'off',
    '@typescript-eslint/no-redeclare': 'error',
    '@typescript-eslint/ban-ts-comment': 'off',
    '@typescript-eslint/ban-types': 'off',
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    '@typescript-eslint/no-empty-function': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/no-non-null-assertion': 'off',
    '@typescript-eslint/no-non-null-asserted-optional-chain': 'off',
    // '@typescript-eslint/consistent-type-imports': ['error', { disallowTypeAnnotations: false }],
    '@typescript-eslint/no-var-requires': 'off',
    '@typescript-eslint/no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
      },
    ],
    'no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
      },
    ],

    // vue
    'vue/no-v-html': 'off',
    'vue/require-default-prop': 'off',
    'vue/require-explicit-emits': 'off',
    'vue/multi-word-component-names': 'off',

    // prettier
    'prettier/prettier': 'error',

    // import
    'import/first': 'error',
    'import/no-duplicates': 'error',
    'import/order': [
      'error',
      {
        groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],

        pathGroups: [
          {
            pattern: 'vue',
            group: 'external',
            position: 'before',
          },
          {
            pattern: '@vue/**',
            group: 'external',
            position: 'before',
          },
          {
            pattern: 'ant-design-vue',
            group: 'internal',
          },
        ],
        pathGroupsExcludedImportTypes: ['type'],
      },
    ],
  },
};