Skip to content

Commit eb3318a

Browse files
fix(minify): keep a constructor's this.x read a getter may answer
Reading `this` in a constructor as never throwing let an unused `this.x` drop, losing a getter's effect; terser keeps it too. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent d647dab commit eb3318a

3 files changed

Lines changed: 16 additions & 47 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19768,41 +19768,6 @@ const installCorrect = (modules) => {
1976819768
}
1976919769
return false;
1977019770
};
19771-
/**
19772-
* Whether `this` where the compressor is is the object a constructor makes:
19773-
* one with no heritage, or a derived one's once `super()` has run.
19774-
* @param {Compressor} compressor the compressor
19775-
* @returns {boolean} true there
19776-
*/
19777-
const thisIsConstructed = (compressor) => {
19778-
let parent;
19779-
let viaArrow = false;
19780-
for (let i = 0; (parent = compressor.parent(i)); i++) {
19781-
if (A.isArrowNode(parent)) viaArrow = true;
19782-
if (!A.isLambdaNode(parent) || A.isArrowNode(parent)) continue;
19783-
const method = compressor.parent(i + 1);
19784-
if (!method || method.kind !== "constructor") return false;
19785-
for (let j = i + 2; (parent = compressor.parent(j)); j++) {
19786-
if (A.isClassNode(parent)) {
19787-
return (
19788-
!parent.superClass ||
19789-
(!viaArrow && superCalledBefore(compressor, i))
19790-
);
19791-
}
19792-
}
19793-
return false;
19794-
}
19795-
return false;
19796-
};
19797-
const dotThrowsOffThis = ast.dotThrows;
19798-
// A constructor's `this` is an object, never `undefined` a property read throws on.
19799-
ast.dotThrows = (node, compressor) =>
19800-
!(
19801-
A.isThisNode(node) &&
19802-
modules.improvements !== undefined &&
19803-
modules.improvements.enabled &&
19804-
thisIsConstructed(compressor)
19805-
) && dotThrowsOffThis(node, compressor);
1980619771
// terser reads `this` as pure, where a derived constructor's throws early.
1980719772
for (const wrapAnswer of [ast.wrapHasSideEffects, ast.wrapMayThrow]) {
1980819773
wrapAnswer(

‎test/helpers/oxcSmaller.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ const OXC_SMALLER_BY_REASON = [
341341
[
342342
"oxc writes conditions shorter, as oxc's `peephole/minimize_conditions.rs` tests show",
343343
[
344+
"oxc minifier: peephole/minimize_conditions.rs:1053",
344345
"oxc minifier: peephole/minimize_conditions.rs:1066",
345346
"oxc minifier: peephole/minimize_conditions.rs:1204",
346347
"oxc minifier: peephole/minimize_conditions.rs:1224",

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9079,17 +9079,13 @@ describe("syntax-printer", () => {
90799079
}
90809080
});
90819081

9082-
it("should write what oxc writes shorter: a catch binding, a nested `if`, a wide name, a loop update and a constructor's `this`", async () => {
9082+
it("should write what oxc writes shorter: a catch binding, a nested `if`, a wide name and a loop update", async () => {
90839083
const { minify } = await load();
90849084
for (const [input, expected] of [
90859085
["try { f(); } catch (e) { g(); }", "catch{g()}"],
90869086
["function _() { if (a) return; if (b) return c; } _();", "if(!a&&b)return c"],
90879087
['a["ÑStuffAfter"];', "a.ÑStuffAfter"],
9088-
["for (var i = 0; i < 10; i++, 0) foo(i);", "i<10;i++)"],
9089-
[
9090-
"class B extends A { constructor() { super(); if (x) this.b = 0; else this.b = 1; } }",
9091-
"super(),this.b=+!x"
9092-
]
9088+
["for (var i = 0; i < 10; i++, 0) foo(i);", "i<10;i++)"]
90939089
]) {
90949090
const { code } = await minify(input, {
90959091
compress: { passes: 2 },
@@ -9103,12 +9099,7 @@ describe("syntax-printer", () => {
91039099
["try { f(); } catch (e) { g(); }", "catch{", { ecma: 2015 }],
91049100
["try { f(); } catch (e) { var e = 1; }", "catch{", { ecma: 2020 }],
91059101
["try { f(); } catch (e) { eval('g(e)'); }", "catch{", { ecma: 2020 }],
9106-
['a["ÑStuffAfter"];', "a.Ñ", { ecma: 5 }],
9107-
[
9108-
"class B extends A { constructor() { if (x) this.b = 0; else this.b = 1; super(); } }",
9109-
"this.b=+!x",
9110-
{ ecma: 2020 }
9111-
]
9102+
['a["ÑStuffAfter"];', "a.Ñ", { ecma: 5 }]
91129103
]) {
91139104
const { code } = await minify(/** @type {string} */ (input), {
91149105
compress: { passes: 2 },
@@ -9119,6 +9110,18 @@ describe("syntax-printer", () => {
91199110
}
91209111
});
91219112

9113+
it("should keep a constructor's `this.x` read a getter may answer", async () => {
9114+
const { minify } = await load();
9115+
for (const input of [
9116+
'class A { get x() { console.log("get"); return 1; } constructor() { this.x; } } new A();',
9117+
'class A { get x() { console.log("get"); return 1; } constructor() { for (var i = 0; i < 2; i++, this.x); } } new A();',
9118+
'class B { get x() { console.log("get"); return 1; } } class A extends B { constructor() { super(); this.x; } } new A();'
9119+
]) {
9120+
const { code } = await minify(input, { compress: {}, mangle: false });
9121+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
9122+
}
9123+
});
9124+
91229125
it("should keep what `delete` and a call read through `??` once its left side is nullish", async () => {
91239126
const { minify } = await load();
91249127
for (const input of [

0 commit comments

Comments
 (0)