如何为不同的文件类型设置不同的规则

解读

在国内一线/二线大厂的日常面试里,这道题常被用来区分“只会用 ESLint 跑脚本”与“真正能把 ESLint 落地到多技术栈工程”的候选人。
面试官想听到的不只是“写两个 extends”,而是:

  1. 你清楚 ESLint 的“配置级联 + 文件匹配”两条核心机制;
  2. 你能结合真实场景(如 Monorepo、混合 JS/TS/Vue/React、旧项目渐进迁移)给出可落地的分层方案;
  3. 你知道如何与 Prettier、Git Hooks、CI 流水线配合,避免“规则打架”或“CI 误报”;
  4. 你能权衡性能,避免 glob 过多导致 lint 耗时翻倍。

知识点

  1. 配置级联(Configuration Cascading):ESLint 从目标文件所在目录逐级向上查找配置文件,直到根目录,合并后生效。
  2. overrides 字段:在任一配置文件中,通过 overrides 数组可对“符合特定 glob 的文件”单独设置 parser、parserOptions、rules、plugins、env 等,优先级高于外层。
  3. 多配置文件:在 Monorepo 或混合技术栈场景下,可在子包/子目录放置独立的 .eslintrc.js,实现“目录级”隔离;配合 root: true 阻断向上查找,避免祖先配置污染。
  4. 文件匹配顺序:同一文件若命中多个 overrides 条目,后声明的条目覆盖先声明的;若同时受“目录级”配置文件与“ overrides” 约束,则 overrides 优先级最高。
  5. 性能优化:
    • 尽量把“同类型”规则收敛到同一个 overrides 条目,减少 glob 数量;
    • 对巨型目录使用 .eslintignore 提前剪枝;
    • 在 CI 中配合 --cache 与并行线程。
  6. 与 Prettier 协作:不同文件类型若需不同 Prettier 配置,可在 overrides 里通过 eslint-plugin-prettier 的 ruleOptions 指定对应 prettierConfig 路径,防止“双格式化”冲突。
  7. 渐进迁移:对老代码目录可临时关闭部分冲突规则,配合 eslint-disable-next-line 注释与“增量 lint”策略,避免一次性改动过大导致回归风险。

答案

落地思路分三步:
第一步,确定“文件类型”维度。
以常见中台项目为例:

  • src 下 .ts、.tsx 走 React + TypeScript 严格规则;
  • scripts/ 下的 .js 使用 Node 环境宽松规则;
  • legacy/ 下的 .js 暂时兼容 ES5,仅做语法错误校验;
  • *.vue 文件需同时校验 <template>、<script>、<style>。

第二步,在仓库根目录维护单一 .eslintrc.js,用 overrides 做分层:

module.exports = {
  root: true,
  // 公共基础
  env: { es2022: true },
  extends: ['eslint:recommended'],
  rules: {
    'no-console': 'warn',
  },
  overrides: [
    // 1) TypeScript React 源码
    {
      files: ['src/**/*.{ts,tsx}'],
      parser: '@typescript-eslint/parser',
      parserOptions: { project: './tsconfig.json' },
      plugins: ['@typescript-eslint', 'react', 'react-hooks'],
      extends: [
        'plugin:@typescript-eslint/recommended-requiring-type-checking',
        'plugin:react/recommended',
        'plugin:react-hooks/recommended',
        'prettier', // 必须放在最后,覆盖格式化冲突
      ],
      rules: {
        '@typescript-eslint/no-unused-vars': 'error',
        'react/react-in-jsx-scope': 'off', // React 17+ 无需引入 React
      },
      settings: { react: { version: 'detect' } },
    },
    // 2) Node 脚本
    {
      files: ['scripts/**/*.js'],
      env: { node: true },
      rules: {
        'no-console': 'off', // 脚本允许 console
        'global-require': 'off',
      },
    },
    // 3) 遗留目录
    {
      files: ['legacy/**/*.js'],
      env: { browser: true, es6: false },
      parserOptions: { ecmaVersion: 5 },
      rules: {
        'no-var': 'off',
        'prefer-const': 'off',
      },
    },
    // 4) Vue SFC
    {
      files: ['**/*.vue'],
      parser: 'vue-eslint-parser',
      parserOptions: {
        parser: '@typescript-eslint/parser', // <script lang="ts"> 时生效
        extraFileExtensions: ['.vue'],
        ecmaFeatures: { jsx: true },
      },
      extends: ['plugin:vue/vue3-recommended', '@vue/typescript/recommended', 'prettier'],
      rules: {
        'vue/multi-word-component-names': 'error',
      },
    },
  ],
};

第三步,在 package.json 中统一脚本,保证本地与 CI 行为一致:

"scripts": {
  "lint": "eslint . --cache --max-warnings 0",
  "lint:fix": "eslint . --fix --cache"
}

并在 .eslintignore 中剔除打包产物与第三方依赖:

dist
node_modules
*.min.js

如此即可“单配置文件”覆盖多文件类型,后续若子包差异过大,再在子包下新建 .eslintrc.js 并设置 root: true 做进一步隔离。

拓展思考

  1. Monorepo 场景:
    若使用 pnpm workspace,可在根目录提供 @scope/eslint-config 私有包,把上述 overrides 抽成可共享配置,各子包仅 extends: ['@scope/eslint-config'],既统一又避免复制粘贴。
  2. 大文件性能:
    对包含自动生成代码(如 protobuf、graphql typings)的目录,可在 overrides 里单独关闭所有规则,并配合 eslint-disable 注释模板,防止 lint 耗时飙升。
  3. 与 Git Hooks 结合:
    用 lint-staged + husky,仅对 staged 文件执行 eslint --cache --fix,命中对应 overrides,保证提交粒度最小、反馈最快。
  4. 规则灰度:
    对“即将启用”的硬性规则,可先设 warn 并配置 CI 允许 warnings,通过“告警率”看板统计逐步降到 0 后再改为 error,实现平滑升级。