.eslintrc.js 2.91 KB
module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
    es2021: true
  },
  extends: [
    'eslint:recommended'
    // 暂时不使用 '@vue/eslint-config-prettier',手动配置规则避免冲突
  ],
  plugins: ['vue'],
  parser: 'vue-eslint-parser', // 使用 Vue parser
  parserOptions: {
    parser: 'espree', // JavaScript parser
    ecmaVersion: 2021,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true
    }
  },
  rules: {
    // Vue 规则(手动配置)
    'vue/multi-word-component-names': 'off', // 允许单词组件名
    'vue/no-v-html': 'warn', // 警告使用 v-html(XSS 风险)
    'vue/require-default-prop': 'off', // 不强制 prop 默认值
    'vue/require-prop-types': 'off', // 不强制 prop 类型(使用 JSDoc)
    'vue/no-unused-vars': 'warn', // 警告未使用的变量
    'vue/no-unused-components': 'warn', // 警告未使用的组件

    // 通用规则
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-unused-vars': ['warn', { argsIgnorePattern: '^_' }], // 警告未使用的变量
    'prefer-const': 'error', // 优先使用 const
    'no-var': 'error', // 禁止使用 var
    'eqeqeq': ['error', 'always'], // 强制使用 === 和 !==
    'curly': ['error', 'all'], // 强制使用大括号
    'brace-style': ['error', '1tbs'], // 大括号风格
    'quotes': ['error', 'single', { avoidEscape: true }], // 单引号
    'semi': ['error', 'never'], // 不使用分号
    'comma-dangle': ['error', 'never'], // 不允许尾随逗号
    'arrow-parens': ['error', 'as-needed'], // 箭头函数参数括号
    'object-curly-spacing': ['error', 'always'], // 对象大括号空格
    'array-bracket-spacing': ['error', 'never'], // 数组方括号无空格

    // 代码质量
    'no-duplicate-imports': 'error', // 禁止重复导入
    'no-useless-return': 'error', // 禁止无用的 return
    'no-else-return': 'error', // 禁止 else return(提前 return)
    'prefer-template': 'error', // 优先使用模板字符串
    'template-curly-spacing': 'error', // 模板字符串大括号内无空格
    'object-shorthand': ['error', 'always'], // 对象属性简写

    // 放宽一些规则,避免过多警告
    'no-prototype-builtins': 'off', // 允许使用原型方法
    'no-nested-ternary': 'off', // 允许嵌套三元表达式
    'no-param-reassign': 'off', // 允许修改参数
    'consistent-return': 'off' // 不要求一致的 return
  },
  globals: {
    // Taro 全局变量
    wx: 'readonly',
    getCurrentPages: 'readonly',
    getApp: 'readonly',

    // NutUI 全局变量
    NutUI: 'readonly'
  },
  overrides: [
    // 测试文件规则
    {
      files: ['**/__tests__/**/*', '**/*.test.js', '**/*.spec.js'],
      env: {
        jest: true,
        node: true
      },
      rules: {
        'no-console': 'off'
      }
    }
  ]
}