在Vue项目中如何处理模板和脚本部分的语法检查

解读

面试官问的不是“会不会配 ESLint”,而是想知道你能否把 ESLint 真正落地到 Vue 技术栈:

  1. 模板(template)不是 JS,ESLint 原生无法解析,必须借助编译器与专属插件;
  2. 脚本(script)分 JS 与 TS,还要兼容 Vue SFC 的 setup、defineProps 等新语法;
  3. 国内主流用 Vite / Webpack + VS Code,规则包选型、性能、CI 卡点、旧项目迁移,每一步都有“坑”。
    回答时要体现“方案完整、踩坑经验、可落地、可度量”。

知识点

  1. AST 链路:vue-eslint-parser 把 .vue 拆成三段 AST,分别交给 @typescript-eslint/parser、eslint-plugin-vue 处理。
  2. 规则分层:
    • 优先级 A(错误):vue/no-unused-vars、vue/no-multiple-template-root(Vue2)
    • 优先级 B(风格):vue/html-self-closing、vue/max-attributes-per-line
    • 优先级 C(业务):自定义规则,如强制使用 <n-button> 代替 <el-button>
  3. 配置组合:
    • extends: ['@vue/eslint-config-typescript', 'plugin:vue/vue3-essential']
    • parser: 'vue-eslint-parser', parserOptions.parser: '@typescript-eslint/parser'
  4. 性能优化:
    • 缓存:eslint --cache --cache-location node_modules/.cache/eslint
    • 并行:pnpm 或 yarn workspaces 下用 eslint . --max-warnings 0 配合 lint-staged
  5. 工程集成:
    • VS Code 插件:dbaeumer.vscode-eslint + 配置 eslint.validate: ['vue', 'javascript', 'typescript']
    • Git Hooks:husky + lint-staged,仅检查增量文件,平均耗时 <3 s
    • CI:GitLab-CI 中开启 eslint --format junit -o reports/eslint.xml,在合并请求里可视化错误趋势
  6. 老项目迁移:
    • 先 --no-eslintrc 生成 .eslintrc.auto.js,用 eslint-find-rules 找出差异;
    • 分模块灰度:npx eslint --rule 'vue/html-self-closing: off' src/modules/legacy/**;
    • 配套脚本批量修复:eslint --fix + prettier --write,再提交一次“大括号”MR,后续 MR 禁止回退。

答案

“我分四步把 ESLint 覆盖到 Vue 的模板和脚本,并在两周内让 30 万行存量代码零阻塞上线。”

  1. 技术选型
    采用 vue-eslint-parser + @typescript-eslint/parser 双解析器,规则包用 @vue/eslint-config-typescript(已集成 vue3-essential、prettier、@typescript-eslint/recommended)。
  2. 配置最小闭环
    // .eslintrc.js
    module.exports = {
      root: true,
      parser: 'vue-eslint-parser',
      parserOptions: {
        parser: '@typescript-eslint/parser',
        ecmaVersion: 2022,
        sourceType: 'module',
        project: './tsconfig.json'
      },
      extends: [
        '@vue/eslint-config-typescript',
        'plugin:vue/vue3-recommended',
        '@vue/eslint-config-prettier'
      ],
      rules: {
        // 业务定制
        'vue/no-unused-refs': 'error',
        '@typescript-eslint/no-explicit-any': ['error', { fixToUnknown: true }]
      }
    }
    
  3. 工具链集成
    • VS Code:settings.json 里开启 “eslint.validate”: [“vue”, “javascript”, “typescript”],保存即修复。
    • Git Hooks:lint-staged 仅检查 git diff 文件,平均耗时 2.7 s,不影响 commit 流畅度。
    • CI:GitLab 合并请求阶段运行 eslint --max-warnings 0 --format codeframe,失败即阻断;同时产出 junit 报告,错误数趋势在 MR 面板可视化。
  4. 存量迁移与度量
    先关闭所有风格规则,仅保留错误级别规则,通过 eslint --fix 批量修正,生成一次“大括号”MR;
    再按模块逐步开启风格规则,用 eslint-find-rules 对比差异,每周下降 warning 数量 20%,两周后清零;
    最后把规则升级为 vue3-strongly-recommended,配合 SonarQube 质量门禁,代码异味率从 2.8% 降到 0.6%。

拓展思考

  1. 当项目同时存在 Vue2 与 Vue3 分支,如何共享一套规则且避免冲突?
    答:用 overrides 字段,针对 .vue 文件按路径匹配不同 extends,例如 /legacy/* 继承 plugin:vue/vue2-recommended,/next/** 继承 vue3-essential;CI 阶段通过 NODE_PATH 动态切换规则集。
  2. 模板里出现大量 v-html,如何自定义规则防止 XSS?
    答:基于 eslint-plugin-vue 的 AST,写自定义规则检测 CallExpression callee 为 v-html 且没有 sanitize 调用,直接报 error;规则单元测试用 vue-eslint-parser 的 parseForESLint 方法,保证覆盖率 100%。
  3. 微前端场景下,主应用与 Vue 子应用仓库独立,如何统一规范?
    答:把 eslint-config 抽成私有 @scope/eslint-config-vue,发版到自建 Verdaccio;各子应用在 CI 里执行 npx @scope/eslint-config-vue sync,保证版本一致;主应用通过 webpack 的 ModuleFederation 插件把 eslint-report 聚合到同一 SonarQube 项目,实现“规范即服务”。