装饰器相关的ESLint规则有哪些

解读

国内前端面试中,装饰器(Decorator)已经从“实验语法”变成“高频考点”。

  1. 面试官真正想听的是:
    • 你能否把“装饰器语法支持”与“装饰器代码质量”拆成两条线;
    • 你是否知道 Babel、TypeScript、SWC 三套编译链路在 ESLint 里的差异;
    • 你能否给出“开规则—配解析器—写共享配置”的完整落地步骤。
  2. 回答时先列“官方规则”,再补“社区规则”,最后落到“工程化集成”,才能体现“代码即规范”的落地能力。

知识点

  1. 解析器层面
    • @typescript-eslint/parser:把 decorator 语法解析成 ESTree 的 Decorator 节点;
    • @babel/eslint-parser:依赖 parserOpts.plugins = [['decorators', { decoratorsBeforeExport: true }]];
    • espree(默认解析器)在 ecmaVersion = 2022 + ecmaFeatures.legacyDecorators = true 时才认。
  2. 核心规则(eslint 本体,实验开关下生效)
    • no-unused-vars:对“装饰器工厂”未使用参数报红;
    • no-undef:装饰器未 import 会触发;
    • constructor-super / no-this-before-super:装饰器在类字段阶段展开,可能间接触发。
  3. TypeScript 插件专属规则(@typescript-eslint)
    • @typescript-eslint/decorator-position:强制同一行或换行;
    • @typescript-eslint/no-parameter-properties:防止“装饰器+构造器参数属性”混写;
    • @typescript-eslint/prefer-readonly-parameter-types:配合装饰器做不可变检查;
    • @typescript-eslint/explicit-function-return-type:装饰器工厂必须显式返回类型。
  4. 社区插件
    • eslint-plugin-decorator-position:统一 @decorator 换行风格;
    • eslint-plugin-angular 的 component-class-suffix / use-component-view-encapsulation:Angular 专属;
    • eslint-plugin-nestjs 的 api-property-matches-property:NestJS Swagger 装饰器必同步。
  5. 配置模板(国内团队常用)
    module.exports = {
      parser: '@typescript-eslint/parser',
      parserOptions: {
        ecmaVersion: 'latest',
        project: './tsconfig.json'
      },
      plugins: ['@typescript-eslint'],
      extends: [
        'eslint:recommended',
        '@typescript-eslint/recommended',
        '@typescript-eslint/recommended-requiring-type-checking'
      ],
      rules: {
        '@typescript-eslint/decorator-position': ['error', {
          properties: 'prefer-inline',
          methods: 'above'
        }],
        'no-unused-vars': 'off',
        '@typescript-eslint/no-unused-vars': ['error', { varsIgnorePattern: '^_' }]
      }
    };
    
  6. 工程化卡点
    • VS Code 保存时自动修复:settings.json 里 eslint.codeActionOnSave.rules 需把 decorator-position 加白名单;
    • Git Hooks(husky + lint-staged)必须加 --cache --fix,否则 monorepo 全量检查 3 万文件会超时;
    • CI 阶段:若用 SWC 编译,需单独跑 eslint --no-eslintrc -c .eslintrc.swc.js 做双通道校验,防止 AST 差异漏检。

答案

“装饰器相关的 ESLint 规则”要分三层回答:

  1. 本体规则:在 parser 打开实验语法后,no-unused-vars、no-undef 等通用规则即生效;
  2. TypeScript 插件规则:重点记忆 @typescript-eslint/decorator-position,它能强制团队统一“装饰器写在行尾还是独占一行”,是国内 Code Review 里最常吵的细节;
  3. 社区生态:Angular 项目用 eslint-plugin-angular,NestJS 项目用 eslint-plugin-nestjs,里面都有装饰器专属规则。
    落地时,先选解析器(TypeScript 项目直接 @typescript-eslint/parser),再在 extends 里引入 @typescript-eslint/recommended,最后把 decorator-position 打开并配好 Git Hooks 与 CI 双通道,就能实现“代码即规范”的自动化管控。

拓展思考

  1. 装饰器元数据反射:当使用 reflect-metadata 时,ESLint 无法静态分析运行期元数据键名,可自定义规则利用 TypeScript typeChecker 把“未声明的元数据 key”提示出来,实现“编译时反射安全”。
  2. 混合装饰器与装饰器工厂:工厂返回箭头函数时,no-this-before-super 可能误报,需要给规则提 PR 或在 overrides 里对 *.decorator.ts 文件关闭该规则。
  3. 未来标准:Decorators 已进 Stage 3,espree 默认解析器将在 ECMAScript 2023 稳定开启,届时可移除 parserOptions.ecmaFeatures.legacyDecorators,减少一次 AST 转换,CI 单测可提速 8% 左右。