Skip to content

Commit

Permalink
Do not remove static class properties that use this. Closes #1385
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed May 8, 2023
1 parent 474fca5 commit 26b70f3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
9 changes: 9 additions & 0 deletions lib/compress/drop-side-effect-free.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ def_drop_side_effect_free(AST_Class, function (compressor) {
}
}

if (
prop instanceof AST_ClassProperty
&& prop.static
&& prop.value.has_side_effects(compressor)
&& prop.contains_this()
) {
return this;
}

const trimmed_prop = prop.drop_side_effect_free(compressor);
if (trimmed_prop)
with_effects.push(trimmed_prop);
Expand Down
13 changes: 0 additions & 13 deletions lib/compress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3358,19 +3358,6 @@ def_optimize(AST_Chain, function (self, compressor) {
return self;
});

AST_Lambda.DEFMETHOD("contains_this", function() {
return walk(this, node => {
if (node instanceof AST_This) return walk_abort;
if (
node !== this
&& node instanceof AST_Scope
&& !(node instanceof AST_Arrow)
) {
return true;
}
});
});

def_optimize(AST_Dot, function(self, compressor) {
const parent = compressor.parent();
if (is_lhs(self, parent)) return self;
Expand Down
14 changes: 14 additions & 0 deletions lib/compress/inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
AST_PropAccess,
AST_RegExp,
AST_Return,
AST_Scope,
AST_Sequence,
AST_SimpleStatement,
AST_Statement,
Expand Down Expand Up @@ -950,6 +951,19 @@ export const aborts = (thing) => thing && thing.aborts();
node.DEFMETHOD("aborts", func);
});

AST_Node.DEFMETHOD("contains_this", function() {
return walk(this, node => {
if (node instanceof AST_This) return walk_abort;
if (
node !== this
&& node instanceof AST_Scope
&& !(node instanceof AST_Arrow)
) {
return true;
}
});
});

export function is_modified(compressor, tw, node, value, level, immutable) {
var parent = tw.parent(level);
var lhs = is_lhs(node, parent);
Expand Down
56 changes: 56 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -2774,6 +2774,62 @@ unused_class_with_static_props_side_effects: {
expect_stdout: "PASS"
}

unused_class_with_static_props_this: {
node_version = ">=12"
options = {
toplevel: true,
unused: true
}
input: {
let _classThis;
var Foo = class {
static _ = Foo = _classThis = this;
};
const x = Foo = _classThis;
leak(x);
}
expect: {
let _classThis;
(class {
static _ = _classThis = this;
});
const x = _classThis;
leak(x);
}
}

unused_class_with_static_props_this_2: {
node_version = ">=12"
options = {
toplevel: true,
unused: true,
}
input: {
let Foo = (()=>{
let _classThis;
var Foo = class {
static _ = (()=>{
Foo = _classThis = this;
})();
foo(){
return "PASS";
}
};
return Foo = _classThis;
})();
console.log(new Foo().foo());
}
expect: {
let Foo = (()=>{
let _classThis;
(class{static _=(()=>{_classThis=this})();foo(){return "PASS"}});
return _classThis
})();
console.log((new Foo).foo());
}
expect_stdout: "PASS"
}

unused_class_with_static_props_side_effects_2: {
node_version = ">=12"
options = {
Expand Down

0 comments on commit 26b70f3

Please sign in to comment.