Nest.js项目的ESLint配置要点

解读

面试官抛出“Nest.js项目的ESLint配置要点”,并不是想听你背一份 .eslintrc 的 JSON,而是考察三件事:

  1. 你是否理解 Nest 技术栈(TypeScript + Decorator + 模块化)带来的独特语法风险;
  2. 你是否能把企业级落地场景(Monorepo、微服务、CI、Git 提交卡口)映射到规则层面;
  3. 你是否具备“把规范写死、把例外收口”的工程化思维,而不是“配完就扔”。
    国内一线团队普遍用pnpm Monorepo + Husky + GitLab CI,面试时若能把“规则、性能、卡口、性能、性能”说满三遍,基本就稳了。

知识点

  1. Parser 与生态版本锁

    • @typescript-eslint/parser 必须与项目 TypeScript 版本对齐(国内镜像源常滞后,需锁版本)。
    • parserOptions.project 指向 tsconfig.eslint.json,避免把编译产物、jest.config、dist 等拖进解析耗时。
  2. 装饰器语法双开关

    • parserOptions.ecmaFeatures.legacyDecorators = true 保证 @Controller() 不被解析报错。
    • @typescript-eslint/decorator-class-property 规则强制“装饰器写在类属性最上方”,防止顺序错乱导致的元数据丢失。
  3. 模块边界规则

    • import/no-internal-modules 禁止跨模块 import ../cats/dto 式“相对越级”,强制走 @app/cats/dto 别名,配合 tsconfig paths 与 eslint-plugin-import 的 typescript resolver。
    • node/no-extraneous-import 识别 monorepo 子包是否声明依赖,防止“幽灵依赖”上线后 pnpm 装包失败。
  4. Provider 注入检查

    • @typescript-eslint/no-unused-vars 的 argsIgnorePattern: '^_' 放行 _ctx 占位,但 eslint-plugin-unused-imports 自动清理未使用的 Repository、Injectable。
    • 自定义规则 no-unregistered-provider(部分大厂内部已落地):扫描 @Inject 的 token 是否在对应 Module 的 providers 数组里注册,运行时少一次 “Nest can’t resolve dependencies” 报错。
  5. 风格与性能

    • prettier 交权给 eslint-config-prettier,禁用冲突规则;CI 里用 eslint --cache --cache-strategy content 把 2w 文件项目检查耗时从 90s 压到 15s。
    • functional/no-class 这类纯函数式规则在 Nest 场景直接关掉,防止“为了函数而函数”导致无法使用 Decorator。
  6. 卡口链路

    • lint-staged 仅检查 git diff 文件,结合 husky pre-commit;MR 阶段用 eslint-nibble 生成增量报告,评论到 GitLab MR 线程,实现“增量清零、存量渐进”。
    • 生产镜像构建前跑一次 eslint . --max-warnings 0,任何 warning 都阻断 docker build,保证“上线零警告”红线。

答案

“Nest 项目的 ESLint 配置核心是把 TypeScript 装饰器语法、模块别名、Provider 注入这三个特色风险管死。
第一步,用 @typescript-eslint/parser 并锁版本,parserOptions.project 指向独立的 tsconfig.eslint.json,把 dist、node_modules 排除掉,减少 40% 解析耗时。
第二步,打开 legacyDecorators 与 decorator-class-property 规则,保证 @Controller、@Injectable 顺序正确,元数据不会丢。
第三步,通过 eslint-plugin-import 的 resolver 配好 tsconfig paths,加一条 import/no-internal-modules,禁止相对路径越级引用,强制走 @app/ 别名,杜绝 Monorepo 下的模块耦合。
第四步,自定义规则扫描 @Inject(token) 是否已在 Module 的 providers 里注册,提前发现依赖注入错误;同时用 unused-imports 自动清理未用的 Repository,防止循环依赖。
第五步,风格层交给 Prettier,ESLint 只保留质量规则;CI 里加 --cache 与 max-warnings 0,MR 阶段用 eslint-nibble 增量报告,评论到 GitLab 线程,实现增量清零。
上线前再跑一次全量检查,任何 warning 都阻断镜像构建,保证线上代码零警告、零幽灵依赖。”

拓展思考

  1. 微服务场景下,每个子服务独立 tsconfig.eslint.json,如何共享一份“基础规则”又能让各自覆盖?
    答:用 eslint-config-neon 思路,把公共规则打成 npm 包,子服务 .eslintrc.js 里 extends: ['@myorg/eslint-config-nest'],再局部 rules: { 'no-console': 'off' } 覆盖;配合 root: true 防止递归向上查找。

  2. 大型 Monorepo 缓存失效频繁,如何做到“秒级”增量?
    答:把 eslint --cache 的 .eslintcache 文件通过 pnpm --filter=<pkg> run lint 拆到各子包,GitLab CI cache key 用 eslint-cache-$CI_COMMIT_REF_SLUG-$PKG_NAME,实现分片缓存;同时把 TIMING=1 打开,慢规则(如 import/namespace)在 CI 摘要里高亮,定期优化。

  3. 如果团队决定从 TSLint 迁移 ESLint,但存量代码 3k 警告,如何“边迁移边交付”?
    答:用 eslint-nibble --rule @typescript-eslint/no-explicit-any 逐条规则认领,配合 // eslint-disable-next-line @typescript-eslint/no-explicit-any -- TODO #1234 收口,工单跟踪;MR 机器人只检测新增文件,存量文件标记 /* eslint-disable */ 区域逐步缩小,实现“增量清零、存量渐进”,三个月内把 warning 降到 0。