fix(metrics): enforce exact runtime inventory

This commit is contained in:
Tam Nhu Tran committed 2026-07-26 09:36:00 -04:00
1 parent 426dc541a9
commit 306a8276b3
7 files changed
+258 -160

No files matched your search

+37 -4
View File
@@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const { collectMaintainabilityMetrics } = require('./maintainability-metrics.js');
const { isTestFile } = require('./runtime-source-classifier.js');
const ROOT_DIR = path.resolve(__dirname, '..');
const SRC_DIR = path.join(ROOT_DIR, 'src');
@@ -98,7 +99,7 @@ function isSourceFile(filePath) {
}
function isHotpath(filePath) {
return HOTPATH_PATTERNS.some((pattern) => pattern.test(filePath));
return !isTestFile(filePath) && HOTPATH_PATTERNS.some((pattern) => pattern.test(filePath));
}
function walkFiles(dirPath) {
@@ -450,7 +451,9 @@ function renderMarkdown(report) {
lines.push(`| Sync fs occurrences (all) | ${report.syncFs.totalOccurrences} |`);
lines.push(`| Sync fs files affected (all) | ${report.syncFs.filesAffected} |`);
lines.push(`| Sync fs occurrences (runtime hotpaths) | ${report.syncFs.hotpathOccurrences} |`);
lines.push(`| Sync fs files affected (runtime hotpaths) | ${report.syncFs.hotpathFilesAffected} |`);
lines.push(
`| Sync fs files affected (runtime hotpaths) | ${report.syncFs.hotpathFilesAffected} |`
);
lines.push(`| Legacy shim markers | ${report.legacyShim.totalMarkers} |`);
lines.push(`| Legacy shim files affected | ${report.legacyShim.filesAffected} |`);
lines.push('');
@@ -547,10 +550,39 @@ function renderMarkdown(report) {
function main() {
const report = buildReport();
const jsonContent = JSON.stringify(report, null, 2) + '\n';
const markdownContent = renderMarkdown(report);
const checkOnly = process.argv.includes('--check');
if (checkOnly) {
const staleReports = [
[JSON_REPORT_PATH, jsonContent],
[MD_REPORT_PATH, markdownContent],
].filter(([reportPath, expected]) => {
try {
return fs.readFileSync(reportPath, 'utf8') !== expected;
} catch {
return true;
}
});
if (staleReports.length > 0) {
console.error('[X] Hardening inventory does not match the current source tree:');
for (const [reportPath] of staleReports) {
console.error(` ${relativePath(reportPath)}`);
}
console.error(' Regenerate with: bun run report:hardening');
process.exitCode = 1;
return;
}
console.log('[OK] Hardening inventory matches the current source tree.');
return;
}
fs.mkdirSync(REPORT_DIR, { recursive: true });
fs.writeFileSync(JSON_REPORT_PATH, JSON.stringify(report, null, 2) + '\n', 'utf8');
fs.writeFileSync(MD_REPORT_PATH, renderMarkdown(report), 'utf8');
fs.writeFileSync(JSON_REPORT_PATH, jsonContent, 'utf8');
fs.writeFileSync(MD_REPORT_PATH, markdownContent, 'utf8');
const relJson = relativePath(JSON_REPORT_PATH);
const relMd = relativePath(MD_REPORT_PATH);
@@ -575,6 +607,7 @@ function main() {
module.exports = {
buildReport,
collectSyncCallSites,
isTestFile,
renderMarkdown,
stripComments,
};
+2 -8
View File
@@ -24,6 +24,7 @@
const fs = require('fs');
const path = require('path');
const { isTestFile } = require('./runtime-source-classifier.js');
const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs']);
@@ -79,14 +80,6 @@ function isSourceFile(filePath) {
return SOURCE_EXTENSIONS.has(path.extname(filePath));
}
function isTestFile(relPath) {
return (
/(?:^|\/)(?:__tests__|tests?)\//.test(relPath) ||
/\.test\./.test(relPath) ||
/\.spec\./.test(relPath)
);
}
function isCliUxExempt(relPath) {
return CLI_UX_EXEMPT_PREFIXES.some(function (prefix) {
return relPath.startsWith(prefix);
@@ -350,6 +343,7 @@ module.exports = {
classifyThrows: classifyThrows,
countConsoleErrors: countConsoleErrors,
hasCreateLogger: hasCreateLogger,
isTestFile: isTestFile,
countLoc: countLoc,
TYPED_ERROR_CLASSES: TYPED_ERROR_CLASSES,
TYPED_ADOPTION_SUBDOMAINS: TYPED_ADOPTION_SUBDOMAINS,
+12
View File
@@ -0,0 +1,12 @@
function isTestFile(filePath) {
const normalizedPath = filePath.replace(/\\/g, '/');
return (
/(^|\/)(__tests__|__mocks__|fixtures)(\/|$)/.test(normalizedPath) ||
/(^|\/)(tests?|test-fixtures)(\/|$)/.test(normalizedPath) ||
/\.(test|spec)\.[^.]+$/.test(normalizedPath)
);
}
module.exports = {
isTestFile,
};