Skip to content

引入

ts
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-

参数

示例

ts
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

示例

ts
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.privateTokenGit API 私有令牌,用于操作 MR
options.gitApiPrefixGit API 地址前缀
options.workspace项目工作目录绝对路径
options.mrUrlMR 页面链接,用于消息通知中展示
options.mrIdMR 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.mrTitlePrefixautoFixMR 创建 MR 时的标题前缀,如 '[WIP]' 可防止被自动合入
options.onReport上报回调,lint 完成后调用,传入错误数量等关键信息

返回: 返回 fileMap,包含各文件类型的 lint 结果

示例

ts
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

NameTypeDescription
totalErrorsnumber各文件类型的错误总数
errorDetailsArray<object>各文件类型的错误详情
errorDetails[].fileTypestring
errorDetails[].errorCountnumber
errorDetails[].fileCountnumber
checkAllboolean是否全量检查
[mrId]stringMR ID
repostring仓库名
[sourceBranch]string源分支
[targetBranch]string目标分支
passedboolean是否通过(无错误)
durationnumberlint 执行耗时(ms)
lintFilesArray检查的文件类型列表
fileMapFileMap完整的 fileMap 结果
[fixMRList]Array<object>自动修复创建的 MR 信息列表(autoFixMR 开启时才有,eslint 和 stylelint 分别创建独立 MR)
fixMRList[].fixBranchstring修复分支名
fixMRList[].fixTargetBranchstring修复 MR 目标分支
[fixMRList[].mrData]anycreateMR 返回的原始数据