mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:40:43 +00:00
77 lines
2.1 KiB
JavaScript
77 lines
2.1 KiB
JavaScript
import { promises as fs } from "node:fs";
|
|
import path from "node:path";
|
|
import {
|
|
collectTypeScriptFilesFromRoots,
|
|
resolveRepoRoot,
|
|
resolveSourceRoots,
|
|
} from "./ts-guard-utils.mjs";
|
|
|
|
function normalizeViolation(rawViolation, relPath) {
|
|
if (typeof rawViolation === "number") {
|
|
return {
|
|
line: rawViolation,
|
|
callsite: `${relPath}:${rawViolation}`,
|
|
};
|
|
}
|
|
return {
|
|
...rawViolation,
|
|
callsite: rawViolation.callsite ?? `${relPath}:${rawViolation.line}`,
|
|
};
|
|
}
|
|
|
|
export async function runCallsiteGuard(params) {
|
|
const repoRoot = resolveRepoRoot(params.importMetaUrl);
|
|
const sourceRoots = resolveSourceRoots(repoRoot, params.sourceRoots);
|
|
const files = await collectTypeScriptFilesFromRoots(sourceRoots, {
|
|
extraTestSuffixes: params.extraTestSuffixes,
|
|
});
|
|
const violations = [];
|
|
|
|
for (const filePath of files) {
|
|
const relPath = path.relative(repoRoot, filePath).replaceAll(path.sep, "/");
|
|
if (params.skipRelativePath?.(relPath)) {
|
|
continue;
|
|
}
|
|
const content = await fs.readFile(filePath, "utf8");
|
|
const rawViolations = params.findCallViolations
|
|
? params.findCallViolations(content, filePath)
|
|
: params.findCallLines(content, filePath);
|
|
for (const rawViolation of rawViolations) {
|
|
const violation = normalizeViolation(rawViolation, relPath);
|
|
if (
|
|
params.allowViolation?.({
|
|
...violation,
|
|
relativePath: relPath,
|
|
filePath,
|
|
}) ??
|
|
params.allowCallsite?.(violation.callsite, violation)
|
|
) {
|
|
continue;
|
|
}
|
|
violations.push(
|
|
params.formatViolation
|
|
? params.formatViolation({
|
|
...violation,
|
|
relativePath: relPath,
|
|
filePath,
|
|
})
|
|
: violation.callsite,
|
|
);
|
|
}
|
|
}
|
|
|
|
if (violations.length === 0) {
|
|
return;
|
|
}
|
|
|
|
console.error(params.header);
|
|
const output = params.sortViolations === false ? violations : violations.toSorted();
|
|
for (const violation of output) {
|
|
console.error(`- ${violation}`);
|
|
}
|
|
if (params.footer) {
|
|
console.error(params.footer);
|
|
}
|
|
process.exit(1);
|
|
}
|