如何配置ESLint检查共享模块的代码质量

解读

“共享模块”在国内通常指:

  1. 公司级私有 npm 包(如 @company/eslint-config-base、@company/utils)
  2. Monorepo 中的 packages/* 目录
  3. 被多个仓库引用的 Git Submodule 或独立 Git 仓库

面试官真正关心的是:你能否把 ESLint 从“单仓库玩具”升级为“跨仓库、跨团队、可持续集成”的工业级方案。核心痛点有三:

  • 规则版本如何统一升级而不逼业务线连夜改代码
  • 不同技术栈(Vue2/3、React、Node、微前端)如何继承同一套底线规则
  • 如何在 CI 阶段“强制卡点”,防止一个低级错误污染所有依赖方

因此,回答必须覆盖“配置分层 + 发包策略 + 集成卡点”三位一体,并给出可落地的国内工程化细节(如 Nexus 私有源、GitLab CI、changesets 发包、husky + lint-staged)。

知识点

  1. 配置分层原理:extends 数组的“后覆盖前”优先级、overrides 的 glob 匹配顺序
  2. 配置发布物:eslint-config-* 与 eslint-plugin-* 的区别,以及为何必须把 parser、parserOptions、plugins、rules 打成一个“全家桶”配置包
  3. 语义化版本与升级策略:semver 的 minor 位新增规则时,如何借助 eslint-find-rules 与 --print-config 做“影响面预检”
  4. Monorepo 场景:root eslintrc 的 root:true 与 project reference 的 parserOptions.tsconfigRootDir 如何联动
  5. CI 卡点:gitlab-ci 的 parallel matrix、changes 语法,配合 eslint --cache --cache-strategy content 把 10 万行代码检查耗时压到 30 秒以内
  6. 国内私有源:.npmrc 的 @company:registry=https://nexus.company.cn/repository/npm-private/ 与 publishConfig.access=restricted
  7. 渐进式治理:eslint-config-prettier 关闭冲突规则、eslint-plugin-deprecation 拦截废弃 API、@typescript-eslint/consistent-type-imports 降低打包副作用

答案

(以下步骤可直接照搬到面试陈述,时间控制在 3 分钟,突出“可落地、可回滚、可灰度”)

第一步,创建“底线配置包” 在公司私有组织下新建 @company/eslint-config-base,目录结构: packages/eslint-config-base ├── index.js # 默认导出 js 规则 ├── node.js # Node 服务专用 ├── react.js # React 技术栈 ├── vue.js # Vue3 技术栈 ├── package.json │ ├── name: "@company/eslint-config-base" │ ├── version: "1.0.0" │ ├── main: "index.js" │ ├── publishConfig.access: "restricted" │ ├── peerDependencies: { "eslint": "^8.57.0" } │ └── dependencies: { "@typescript-eslint/eslint-plugin": "^7", "@typescript-eslint/parser": "^7", "eslint-plugin-import": "^2", "eslint-config-prettier": "^9" }

index.js 内容示例: module.exports = { root: true, # 阻止继续向上查找 parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint', 'import'], extends: [ 'eslint:recommended', '@typescript-eslint/recommended', 'prettier', # 必须放最后,关闭冲突 ], rules: { '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], 'import/no-cycle': 'error', }, overrides: [ { files: ['**/*.test.{js,ts}'], rules: { '@typescript-eslint/no-explicit-any': 'off' }, }, ], };

第二步,发包与版本策略

  1. 采用 changesets 工具做“集中式变更日志”,合并到 main 分支后自动触发 GitHub Action 或 GitLab CI 调用 npm publish
  2. 遵循 semver:只新增规则必须发 minor;调整错误等级(warn→error)视为 breaking,发 major
  3. 在 README 中给出“升级命令 + 影响面扫描”脚本: npx eslint-find-rules --unused --current @company/eslint-config-base npx eslint . --print-config src/index.ts | jq '.rules' > /tmp/new.rules.json diff old.rules.json /tmp/new.rules.json

第三步,业务仓库接入

  1. 安装 npm i -D eslint @company/eslint-config-base
  2. 新建 .eslintrc.js module.exports = { extends: ['@company/eslint-config-base/react'] };
  3. package.json 追加脚本 "scripts": { "lint": "eslint . --cache --cache-strategy content --max-warnings 0", "lint:fix": "npm run lint -- --fix" }
  4. 集成 lint-staged + husky(安装依赖后自动写入 .husky/pre-commit) npx husky add .husky/pre-commit 'npx lint-staged' 并在 package.json 中配置 "lint-staged": { "*.{js,ts,vue}": "eslint --cache --fix" }

第四步,CI 强制卡点 GitLab CI 片段: eslint: stage: check image: node:18-alpine cache: key: $CI_COMMIT_REF_SLUG paths: [.eslintcache] script: - npm ci - npm run lint only: changes: ["src/**/", "package.json", ".eslintrc."]

若检查失败,MR 无法合并;缓存命中后平均耗时 25 秒,满足国内“午饭前合并”节奏。

第五步,共享模块自身也要被检查 在共享模块仓库里,把 eslint-config-base 作为 devDependency 自引用: extends: ['./index.js'] 并在 CI 中执行 npm run lint,防止“配置包自己违反自己”的尴尬。

通过以上五步,即可实现“一处改规则、全公司无痛升级”,且历史代码可灰度修复,保障共享模块长期高质量。

拓展思考

  1. 如果公司同时存在 Vue2 与 Vue3,如何在一个 config 包里用 overrides 区分 .vue 的 parser 选项,避免两份包? 答:在 vue.js 里用 overrides 的 files: ['**/.vue'] 并动态读取项目根目录的 vue-demi 版本,决定 parserOptions.ecmaVersion 与 vue-eslint-parser 的 parser 字段。

  2. 大型 Monorepo 中,每个子包依赖的 tsconfig 路径别名不同,如何防止 import/no-unresolved 误报? 答:在 eslint-config-base 里把 import/resolver 配成 eslint-import-resolver-typescript,并设置 project: ['./packages/*/tsconfig.json'],利用 TypeScript 自己的路径解析。

  3. 国内网络拉取 npm 慢,CI 频繁超时,如何加速? 答:把 @company/eslint-config-base 与相关 parser/plugin 提前打成一个 docker 镜像,内网 harbor 托管;CI 直接引用该镜像,无需每次 npm ci。

  4. 规则升级导致 5000 条历史错误,业务线无力一次性修复,如何“渐进式”? 答:在 config 包里新增一个 legacy.js,只关闭新增规则;业务线先 extends: ['@company/eslint-config-base/legacy'],后续用 eslint-interactive 或 eslint-nibble 逐条打开并修复,MR 中只允许修改的文件通过新规则。

  5. 未来迁移到 ESLint Flat Config(eslint.config.js)后,上述发包策略是否仍然适用? 答:适用。把导出的 commonjs 对象换成 flat config 数组,仍通过 npm 版本管理;只是业务线需要把 .eslintrc.* 改成 eslint.config.js 并在 package.json 中标记 "type":"module" 或使用 .cjs 后缀,CI 脚本无需改动。