重构:完成架构一致性整改与生命周期收口

This commit is contained in:
Christ committed 2026-08-18 14:05:08 +08:00
1 parent 55904cc409
commit 9eb6be2875
344 files changed
+15961 -13926

No files matched your search

+56 -41
View File
@@ -7,18 +7,26 @@ import ts from "typescript";
// HAN_PATTERN 用于识别至少包含一个汉字的注释文本。
const HAN_PATTERN = /\p{Script=Han}/u;
// TEMPLATE_COMMENT_PATTERNS 是阶段 10 禁止继续保留的机械注释句式。
const TEMPLATE_COMMENT_PATTERNS = [
{ name: '保存当前处理流程', expression: /保存.{0,80}供当前处理流程使用/u },
{ name: '负责相关处理', expression: /负责.{0,80}相关处理/u },
{ name: '泛化回调职责', expression: /回调函数负责当前业务流程/u },
{ name: '泛化错误说明', expression: /表示错误/u },
{ name: '泛化数量说明', expression: /表示数量/u },
];
// parseArguments 解析注释门禁所需的少量命令行参数。
function parseArguments(argv) {
// options 保存命令行参数及其默认值。
const options = {
mode: "check",
root: ".",
baseline: ".commentlint/frontend-baseline.json",
};
for (let index = 0; index < argv.length; index += 1) {
// argument 是当前处理的命令行参数。
const argument = argv[index];
if (argument === "--mode" || argument === "--root" || argument === "--baseline") {
if (argument === "--mode" || argument === "--root") {
// value 是当前选项紧随其后的值。
const value = argv[index + 1];
if (!value) {
@@ -28,12 +36,35 @@ function parseArguments(argv) {
index += 1;
}
}
if (options.mode !== "check" && options.mode !== "baseline") {
if (options.mode !== "check" && options.mode !== "template-audit") {
throw new Error(`不支持的模式:${options.mode}`);
}
return options;
}
// collectTemplateFindings 逐行收集 TypeScript/TSX 中不具备实际语义的历史模板注释。
function collectTemplateFindings(rootDirectory) {
// findings 保存模板注释的相对路径、行号与命中规则,便于阶段 10 按文件替换。
const findings = [];
// filePath 是当前待审计的 TypeScript 或 TSX 源码文件。
for (const filePath of collectSourceFiles(rootDirectory)) {
// relativePath 是跨平台稳定的源码相对路径。
const relativePath = path.relative(rootDirectory, filePath).split(path.sep).join("/");
// lines 是保留原始行号的源码文本行集合。
const lines = fs.readFileSync(filePath, "utf8").split(/\r?\n/u);
// sourceLine、lineIndex 是当前待匹配的源码文本及其零基行号。
for (const [lineIndex, sourceLine] of lines.entries()) {
// pattern 是当前禁止模板句式的审计规则。
for (const pattern of TEMPLATE_COMMENT_PATTERNS) {
if (pattern.expression.test(sourceLine)) {
findings.push({ file: relativePath, line: lineIndex + 1, kind: pattern.name, name: "template-comment" });
}
}
}
}
return sortFindings(findings);
}
// collectSourceFiles 递归找到需要检查的 TypeScript 和 TSX 源文件。
function collectSourceFiles(rootDirectory) {
// sourceFiles 保存按路径排序后的源码文件。
@@ -131,7 +162,7 @@ function getNodeName(node) {
return "anonymous";
}
// findingFor 将一个 AST 节点转换成稳定的基线记录。
// findingFor 将一个 AST 节点转换成稳定的门禁记录。
function findingFor(relativePath, sourceFile, node, kind) {
// line 是声明所在的一基行号。
const line = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
@@ -144,7 +175,7 @@ function inspectSourceFile(rootDirectory, filePath) {
const sourceText = fs.readFileSync(filePath, "utf8");
// sourceFile 是开启父节点信息的 TypeScript AST。
const sourceFile = ts.createSourceFile(filePath, sourceText, ts.ScriptTarget.Latest, true, filePath.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
// relativePath 使基线在不同机器上保持可复用。
// relativePath 使门禁输出在不同机器上保持稳定。
const relativePath = path.relative(rootDirectory, filePath).split(path.sep).join("/");
// findings 保存当前文件的注释问题。
const findings = [];
@@ -167,7 +198,7 @@ function inspectSourceFile(rootDirectory, filePath) {
return findings;
}
// findingKey 生成历史基线中的稳定键。
// findingKey 生成排序和输出使用的稳定键。
function findingKey(finding) {
return `${finding.file}:${finding.line}:${finding.kind}:${finding.name}`;
}
@@ -177,51 +208,35 @@ function sortFindings(findings) {
return findings.sort((left, right) => findingKey(left).localeCompare(findingKey(right)));
}
// writeBaseline 写入缩进格式的前端历史问题基线。
function writeBaseline(filePath, findings) {
// parentDirectory 是基线文件所属目录。
const parentDirectory = path.dirname(filePath);
fs.mkdirSync(parentDirectory, { recursive: true });
fs.writeFileSync(filePath, `${JSON.stringify(findings, null, 2)}\n`, "utf8");
}
// readBaseline 读取历史问题并建立快速查询集合。
function readBaseline(filePath) {
// baselineMissing 表示最终严格门禁已删除历史基线文件,此时必须按空集合执行。
if (!fs.existsSync(filePath)) return new Set();
// baselineText 是基线文件的 JSON 文本。
const baselineText = fs.readFileSync(filePath, "utf8");
// baselineFindings 是反序列化后的历史问题数组。
const baselineFindings = JSON.parse(baselineText);
return new Set(baselineFindings.map(findingKey));
}
// main 执行前端源码扫描、基线生成或新增问题检查。
// main 执行前端源码严格注释检查或输出模板注释审计结果。
function main() {
// options 保存解析后的命令行选项。
const options = parseArguments(process.argv.slice(2));
// rootDirectory 是规范化后的源码根目录。
const rootDirectory = path.resolve(options.root);
// baselinePath 是规范化后的历史基线路径。
const baselinePath = path.resolve(options.baseline);
// findings 是当前所有 TypeScript/TSX 文件的检查结果。
const findings = sortFindings(collectSourceFiles(rootDirectory).flatMap((filePath) => inspectSourceFile(rootDirectory, filePath)));
if (options.mode === "baseline") {
writeBaseline(baselinePath, findings);
console.log(`commentlint: 已记录 ${findings.length} 个历史问题到 ${options.baseline}`);
if (options.mode === "template-audit") {
// templateFindings 是按稳定顺序排列的前端模板注释记录。
const templateFindings = collectTemplateFindings(rootDirectory);
for (const finding of templateFindings) {
console.log(`${finding.file}:${finding.line}: [${finding.kind}] 模板化注释`);
}
console.log(`commentlint: 发现 ${templateFindings.length} 条模板化注释`);
return;
}
// baselineKeys 保存允许继续存在的历史问题。
const baselineKeys = readBaseline(baselinePath);
// newFindings 只包含本次新增或位置变化的问题。
const newFindings = findings.filter((finding) => !baselineKeys.has(findingKey(finding)));
for (const finding of newFindings) {
// findings 是当前所有 TypeScript/TSX 文件的检查结果。
const findings = sortFindings(collectSourceFiles(rootDirectory).flatMap((filePath) => inspectSourceFile(rootDirectory, filePath)));
for (const finding of findings) {
console.log(`${finding.file}:${finding.line}: [${finding.kind}] ${finding.name} 缺少中文注释`);
}
if (newFindings.length > 0) {
throw new Error(`发现 ${newFindings.length} 个新增问题`);
// templateFindings 是阶段 10 不允许保留的机械注释位置。
const templateFindings = collectTemplateFindings(rootDirectory);
for (const finding of templateFindings) {
console.log(`${finding.file}:${finding.line}: [${finding.kind}] 模板化注释`);
}
console.log(`commentlint: 通过(扫描 ${findings.length} 个历史问题)`);
if (findings.length > 0 || templateFindings.length > 0) {
throw new Error(`发现 ${findings.length} 个缺少中文注释项和 ${templateFindings.length} 条模板化注释`);
}
console.log("commentlint: 通过(无缺少中文注释或模板化注释)");
}
try {