如何对“导出” React 代码做“语法”校验?
解读
“导出”指项目里所有会被外部引用到的 React 模块(组件、工具函数、Hook 等),既包括源码目录,也包括打包产物;“语法”校验不仅指 ES 语法合法性,还要覆盖 JSX 语法、TypeScript 语法、模块导出规范(默认导出、命名导出、循环依赖)以及国内常见的 ESLint 规则(阿里、腾讯、字节等大厂内部规范)。
面试官真正想考察的是:
- 你是否能把“语法校验”从开发阶段延伸到 CI 阶段,并覆盖到“导出”这一关键切面;
- 你是否熟悉 Jest 在“语法层”能做的极限,以及它如何与 ESLint、TypeScript、Babel、Webpack、Vite 等工具链配合;
- 你是否知道国内代码质量门禁(GitLab-CI、阿里云效、腾讯蓝盾、字节 Flow)对“零语法警告”的硬性要求,并能给出可落地的脚本与卡点方案。
知识点
- Jest 的“语法层”边界:Jest 本身只负责“运行时”测试,不内置静态语法检查;但可以通过自定义 runner、transform、watch 插件把 ESLint/TypeScript 诊断信息转成 Jest 的 test failure,从而把“语法错误”变成“用例失败”。
- 国内主流语法检查链路:
- ESLint(@typescript-eslint/parser、eslint-plugin-react、eslint-plugin-react-hooks)
- TypeScript tsc --noEmit
- Babel parser 的语法插件(jsx、decorators、dynamic import)
- 打包器语法检查:Webpack5 的 built-in parser、Vite 的 esbuild 语法阶段
- “导出”切面采集:
- 静态:使用 eslint-plugin-import 的 export 规则(named、default、namespace)
- 动态:使用 TypeScript Compiler API 遍历 SourceFile,收集 ExportDeclaration,结合 package.json 的 exports 字段做白名单校验
- Jest 与语法检查融合方案:
- jest-runner-eslint:把 ESLint 配置成 Jest 的 runner,每条规则对应一个 test suite
- jest-runner-tsc:把 tsc --noEmit 封装成 Jest runner,每条 ts 文件对应一个 test
- 自定义 transformer:在 Jest transform 阶段调用 Babel parser,若 parse 失败直接抛 Jest 语法错误,可定位到行列号
- 国内门禁卡点:
- 在 GitLab-CI 的 test 阶段先跑 jest-runner-eslint,失败则 MR 不可合并;
- 在云效 Flow 里配置“零警告”策略,把 jest --coverage 与 eslint --max-warnings=0 串行执行,任一失败即阻断发布;
- 在 monorepo 场景下,使用 jest projects 分别对 packages/*/src 做导出语法校验,避免“子包导出非法默认导出”导致主包构建失败。
答案
步骤一:安装依赖
npm i -D jest jest-runner-eslint eslint eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-import @typescript-eslint/parser @typescript-eslint/eslint-plugin
步骤二:在项目根目录新建 eslint.config.js(flat config,适配 ESLint9,国内大厂已逐步迁移)
import js from '@eslint/js'
import ts from '@typescript-eslint/eslint-plugin'
import tsParser from '@typescript-eslint/parser'
import reactPlugin from 'eslint-plugin-react'
import importPlugin from 'eslint-plugin-import'
export default [
js.configs.recommended,
{
files: ['**/*.{js,jsx,ts,tsx}'],
languageOptions: {
parser: tsParser,
parserOptions: { ecmaFeatures: { jsx: true }, project: './tsconfig.json' }
},
plugins: { react: reactPlugin, import: importPlugin },
rules: {
// 导出语法核心规则
'import/named': 'error',
'import/default': 'error',
'import/namespace': 'error',
'import/export': 'error',
'react/jsx-uses-react': 'error',
'react/jsx-uses-vars': 'error'
}
}
]
步骤三:在 jest.config.js 中把 ESLint 配置成 runner
export default {
runner: 'jest-runner-eslint',
displayName: 'eslint',
testMatch: ['<rootDir>/src/**/*.{js,jsx,ts,tsx}'],
testPathIgnorePatterns: ['/node_modules/', '/dist/']
}
步骤四:在 CI 脚本(.gitlab-ci.yml,国内云效/蓝盾同理)增加门禁
lint-export-syntax:
stage: test
script:
- npm run lint:export # 对应 package.json 中 "lint:export": "jest --selectProjects=eslint"
only:
- merge_requests
allow_failure: false
步骤五:本地开发阶段使用 Jest watch 模式
npm run lint:export -- --watch
一旦“导出”语法违规(例如默认导出与命名导出混用、JSX 未引入 React、TypeScript 导出类型未标记 type),Jest 会立即把 ESLint 错误转成用例失败,行列号精准定位,开发者保存即见红,达到“语法校验”与“单测反馈”同频。
拓展思考
- 如果项目使用 Vite + esbuild,想利用 esbuild 的“语法阶段”更快校验,可自定义 jest-runner-esbuild:在 runner 里调用 esbuild.transform,把语法错误转成 Jest AssertionError,速度比 ESLint 快 5~10 倍,适合万级模块的大型 monorepo。
- 对于“导出”语义校验(例如组件库禁止导出内部 Hook),可基于 TypeScript Compiler API 写自定义 ESLint rule:遍历 ExportDeclaration,若导出的标识符名以 use 开头且不在白名单,则报 error;再把该规则纳入 jest-runner-eslint,实现“语义级”导出门禁。
- 在微前端场景,主应用通过 importmap 动态加载子应用暴露的组件,若子应用导出语法错误会导致线上 500。可在子应用构建完成后,增加一步 jest-runner-eslint --testPathPattern=dist,对 rollup/vite 产物再做一次“导出”语法兜底,确保上线零语法事故。