diff --git a/.ai/contexts/module-moves.md b/.ai/contexts/module-moves.md index 26c3f8f70..c8074fd90 100644 --- a/.ai/contexts/module-moves.md +++ b/.ai/contexts/module-moves.md @@ -28,9 +28,14 @@ to `slice` or `substring` preserves a full load; unknown `extract*` helpers rema unless their result is demonstrably excerpted at the call site. Full evaluations and unknown uses keep conservative ambiguity checking. This is syntactic inference, not lexical scope or interprocedural analysis; computed loaders and separate VM -contexts still need review. Strings, regex patterns (with escaped punctuation decoded), and -cooked template elements containing quoted moved references warn without changing their -embedded assertions. +contexts still need review. Strings, regex patterns and cooked template elements containing +quoted moved references warn without changing their embedded assertions. Regex patterns are +tokenised (`regexText`): `'`, `"`, `\'`, `\"`, `\x27`, `\x22` and classes made only of those +forms become one canonical quote, so mixed delimiters still pair; escaped non-word characters +read as themselves. Other classes (ranges, negation, any non-quote member) and other escapes +(`\s`, `\\`, `'`) are opaque and contribute no literal text. References resolve +from the test and the root only, so an assertion on a moved module's require of a module that +stays put does not warn. `test/move-modules.test.js` uses synthetic sources and disposable fixture trees so a migration cannot rewrite its own fixture maps into identity maps. Run it after repeated apply/check in diff --git a/docs/module-moves.md b/docs/module-moves.md index ddab1880c..b06a9243e 100644 --- a/docs/module-moves.md +++ b/docs/module-moves.md @@ -49,12 +49,19 @@ VM stub keys/comparisons inferred from the source files the test loads. Only stu actually present in the test participate in ambiguity checks. Loaded source requires are resolved from their current location once rewritten, so repeated apply/check runs preserve the stubs. Shared stub keys must agree across loaded sources, including sources that keep their original location. -Sources read only for `indexOf`/`slice`/`extract*` excerpts produce a `WARNING` and do not -contribute their whole module's requires to stub ambiguity checks. The excerpt and its stubs -still need manual review. A full source evaluation keeps participating even when the same text -is also sliced. Unknown source uses remain conservative; separate VM contexts can still require -manual ambiguity resolution. +Source reads associated with static joins are followed through local variable declarations. +Text consumed only by `indexOf` or partial slicing is an excerpt: it does not contribute its +whole module's requires to stub ambiguity checks, and it produces a `WARNING` only when that +source requires a moved module or a key stubbed by the test, resolving rewritten requires from +the source's current location. The excerpt and its stubs then need manual review. A sole +literal-zero argument to `slice` or `substring` preserves a full load, and so does an `extract*` +helper whose result reaches `vm` or any other use: it is an excerpt only when its result is +itself demonstrably excerpted at the call site. A full source evaluation keeps participating +even when the same text is also sliced. Unknown source uses remain conservative; this is +syntactic inference, not lexical scope or interprocedural analysis, and separate VM contexts can +still require manual ambiguity resolution. Non-first path segments in `path.join` are never rewritten as standalone module filenames. +A skipped segment that names a moved key or basename produces a `WARNING` with its file and line. Directory constants such as `PUBLIC_DIR`, and root/dirname joins resolving to an unmapped subdirectory module with a moved basename (for example `public/activity-trace.js`), remain unchanged and produce a `WARNING` when the consuming module stays in place. In a moved module, @@ -67,8 +74,18 @@ Qualified documentation paths change; bare filenames remain valid. It does not evaluate code or computed path expressions. Review dynamic paths manually, especially interpolated worker paths and source strings inside tests. Source-text assertions such as `"} = require('./db');"` are not parsed as executable requires and -remain unchanged; string literals containing quoted relative paths to moved modules produce a -`WARNING` with the file, line and reference. Review and update them manually after a move. Helper loaders such as +remain unchanged; string literals, regex patterns and cooked template elements containing quoted +relative paths to moved modules produce a `WARNING` with the file, line and reference. A regex +pattern is tokenised before matching. Every quote form — `'`, `"`, `\'`, `\"`, `\x27`, `\x22`, +and a character class whose members are all such forms (`['"]`, `["']`, `[']`, `[\x22]`) — +becomes one canonical quote, so mixed delimiters such as `/require\(['"]\.\/a\x22\)/` warn. +Escaped non-word characters (`\.`, `\/`, `\-`) read as themselves. Any other character class +(a range, a negation, or any member that is not a quote) and any other escape (`\s`, `\d`, +`\\`, `'`) are opaque: they never contribute literal text, so `/^[\x27.\/a\x27]$/` does +not warn. A reference is resolved +from the test and from the root, never from the module whose text it inspects: a test asserting +that a moved module requires a module that stays put gets no warning, though the relative path +in that assertion changes with the move. Review and update them manually after a move. Helper loaders such as `loadShipped(filename)` with `path.join(__dirname, '..', filename)` do not associate a loaded source with its VM stubs. If no static loaded application source is detected and a stub-shaped literal matches a moved module or one of its relative dependencies, both apply and check print an explicit `WARNING` listing diff --git a/scripts/move-modules.js b/scripts/move-modules.js index 469f59446..a98e50a44 100644 --- a/scripts/move-modules.js +++ b/scripts/move-modules.js @@ -89,6 +89,40 @@ function excerptOnly(join, tree) { return reads.length > 0 && reads.every(extracted); } +function regexAtom(pattern, i) { + if (pattern[i] !== '\\') return { text: pattern[i], quote: /['"]/.test(pattern[i]), end: i + 1 }; + const hex = pattern.slice(i + 1, i + 4); + if (/^x(27|22)$/.test(hex)) return { text: "'", quote: true, end: i + 4 }; + const escaped = pattern[i + 1] ?? ''; + if (/['"]/.test(escaped)) return { text: "'", quote: true, end: i + 2 }; + if (/^[^\w]$/.test(escaped)) return { text: escaped, quote: false, end: i + 2 }; + return { text: ' ', quote: false, end: i + 2 }; +} + +function regexText(pattern) { + let text = ''; + for (let i = 0; i < pattern.length;) { + if (pattern[i] !== '[') { + const atom = regexAtom(pattern, i); + text += atom.quote ? "'" : atom.text; + i = atom.end; + continue; + } + let j = i + 1; + let quotes = true; + const members = []; + while (j < pattern.length && pattern[j] !== ']') { + const atom = regexAtom(pattern, j); + if (!atom.quote) quotes = false; + members.push(atom); + j = atom.end; + } + text += quotes && members.length ? "'" : ' '; + i = j + 1; + } + return text; +} + function rewrite(file, source, map, options = {}) { map = validateMap(map); const edits = []; @@ -226,7 +260,7 @@ function rewrite(file, source, map, options = {}) { } }; for (const { node } of tree) { - if (node.regex) warnSourceText(node, node.regex.pattern.replace(/\\([.\/()'"])/g, '$1')); + if (node.regex) warnSourceText(node, regexText(node.regex.pattern)); if (node.type === 'TemplateElement') warnSourceText(node, node.value.cooked ?? node.value.raw); if (node.type !== 'Literal' || typeof node.value !== 'string' || handled.has(node)) continue; const value = node.value; diff --git a/test/move-modules.test.js b/test/move-modules.test.js index 265b93f1c..113a9b38f 100644 --- a/test/move-modules.test.js +++ b/test/move-modules.test.js @@ -140,6 +140,13 @@ test('prototype-named literals stay byte-identical with a nonempty move map', () for (const [kind, input] of [ ['regex', String.raw`const re = /require\('\.\/a'\)/;`], + ['regex single-double quote class', String.raw`const re = /require\(['"]\.\/a['"]\)/;`], + ['regex double-single quote class', String.raw`const re = /require\(["']\.\/a["']\)/;`], + ['regex hex single quote', String.raw`const re = /require\(\x27\.\/a\x27\)/;`], + ['regex hex double quote', String.raw`const re = /require\(\x22\.\/a\x22\)/;`], + ['regex quote class closed by hex double quote', String.raw`const re = /require\(['"]\.\/a\x22\)/u;`], + ['regex hex single quote closed by double quote', String.raw`const re = /require\(\x27\.\/a"\)/u;`], + ['regex double quote closed by hex single quote', String.raw`const re = /require\("\.\/a\x27\)/u;`], ['template', "const text = `require('./a')`;"], ]) test(`${kind} source assertions warn without changing their text`, () => { const warnings = []; @@ -147,6 +154,18 @@ for (const [kind, input] of [ assert.ok(warnings.some(w => /:1: source-text reference \.\/a;/.test(w)), warnings.join('\n')); }); +for (const [kind, input] of [ + ['class of quotes and path characters', String.raw`const re = /^[\x27.\/a\x27]$/u;`], + ['class with a range', String.raw`const re = /^[a-z\x27.\/a\x27]$/u;`], + ['negated class', String.raw`const re = /^[^\x27.\/a\x27]$/u;`], + ['uppercase X identity escape', String.raw`const re = /"\.\/a\X27/;`], + ['uppercase X inside a class', String.raw`const re = /[\X27]\.\/a"/;`], +]) test(`regex ${kind} is opaque and does not warn`, () => { + const warnings = []; + assert.equal(rewrite('test/assertions.test.js', input, map, { onWarning: w => warnings.push(w) }), input); + assert.deepEqual(warnings, []); +}); + test('unsupported path.join anchors warn for moved keys and basenames only', () => { const input = "path.join(root, 'a.js');\npath.join(base, 'workers', 'child.js');\npath.join(base, 'other.js');"; const warnings = [];