Skip to content

Commit 0fc4796

Browse files
test(javascript): read a duplicate-named regular expression by the runtime
Duplicate group names in alternatives compile on newer runtimes only, so whether its `source` folds follows the runtime the test runs on. Claude-Session: https://claude.ai/code/session_017TkBWTk3wcyUcJYDp5ZhaP
1 parent e3f77fe commit 0fc4796

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎test/unitCases/javascript/__snapshots__/printer.unittest.js.snap‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ exports[`JavaScript minifier, pass by pass what the correct and improve phases w
183183

184184
exports[`JavaScript minifier, pass by pass what the correct and improve phases write should write what runs as the source does: a method of a variable holding an array, not a string 1`] = `"!function(){var a=[1,2];console.log(a.join(),a.join(\\"-\\"),a.indexOf(2))}();"`;
185185

186-
exports[`JavaScript minifier, pass by pass what the correct and improve phases write should write what runs as the source does: a regular expression's \`source\` the runtime rejects 1`] = `"try{console.log(/(?<a>x)|(?<a>y)/.source)}catch(e){console.log(e.name)}"`;
187-
188186
exports[`JavaScript minifier, pass by pass what the correct and improve phases write should write what runs as the source does: a remainder of what is no BigInt, its value unread 1`] = `"function f(a,b){return a%b,1}console.log(f(5,2));"`;
189187

190188
exports[`JavaScript minifier, pass by pass what the correct and improve phases write should write what runs as the source does: a sloppy function's \`this\` handed to a call 1`] = `"function F(){g(this),this.y=1}function g(o){o.x=1}console.log((new F).x,3);"`;

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15726,11 +15726,6 @@ const BEHAVIOUR_CASES = [
1572615726
"console.log(Math.ceil(Math.sqrt(2)), Math.floor(Math.max(1, 2)), Math.round(Math.random()) < 2, Math.abs(Math.sign(-3)), Math.max(Math.min('a', 1), 2));",
1572715727
{ compress: {}, mangle: false }
1572815728
],
15729-
[
15730-
"a regular expression's `source` the runtime rejects",
15731-
"try { console.log(/(?<a>x)|(?<a>y)/.source); } catch (e) { console.log(e.name); }",
15732-
{ compress: {}, mangle: false }
15733-
],
1573415729
[
1573515730
"`Math` constants whose digits print longer",
1573615731
"console.log(Math.PI, Math.E, Math.LN2, Number.MAX_VALUE, Number.MIN_VALUE);",
@@ -16266,6 +16261,26 @@ describe("JavaScript minifier, pass by pass", () => {
1626616261
expect(code).toMatchSnapshot();
1626716262
});
1626816263
}
16264+
16265+
it("should read a regular expression's `source` only where the runtime compiles it", async () => {
16266+
const { minify } = await load();
16267+
// Duplicate names in alternatives parse, but only newer runtimes compile them.
16268+
const pattern = "(?<a>x)|(?<a>y)";
16269+
const source = `try { console.log(/${pattern}/.source); } catch (e) { console.log(e.name); }`;
16270+
let compiles = true;
16271+
try {
16272+
new RegExp(pattern);
16273+
} catch (_err) {
16274+
compiles = false;
16275+
}
16276+
const { code } = await minify(source, { compress: {}, mangle: false });
16277+
expect(code).toBe(
16278+
compiles
16279+
? `try{console.log(${JSON.stringify(pattern)})}catch(e){console.log(e.name)}`
16280+
: `try{console.log(/${pattern}/.source)}catch(e){console.log(e.name)}`
16281+
);
16282+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(source));
16283+
});
1626916284
});
1627016285
});
1627116286

0 commit comments

Comments
 (0)