关于eslint的一些API记录

404 阅读3分钟
// @ts-check
const { defineConfig } = require('eslint-define-config');
module.exports = defineConfig({
  // 默认情况下,ESLint 将在直到根目录的所有父文件夹中查找配置文件。
  // 如果您希望所有项目都遵循某种约定,那么这可能会很有用,
  // 但有时可能会导致意想不到的结果。若要将 ESLint 限制为特定的项目,
  // 请将“ root”: true 放在。Eslintrc.* package.json 文件的 eslintConfig 字段或 package.json 文件的。
  // Eslintrc.* 在你的项目的根级别上建立档案。当 ESLint
  // 发现一个“ root”配置时,它将停止查找父文件夹: true。
  root: true,
  env: {
    browser: true, // 浏览器全局变量
    node: true, // node全局变量
    es6: true, // 启用除模块以外的所有 ECMAScript 6特性(这会自动设置
  },
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    ecmaVersion: 2020, // 接受任何有效的 ECMAScript 版本号或“最新”
    sourceType: 'module',
    jsxPragma: 'React',
    ecmaFeatures: {
      jsx: true, // 启用解析 JSX
    },
  },
  extends: [
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'plugin:prettier/recommended',
    'plugin:jest/recommended',
  ],
  rules: {
    // 防止 <template> 中使用的 <script setup> 变量被标记为未使用
    'vue/script-setup-uses-vars': 'error',
    // 禁用 eslint 规则
    '@typescript-eslint/ban-ts-ignore': 'off',
    // 要求函数和类方法的显式返回类型
    '@typescript-eslint/explicit-function-return-type': 'off',
    // 禁止使用any类型
    '@typescript-eslint/no-explicit-any': 'off',
    // 除了import外禁止使用requires
    '@typescript-eslint/no-var-requires': 'off',
    // 不允许空函数
    '@typescript-eslint/no-empty-function': 'off',
    // 为自定义事件名称强制使用特定大小写。
    'vue/custom-event-name-casing': 'off',
    // 在定义之前禁止使用变量。
    'no-use-before-define': 'off',
    '@typescript-eslint/no-use-before-define': 'off',
    // 禁止@ts-<directive>使用注释或要求在指令后进行描述
    '@typescript-eslint/ban-ts-comment': 'off',
    // 禁止使用特定类型
    '@typescript-eslint/ban-types': 'off',
    // !不允许使用后缀运算符的非空断言
    '@typescript-eslint/no-non-null-assertion': 'off',
    // 要求导出函数和类的公共类方法的显式返回和参数类型
    '@typescript-eslint/explicit-module-boundary-types': 'off',
    // 禁止未使用的变量
    '@typescript-eslint/no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
      },
    ],
    'no-unused-vars': [
      'error',
      {
        argsIgnorePattern: '^_',
        varsIgnorePattern: '^_',
      },
    ],
    // 在函数括号之前强制执行一致的间距
    'space-before-function-paren': 'off',
    // 强制执行属性顺序
    'vue/attributes-order': 'off',
    // 对模板中的自定义组件强制执行 v-on 事件命名样式
    'vue/v-on-event-hyphenation': 'off',
    // 要求组件名称始终为多字
    'vue/multi-word-component-names': 'off',
    // 强制每个组件都应该在自己的文件中
    'vue/one-component-per-file': 'off',
    // 在标签的右括号之前要求或禁止换行
    'vue/html-closing-bracket-newline': 'off',
    // 强制每行的最大属性数
    'vue/max-attributes-per-line': 'off',
    // 在多行元素的内容之前和之后需要换行符
    'vue/multiline-html-element-content-newline': 'off',
    // 在单行元素的内容之前和之后需要换行符
    'vue/singleline-html-element-content-newline': 'off',
    // 对模板中的自定义组件强制执行属性命名样式
    'vue/attribute-hyphenation': 'off',
    // 需要 props 的默认值
    'vue/require-default-prop': 'off',
    // 此规则旨在强制将自动关闭标志作为配置的样式
    'vue/html-self-closing': [
      'error',
      {
        html: {
          void: 'always', // 众所周知的 HTML void 元素的样式要求在没有内容的元素处自动关闭
          normal: 'never', // 众所周知的 HTML 元素的样式,除了 void 元素禁止自动关闭
          component: 'always', // 自定义组件的样式 要求在没有内容的元素处自动关闭
        },
        svg: 'always', // 知名 SVG 元素的样式 要求在没有内容的元素处自动关闭
        math: 'always', // 著名的 MathML 元素的样式  要求在没有内容的元素处自动关闭
      },
    ],
  },
});