引入
import {
GIT_ADD_EXCLUDE_PATTERNS,
appendIgnoreIfNeeded,
removeParserOptionsProject,
genFixBranchName,
gitAddExcludingLintOutputs,
commitFixAndCreateMR,
autoFixAndCreateMR,
checkLint,
CheckLintReportInfo
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
GIT_ADD_EXCLUDE_PATTERNS,
appendIgnoreIfNeeded,
removeParserOptionsProject,
genFixBranchName,
gitAddExcludingLintOutputs,
commitFixAndCreateMR,
autoFixAndCreateMR,
checkLint,
CheckLintReportInfo
} from 't-comm/lib/lint/index';
// 只支持 ESM 的项目
import {
GIT_ADD_EXCLUDE_PATTERNS,
appendIgnoreIfNeeded,
removeParserOptionsProject,
genFixBranchName,
gitAddExcludingLintOutputs,
commitFixAndCreateMR,
autoFixAndCreateMR,
checkLint,
CheckLintReportInfo
} from 't-comm/es/lint/index';GIT_ADD_EXCLUDE_PATTERNS
描述:应在 git add 时排除的文件模式:
- lint 产出的 JSON 报告文件
- ignoreSubmodule 可能修改的 ignore 文件
参数:
appendIgnoreIfNeeded()
描述:检查 ignore 文件是否已包含 submodule 忽略内容, 只有不存在或未包含时才追加,避免重复写入
参数:
removeParserOptionsProject()
描述:临时移除 .eslintrc.js 中的 parserOptions.project 配置。 背景:lint vue 文件时,如果 tsconfig.json 的 include 不包含 .vue,
Typescript-eslint/parser: 会抛 parsing error 导致整个 lint 流程中断; 但对于 ts/tsx 文件,parserOptions.project 是开启类型感知规则的关键配置, 不能永久删除。因此本函数返回一个 restore 函数,调用方应在 lint 完成后 (建议放在 finally 中)调用它来还原配置文件。
同时注册了进程退出钩子,作为异常崩溃时的兜底恢复。
参数:
返回: restore 函数,用于恢复 .eslintrc.js 原始内容;若未发生修改则为 no-op
genFixBranchName()
描述:生成修复分支名:feature/fix-eslint-{yyyyMMddhhmmss} 或 feature/fix-stylelint-
参数:
示例
genFixBranchName('eslint', new Date('2025-01-02 03:04:05').getTime());
// 'feature/fix-eslint-20250102030405'
genFixBranchName('stylelint');
// 'feature/fix-stylelint-{当前时间}'gitAddExcludingLintOutputs()
描述:在 fix 分支上执行 git add,排除 lint 产出文件和 ignore 文件变更
参数:
commitFixAndCreateMR()
描述:创建一个 fix 分支、提交并推送,然后创建 MR
参数:
返回: MR 信息,失败时返回 undefined
autoFixAndCreateMR()
描述:自动执行 --fix 并创建修复 MR eslint 和 stylelint 分别创建独立 MR,便于 review
参数:
返回: 创建的修复 MR 信息列表,失败时返回 undefined
示例
const fixMRList = await autoFixAndCreateMR({
fileMap,
workspace: '/path/to/repo',
sourceBranch: 'feature/foo',
targetBranch: 'master',
repo: 'group/project',
privateToken: 'xxx',
mrTitlePrefix: '[WIP]',
});checkLint(options)
描述:执行代码 lint 检查(支持 ESLint 和 StyleLint),并将结果通知到企业微信群、MR 评论等。 支持增量模式(仅检查 sourceBranch 与 targetBranch 之间的 diff 文件)和全量模式(checkAll=true)。 检查完成后可自动执行 --fix 并创建修复 MR(autoFixMR=true)。
Throws:
- 当 throwError 为 true 且存在 lint 错误时抛出异常
参数:
| 参数名 | 描述 |
|---|---|
| options | 配置参数 |
| options.privateToken | Git API 私有令牌,用于操作 MR |
| options.gitApiPrefix | Git API 地址前缀 |
| options.workspace | 项目工作目录绝对路径 |
| options.mrUrl | MR 页面链接,用于消息通知中展示 |
| options.mrId | MR ID,传入后会在 MR 中添加评论和逐行批注 |
| options.buildUrl | 流水线构建链接,用于消息通知中展示 |
| options.repo | 仓库名称(如 group/project) |
| options.repoUrl | 仓库页面链接 |
| options.sourceBranch | 源分支名(增量模式必填) |
| options.targetBranch | 目标分支名(增量模式必填) |
| options.docLink | 说明文档链接,用于消息通知中展示 |
| options.webhookUrl | 企业微信机器人 Webhook 地址 |
| options.chatId | 企业微信群聊 ID 列表,默认 ['ALL'] |
| options.checkAll | 是否全量检查,默认 false(增量模式) |
| options.mentionList | 需要 @ 的企业微信用户列表 |
| options.lintFiles | 需要检查的文件类型列表,默认检查所有配置的类型 |
| options.throwError | 检查不通过时是否抛出异常,默认 true |
| options.ignoreSubmodules | 是否在 lint 时忽略 git submodule,默认 true |
| options.autoFixMR | 是否自动执行 --fix 并创建修复 MR,默认 false |
| options.mrTitlePrefix | autoFixMR 创建 MR 时的标题前缀,如 '[WIP]' 可防止被自动合入 |
| options.onReport | 上报回调,lint 完成后调用,传入错误数量等关键信息 |
返回: 返回 fileMap,包含各文件类型的 lint 结果
示例
import { checkLint } from 't-comm';
await checkLint({
privateToken: 'your-token',
gitApiPrefix: 'https://git.woa.com/api/v3',
workspace: '/path/to/project',
buildUrl: 'https://ci.example.com/build/123',
repo: 'group/project',
sourceBranch: 'feature/xxx',
targetBranch: 'master',
docLink: 'https://doc.example.com/lint',
webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx',
});CheckLintReportInfo
描述:checkLint 完成后传给 onReport 回调的上报信息
参数:
Properties
| Name | Type | Description |
|---|---|---|
| totalErrors | number | 各文件类型的错误总数 |
| errorDetails | Array<object> | 各文件类型的错误详情 |
| errorDetails[].fileType | string | |
| errorDetails[].errorCount | number | |
| errorDetails[].fileCount | number | |
| checkAll | boolean | 是否全量检查 |
| [mrId] | string | MR ID |
| repo | string | 仓库名 |
| [sourceBranch] | string | 源分支 |
| [targetBranch] | string | 目标分支 |
| passed | boolean | 是否通过(无错误) |
| duration | number | lint 执行耗时(ms) |
| lintFiles | Array | 检查的文件类型列表 |
| fileMap | FileMap | 完整的 fileMap 结果 |
| [fixMRList] | Array<object> | 自动修复创建的 MR 信息列表(autoFixMR 开启时才有,eslint 和 stylelint 分别创建独立 MR) |
| fixMRList[].fixBranch | string | 修复分支名 |
| fixMRList[].fixTargetBranch | string | 修复 MR 目标分支 |
| [fixMRList[].mrData] | any | createMR 返回的原始数据 |