Skip to content

Commit 7cfdad0

Browse files
committed
chore(gates): commit the daemon residue scan the 11a slices measure with
The review could not find the scan the PR body reported, and the previous ad-hoc copy under-reported edges: its single alternation bridged `import|export … [\s\S]*? from`, so a vi.mock() specifier sitting between an importOriginal type-import and the file's next static from clause was swallowed by the earlier match and never reported. Five mocks of root platform-runtime modules went unnoticed under it. This scan matches every specifier form independently — statement-anchored static import/export, side-effect import, dynamic import()/require(), vi.mock/doMock/unmock/importMock/importActual/importOriginal with string or template-literal arguments, and importOriginal<typeof import()> — and exits 1 on any relative edge leaving src/daemon. Run: node scripts/daemon-edge-residue-scan.mjs. 11b's R11 package-boundary rule (#3546) makes it permanent once the daemon is its own package.
1 parent 7eca0e8 commit 7cfdad0

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Daemon residue scan (11a-4): every module specifier in a file under src/daemon/ that resolves
2+
// to a non-package module outside src/daemon/. The rule: daemon files may import workspace
3+
// packages, externals, and each other — nothing else. Once the daemon is its own package, R11 in
4+
// the layering guard (#3546) enforces this permanently; until then this scan is the measurement.
5+
//
6+
// Why a naive regex reported zero edges while five mocks survived: a single alternation that
7+
// starts at `import|export` and bridges to the next `from` with `[\s\S]*?` swallows everything in
8+
// between — including a `vi.mock('../../../platform-runtime-….ts', …)` whose own specifier sits
9+
// between an earlier `importOriginal<typeof import(...)>` and the file's next static `from`. The
10+
// mocked specifier is then attributed to the wrong (allowed) capture or skipped entirely. So each
11+
// specifier form is matched independently, statement-anchored where the language is:
12+
//
13+
// - static `import`/`export … from '…'` and side-effect `import '…'`, anchored at a statement
14+
// start (line start or `;`), never bridging statements;
15+
// - `import type` / `export type` variants (same anchor);
16+
// - dynamic `import('…')` / `require('…')` with a string or no-substitution template argument;
17+
// - `vi.mock` / `vi.doMock` / `vi.unmock` / `vi.importMock` / `vi.importActual` /
18+
// `vi.importOriginal` first argument, any position, string or template literal;
19+
// - `importOriginal<typeof import('…')>` type-position specifiers (the dynamic-import form).
20+
//
21+
// Known limits: computed specifiers (variables, string concatenation) are out of scope — none
22+
// exist in the daemon tree, and a runtime resolver is the only tool that could see them.
23+
//
24+
// Usage: `node scripts/daemon-edge-residue-scan.mjs` from the repo root. Exit code 1 when any
25+
// edge is found.
26+
import fs from 'node:fs';
27+
import path from 'node:path';
28+
29+
const root = process.cwd();
30+
31+
function walk(dir, out) {
32+
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
33+
const p = path.join(dir, ent.name);
34+
if (ent.isDirectory()) walk(p, out);
35+
else if (/\.(ts|tsx|mjs)$/.test(ent.name)) out.push(p);
36+
}
37+
return out;
38+
}
39+
40+
// A quoted string or no-substitution template literal as the specifier position.
41+
const SPEC = '(?:\'([^\']*)\'|"([^"]*)"|`([^`]*)`)';
42+
const forms = [
43+
// Static import/export with a `from` clause; the keyword must start a statement.
44+
new RegExp(`(?:^|[;\\n])\\s*(?:import|export)\\b[^'"()\`]*?\\bfrom\\s*${SPEC}`, 'gm'),
45+
// Side-effect import.
46+
new RegExp(`(?:^|[;\\n])\\s*import\\s+${SPEC}`, 'gm'),
47+
// Dynamic import (also catches `typeof import('…')` in type positions) and require.
48+
new RegExp(`\\bimport\\s*\\(\\s*${SPEC}`, 'g'),
49+
new RegExp(`\\brequire\\s*\\(\\s*${SPEC}`, 'g'),
50+
// Vitest module-factory hoists, any position in the file.
51+
new RegExp(
52+
`\\bvi\\.(?:mock|doMock|unmock|importMock|importActual|importOriginal)\\s*\\(\\s*${SPEC}`,
53+
'g',
54+
),
55+
];
56+
57+
const files = walk(path.join(root, 'src/daemon'), []);
58+
const edges = [];
59+
for (const file of files) {
60+
const rel = path.relative(root, file);
61+
const source = fs.readFileSync(file, 'utf8');
62+
const seen = new Set();
63+
for (const form of forms) {
64+
form.lastIndex = 0;
65+
let match;
66+
while ((match = form.exec(source))) {
67+
const specifier = match[1] ?? match[2] ?? match[3];
68+
if (!specifier) continue;
69+
if (!specifier.startsWith('.')) continue; // bare specifier: package or external, allowed
70+
const resolved = path
71+
.normalize(path.join(path.dirname(rel), specifier))
72+
.replace(/\.tsx?$/, '');
73+
if (resolved === 'src/daemon' || resolved.startsWith('src/daemon/')) continue;
74+
if (seen.has(resolved)) continue;
75+
seen.add(resolved);
76+
const line = source.slice(0, match.index).split('\n').length;
77+
edges.push(`${rel}:${line} -> ${resolved}`);
78+
}
79+
}
80+
}
81+
edges.sort();
82+
if (edges.length > 0) process.stdout.write(edges.join('\n') + '\n');
83+
process.stdout.write(`daemon residue edges: ${edges.length}\n`);
84+
if (edges.length > 0) process.exit(1);

0 commit comments

Comments
 (0)