Skip to content

Commit cdd1d8c

Browse files
feat(javascript): call RegExp without new on a string pattern in the minifier (#22547)
`new RegExp("^" + s + "$")` becomes `RegExp("^" + s + "$")`, and so does a `RegExp` given flags sure to be a string, as `new RegExp(s, g ? "g" : "i")`. Called, `RegExp` hands back a regular expression passed without flags as it is; a string pattern, or flags that are a string, make it construct as `new` does. Only a constant pattern or literal flags qualified before. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 2d3f2ca commit cdd1d8c

5 files changed

Lines changed: 18 additions & 11 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19532,11 +19532,13 @@ const installImprove = (modules) => {
1953219532

1953319533
/**
1953419534
* Whether a `new` of a built-in constructs as its call does: always but for
19535-
* `RegExp`, which hands back a regular expression it is passed without flags.
19535+
* `RegExp`, which hands back a regular expression passed without flags, so
19536+
* it needs a constant or string pattern, or flags that are a string.
1953619537
* @param {Node} node a `new` expression
19538+
* @param {CompressorShape} compressor the compressor
1953719539
* @returns {boolean} true when the call constructs the same
1953819540
*/
19539-
const constructsWhenCalled = (node) => {
19541+
const constructsWhenCalled = (node, compressor) => {
1954019542
const { callee } = node;
1954119543
if (
1954219544
!A.isSymbolRefNode(callee) ||
@@ -19551,7 +19553,8 @@ const installImprove = (modules) => {
1955119553
return (
1955219554
!args.some((/** @type {Node} */ arg) => A.isExpansionNode(arg)) &&
1955319555
(constantOf(pattern) !== NOT_CONSTANT ||
19554-
(flags !== undefined && A.isStringNode(flags)))
19556+
A.isString(pattern, compressor) ||
19557+
(flags !== undefined && A.isString(flags, compressor)))
1955519558
);
1955619559
};
1955719560

@@ -19626,7 +19629,10 @@ const installImprove = (modules) => {
1962619629
while (end > read && !A.hasSideEffects(args[end - 1], compressor)) end--;
1962719630
args.length = end;
1962819631
}
19629-
if (!constructsWhenCalled(optimized) || globalMayBeRebound(compressor)) {
19632+
if (
19633+
!constructsWhenCalled(optimized, compressor) ||
19634+
globalMayBeRebound(compressor)
19635+
) {
1963019636
return optimized;
1963119637
}
1963219638
return A.optimize(

‎test/helpers/oxcSmaller.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,9 @@ const OXC_SMALLER_BY_REASON = [
973973
[
974974
"oxc drops `new` before a built-in that constructs the same when called",
975975
[
976-
"swc exec: regexp_1",
977976
"swc minifier: fixture/issues/11829/input.js",
978977
"swc minifier: fixture/issues/12187/input.js",
979978
"swc minifier: fixture/issues/12191/iterator-errors/input.js",
980-
"swc minifier: fixture/projects/backbone/18/input.js",
981979
"swc minifier: fixture/projects/react/3/input.js"
982980
]
983981
],

‎test/helpers/swcSmaller.js‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,10 +520,7 @@ const SWC_SMALLER_BY_REASON = [
520520
],
521521
[
522522
"swc drops `new` before a built-in that constructs the same when called (`Error`, `RegExp`, `Array`)",
523-
[
524-
"swc minifier: fixture/issues/12187/input.js",
525-
"swc minifier: fixture/projects/backbone/18/input.js"
526-
]
523+
["swc minifier: fixture/issues/12187/input.js"]
527524
],
528525
[
529526
"swc drops call and `new` arguments past the parameters of a callee it can see, and an unused parameter whose default is pure",

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,6 @@ for (const [reason, keys] of /** @type {[string, string[]][]} */ ([
15761576
"non-callable-join-string-tag.js (the default minimizer's options)",
15771577
"optional-catch-binding-throws.js (the default minimizer's options)",
15781578
"predicate-throws-then-closing-iterator-also-throws.js (the default minimizer's options)",
1579-
"quantifier-integer-limit.js (the default minimizer's options)",
15801579
"sorting_buffer_access.js (the default minimizer's options)",
15811580
"symbol-tag-override-instances.js (the default minimizer's options)",
15821581
"this-val-regexp.js (the default minimizer's options)",

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ const IMPROVED_CASES = [
534534
'var y = Math.random() < 2 ? "a" : ""; console.log(new RegExp(y, "g").global, new Array(typeof y)[0], new Array(!0).length, new Array(7).length, typeof Object(y));',
535535
{ compress: {}, mangle: false }
536536
],
537+
[
538+
"`RegExp` given a pattern or flags sure to be a string",
539+
'var s = Math.random() < 2 ? "a" : "b", g = Math.random() < 2; console.log(new RegExp("^" + s + "$").test("a"), new RegExp(`${s}+`).source, new RegExp(s, g ? "g" : "i").flags, new RegExp(typeof s).source);',
540+
{ compress: {}, mangle: false },
541+
'var s=Math.random()<2?"a":"b",g=Math.random()<2;console.log(RegExp("^"+s+"$").test("a"),RegExp(`${s}+`).source,RegExp(s,g?"g":"i").flags,RegExp(typeof s).source);'
542+
],
537543
[
538544
"a `+` beside a number literal in arithmetic",
539545
'var d = Math.random() < 2 ? "5" : ""; console.log(1000 * +d, 1 - +d, 5 | +d, 2 ** +d);',
@@ -967,6 +973,7 @@ const KEPT_CASES = [
967973
["`Number.EPSILON`, longer as a number", "console.log(Number.EPSILON);"],
968974
["safe-integer bounds, whose digits gzip worse", "console.log(Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);"],
969975
["a `RegExp` constructed from a regular expression", "var r = /a/g; console.log(new RegExp(r) === r, RegExp(r) === r);"],
976+
["a `RegExp` constructed from what may be a regular expression, with flags that may be missing", "function f(a, b, g) { return [new RegExp(a + b).source, new RegExp(a, g) === a]; } var r = /a/; console.log(f(1, 2), f(r, void 0, void 0));"],
970977
["a spread `Array` call", "var a = [3]; console.log(Array(...a).length);"],
971978
["a shadowed `Array`", "function f(Array) { return new Array(1, 2); } console.log(f(function (a, b) { this.s = a + b; }).s, f(function () { this.s = 0; }).s);"],
972979
["a call of a function returning a short constant or none, called once, marked `@__INLINE__`, or read by a test or an operator", '!function () { function f() { return "a string long enough that a copy costs more than a call"; } function g() { return "ab"; } function k() { return "a string long enough that a copy costs more than a call"; } function n() {} function r() { return; } var id = (x) => x; f(); console.log(/* @__INLINE__ */ f(), f() ? 1 : 2, typeof f(), f().length, f() || 1, !f(), g(), g(), k(), n(), n(), r(), r(), id(), id(void 0)); }();'],

0 commit comments

Comments
 (0)