eslint配置项说明

324 阅读3分钟

eslint.bootcss.com/docs/user-g…


module.exports = {
  root: true, // 为true根配置文件,否则会按照目录树向上搜索
  env: {
    // 执行环境
    browser: true,
    es2021: true,
  },
  parser: 'vue-eslint-parser', // https://eslint.vuejs.org/user-guide/#faq
  parserOptions: {
    // 语法解析器选项
    ecmaVersion: 12,
    parser: '@typescript-eslint/parser',
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  extends: [
    // 对基础配置进行扩展,可以配置共享配置包
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'plugin:prettier/recommended',
  ],
  plugins: ['vue', '@typescript-eslint'],
  rules: {
    // 校验规则
    // common
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'arrow-parens': ['error', 'as-needed'], // 要求箭头函数的参数使用圆括号 (arrow-parens)
    // note you must disable the base rule as it can report incorrect errors
    // use '@typescript-eslint/brace-style': ['error', '1tbs']
    'brace-style': 'off', // 大括号风格要求
    curly: ['warn', 'all'], // 当代码块只有一条语句时,JavaScript 允许省略大括号
    camelcase: 'off', // 要求使用骆驼拼写法
    'comma-style': ['error', 'last'], //规则强制在数组字面量、对象字面量和变量声明中使用一致的逗号风格。该规则不适用于以下两种情况:在逗号前后有换行符 (单独的逗号)单行数组字面量、对象字面量和变量声明
    'comma-dangle': [
      // 要求或禁止使用拖尾逗号
      'error',
      {
        arrays: 'always-multiline',
        objects: 'always-multiline',
        imports: 'always-multiline',
        exports: 'always-multiline',
        functions: 'ignore',
      },
    ],
    'eol-last': 'error', // 要求在非空文件的末尾至少有一个换行
    indent: 'off', // 此规则强制执行一致的缩进样式。 默认样式为 4 个空格。
    'vue/script-indent': 'off', // 在 <script> 中强制执行一致的缩进
    'no-trailing-spaces': 'error',
    'object-curly-spacing': ['error', 'always'],
    quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: true }],
    'no-useless-escape': 'off',
    'no-prototype-builtins': 'off',
    'no-undef': 'off', // 解决ts导入全局命名空间问题
    'prefer-const': 'off',

    // ts
    // 关闭函数返回值类型检查规则,在overrides中对ts文件及ts+vue模块单独开启该规则
    '@typescript-eslint/explicit-module-boundary-types': 'off', // 在导出的函数和类的公共类方法上需要显式的返回值和参数类型
    // 关闭!断言检查,后续有更好的方案后开启该规则
    '@typescript-eslint/no-non-null-assertion': 'off', // 禁止使用!后缀运算符进行非null断言
    '@typescript-eslint/brace-style': ['error', '1tbs'],
    '@typescript-eslint/no-empty-function': 'off', // 禁止使用空函数
    '@typescript-eslint/no-unused-vars': [
      // 禁止使用未使用的变量
      'error',
      {
        args: 'after-used',
        argsIgnorePattern: '^_',
        ignoreRestSiblings: true,
      },
    ],
    '@typescript-eslint/no-this-alias': 'off',
    '@typescript-eslint/ban-ts-comment': 'off',
    '@typescript-eslint/no-empty-interface': 'off',
    '@typescript-eslint/no-namespace': 'off',

    // vue
    'vue/attribute-hyphenation': 'off', // 在模板中的自定义组件上实施属性命名样式
    'vue/html-closing-bracket-spacing': 'error',
    'vue/max-attributes-per-line': 'off', // 强制每行的最大属性数
    'vue/require-default-prop': 'error', //需要道具的默认值
    'vue/one-component-per-file': 'off', // 强制每个组件应位于其自己的文件中

    'vue/html-self-closing': [
      // 实施自我封闭的风格
      'error',
      {
        html: {
          void: 'always',
          normal: 'never',
          component: 'always',
        },
      },
    ],
    'vue/component-tags-order': [
      // 可以用下面的库进行 vue tags 排序规则修改
      // https://github.com/kawamataryo/v-change-tags-order
      'error',
      {
        order: ['script', 'template', 'style'],
      },
    ],
    'vue/attributes-order': 'off', // 强制属性顺序
  },
  overrides: [
    // 为特定类型的文件指定处理器, 另外还支持processor配置指定的解析器
    {
      files: ['*.js'],
      rules: {
        '@typescript-eslint/no-var-requires': 0,
        '@typescript-eslint/brace-style': 0,
        '@typescript-eslint/no-unused-vars': 0,
      },
    },
  ],
};