Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions cli/commands/diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ function diffTests(dir, config = {}) {
// Glob-aware matching (documented entries are often patterns or basenames).
const codeArr = [...codeTests];

// PERFORMANCE OPTIMIZATION: Pre-compile regular expressions to avoid O(N*M)
// instantiation bottlenecks inside the nested .filter and .some loops below.
// PERFORMANCE OPTIMIZATION: Pre-compile regular expressions and basenames to avoid O(N*M)
// instantiation and string processing bottlenecks. We use Sets to track matches in a
// single pass cross-comparison instead of multiple nested .filter and .some loops.
const docMatchers = [...docTests].map(docEntry => {
const entry = String(docEntry).trim();
const hasSlash = entry.includes('/');
Expand All @@ -363,17 +364,30 @@ function diffTests(dir, config = {}) {
};
});

const matches = (matcher, codeRel) => {
const subject = matcher.hasSlash ? codeRel : basename(codeRel);
return matcher.rx.test(subject);
};
const codeItems = codeArr.map(c => ({
original: c,
basename: basename(c)
}));

const matchedDocs = new Set();
const matchedCode = new Set();

for (const m of docMatchers) {
for (const c of codeItems) {
const subject = m.hasSlash ? c.original : c.basename;
if (m.rx.test(subject)) {
matchedDocs.add(m.original);
matchedCode.add(c.original);
}
}
}

return {
title: 'Test Files',
icon: '🧪',
onlyInDocs: docMatchers.filter(m => !codeArr.some(c => matches(m, c))).map(m => m.original),
onlyInCode: codeArr.filter(c => !docMatchers.some(m => matches(m, c))),
matched: docMatchers.filter(m => codeArr.some(c => matches(m, c))).map(m => m.original),
onlyInDocs: docMatchers.filter(m => !matchedDocs.has(m.original)).map(m => m.original),
onlyInCode: codeItems.filter(c => !matchedCode.has(c.original)).map(c => c.original),
matched: docMatchers.filter(m => matchedDocs.has(m.original)).map(m => m.original),
};
}

Expand Down
30 changes: 22 additions & 8 deletions cli/validators/docs-diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ function diffTests(dir, config) {
// Exact-string comparison produced the false "N documented but not found".
const codeArr = [...codeTests];

// PERFORMANCE OPTIMIZATION: Pre-compile regular expressions to avoid O(N*M)
// instantiation bottlenecks inside the nested .filter and .some loops below.
// PERFORMANCE OPTIMIZATION: Pre-compile regular expressions and basenames to avoid O(N*M)
// instantiation and string processing bottlenecks. We use Sets to track matches in a
// single pass cross-comparison instead of multiple nested .filter and .some loops.
const docMatchers = [...docTests].map(docEntry => {
const entry = String(docEntry).trim();
const hasSlash = entry.includes('/');
Expand All @@ -188,15 +189,28 @@ function diffTests(dir, config) {
};
});

const matches = (matcher, codeRel) => {
const subject = matcher.hasSlash ? codeRel : basename(codeRel);
return matcher.rx.test(subject);
};
const codeItems = codeArr.map(c => ({
original: c,
basename: basename(c)
}));

const matchedDocs = new Set();
const matchedCode = new Set();

for (const m of docMatchers) {
for (const c of codeItems) {
const subject = m.hasSlash ? c.original : c.basename;
if (m.rx.test(subject)) {
matchedDocs.add(m.original);
matchedCode.add(c.original);
}
}
}

return {
title: 'Test Files',
onlyInDocs: docMatchers.filter(m => !codeArr.some(c => matches(m, c))).map(m => m.original),
onlyInCode: codeArr.filter(c => !docMatchers.some(m => matches(m, c))),
onlyInDocs: docMatchers.filter(m => !matchedDocs.has(m.original)).map(m => m.original),
onlyInCode: codeItems.filter(c => !matchedCode.has(c.original)).map(c => c.original),
};
}

Expand Down