From a1cc41b93e841f022940e67a0b863003d5fc3568 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 02:17:29 +0100 Subject: [PATCH 1/3] fix(layout): codemod doc catch-up and regex quote forms (#862) Regex source-text assertions written with a quote class (['"] or ["']) or a \x27/\x22 escape now warn like plain quoted ones; the fts-match assertions in test/db-search-query-bound.test.js were missed before. docs/module-moves.md still described the first-round excerpt warnings. It now matches the scanner: excerpts warn only when their source requires a moved module or a stubbed key, extract* results that reach vm are full loads, skipped path.join segments and regex/template text warn, and an assertion on a moved module's require of a module that stays put does not. --- .ai/contexts/module-moves.md | 8 +++++--- docs/module-moves.md | 26 +++++++++++++++++++------- scripts/move-modules.js | 5 ++++- test/move-modules.test.js | 4 ++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/.ai/contexts/module-moves.md b/.ai/contexts/module-moves.md index 26c3f8f70..9a75a57ac 100644 --- a/.ai/contexts/module-moves.md +++ b/.ai/contexts/module-moves.md @@ -28,9 +28,11 @@ 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 (with escaped punctuation, `['"]`/`["']` +classes and `\x27`/`\x22` escapes decoded to quotes), and cooked template elements containing +quoted moved references warn without changing their embedded assertions. 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..3aab37a1c 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,13 @@ 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. In regex +patterns, escaped punctuation, the quote classes `['"]` and `["']`, and the escapes `\x27` and +`\x22` are read as quotes first, so `/require\(['"]\.\/a['"]\)/` warns. 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..81aba3da7 100644 --- a/scripts/move-modules.js +++ b/scripts/move-modules.js @@ -226,7 +226,10 @@ 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, node.regex.pattern.replace(/\[(?:'"|"')\]|\\x(27|22)|\\([.\/()'"\\])/g, + (match, hex, escaped) => escaped ?? (hex === '22' ? '"' : "'"))); + } 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..6c0c60a09 100644 --- a/test/move-modules.test.js +++ b/test/move-modules.test.js @@ -140,6 +140,10 @@ 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\)/;`], ['template', "const text = `require('./a')`;"], ]) test(`${kind} source assertions warn without changing their text`, () => { const warnings = []; From 2fbaaf35d6fcc7f464808da881ee461cfc382ea0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 02:29:06 +0100 Subject: [PATCH 2/3] fix(layout): tokenise regex patterns before source-text matching (#862) Decoding the pattern text ignored character-class context: a class such as [\x27.\/a\x27] or [^\x27.\/a\x27] matches one character but was read as the literal './a' and warned. Mixed delimiters such as ['"]...\x22 were missed because the two quotes decoded differently and the matcher pairs identical quotes. Regex patterns are now tokenised: every quote form and every class made only of quote forms becomes one canonical quote, escaped non-word characters read as themselves, and any other class or escape is opaque. --- .ai/contexts/module-moves.md | 9 ++++++--- docs/module-moves.md | 11 +++++++--- scripts/move-modules.js | 39 ++++++++++++++++++++++++++++++++---- test/move-modules.test.js | 13 ++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/.ai/contexts/module-moves.md b/.ai/contexts/module-moves.md index 9a75a57ac..c8074fd90 100644 --- a/.ai/contexts/module-moves.md +++ b/.ai/contexts/module-moves.md @@ -28,9 +28,12 @@ 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, `['"]`/`["']` -classes and `\x27`/`\x22` escapes decoded to quotes), and cooked template elements containing -quoted moved references warn without changing their embedded assertions. References resolve +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. diff --git a/docs/module-moves.md b/docs/module-moves.md index 3aab37a1c..b06a9243e 100644 --- a/docs/module-moves.md +++ b/docs/module-moves.md @@ -75,9 +75,14 @@ change; bare filenames remain valid. It does not evaluate code or computed path 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, regex patterns and cooked template elements containing quoted -relative paths to moved modules produce a `WARNING` with the file, line and reference. In regex -patterns, escaped punctuation, the quote classes `['"]` and `["']`, and the escapes `\x27` and -`\x22` are read as quotes first, so `/require\(['"]\.\/a['"]\)/` warns. A reference is resolved +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 diff --git a/scripts/move-modules.js b/scripts/move-modules.js index 81aba3da7..ee2624d69 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)$/i.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,10 +260,7 @@ function rewrite(file, source, map, options = {}) { } }; for (const { node } of tree) { - if (node.regex) { - warnSourceText(node, node.regex.pattern.replace(/\[(?:'"|"')\]|\\x(27|22)|\\([.\/()'"\\])/g, - (match, hex, escaped) => escaped ?? (hex === '22' ? '"' : "'"))); - } + 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 6c0c60a09..8ab463941 100644 --- a/test/move-modules.test.js +++ b/test/move-modules.test.js @@ -144,6 +144,9 @@ for (const [kind, input] of [ ['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 = []; @@ -151,6 +154,16 @@ 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;`], +]) 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 = []; From 362faa176a8868b2be39e3e55f9f3aa7e3502433 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 02:37:21 +0100 Subject: [PATCH 3/3] fix(layout): read only lowercase \x as a hex escape in regex patterns (#862) Uppercase \X is an identity escape, so \X27 is not a quote. --- scripts/move-modules.js | 2 +- test/move-modules.test.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/move-modules.js b/scripts/move-modules.js index ee2624d69..a98e50a44 100644 --- a/scripts/move-modules.js +++ b/scripts/move-modules.js @@ -92,7 +92,7 @@ function excerptOnly(join, tree) { 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)$/i.test(hex)) return { text: "'", quote: true, end: 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 }; diff --git a/test/move-modules.test.js b/test/move-modules.test.js index 8ab463941..113a9b38f 100644 --- a/test/move-modules.test.js +++ b/test/move-modules.test.js @@ -158,6 +158,8 @@ 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);