Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5875)
Browse files Browse the repository at this point in the history
fixes #5874
  • Loading branch information
alexlamsl authored Jul 10, 2024
1 parent 124c4d3 commit 2c73103
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
52 changes: 30 additions & 22 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6258,26 +6258,8 @@ Compressor.prototype.compress = function(node) {
return false;
return true;
}
def(AST_Node, return_false);
def(AST_Array, function(scope) {
return all_constant(this.elements, scope);
});
def(AST_Binary, function(scope) {
return this.left.is_constant_expression(scope)
&& this.right.is_constant_expression(scope)
&& can_drop_op(this);
});
def(AST_Class, function(scope) {
var base = this.extends;
if (base && !safe_for_extends(base)) return false;
return all_constant(this.properties, scope);
});
def(AST_ClassProperty, function(scope) {
return typeof this.key == "string" && (!this.value || this.value.is_constant_expression(scope));
});
def(AST_Constant, return_true);
def(AST_Lambda, function(scope) {
var self = this;

function walk_scoped(self, scope) {
var result = true;
var scopes = [];
self.walk(new TreeWalker(function(node, descend) {
Expand All @@ -6294,7 +6276,7 @@ Compressor.prototype.compress = function(node) {
result = false;
return true;
}
if (self.variables.has(node.name)) return true;
if (self.variables && self.variables.has(node.name)) return true;
var def = node.definition();
if (member(def.scope, scopes)) return true;
if (scope && !def.redefined()) {
Expand All @@ -6315,6 +6297,31 @@ Compressor.prototype.compress = function(node) {
}
}));
return result;
}

def(AST_Node, return_false);
def(AST_Array, function(scope) {
return all_constant(this.elements, scope);
});
def(AST_Binary, function(scope) {
return this.left.is_constant_expression(scope)
&& this.right.is_constant_expression(scope)
&& can_drop_op(this);
});
def(AST_Class, function(scope) {
var base = this.extends;
if (base && !safe_for_extends(base)) return false;
return all_constant(this.properties, scope);
});
def(AST_ClassProperty, function(scope) {
if (typeof this.key != "string") return false;
var value = this.value;
if (!value) return true;
return walk_scoped(value, scope);
});
def(AST_Constant, return_true);
def(AST_Lambda, function(scope) {
return walk_scoped(this, scope);
});
def(AST_Object, function(scope) {
return all_constant(this.properties, scope);
Expand Down Expand Up @@ -12704,7 +12711,8 @@ Compressor.prototype.compress = function(node) {
single_use = false;
} else if (fixed.has_side_effects(compressor)) {
single_use = false;
} else if (compressor.option("ie") && fixed instanceof AST_Class) {
} else if (fixed instanceof AST_Class
&& (compressor.option("ie") || !fixed.is_constant_expression(self.scope))) {
single_use = false;
}
if (single_use) fixed.parent_scope = self.scope;
Expand Down
29 changes: 29 additions & 0 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4030,3 +4030,32 @@ issue_5747_2: {
expect_stdout: "function"
node_version: ">=16"
}

issue_5874: {
options = {
reduce_vars: true,
unused: true,
}
input: {
var a = "PASS";
console.log(Object.keys(function() {
class A {
[a];
}
a = "FAIL";
return new A();
}())[0]);
}
expect: {
var a = "PASS";
console.log(Object.keys(function() {
class A {
[a];
}
a = "FAIL";
return new A();
}())[0]);
}
expect_stdout: "PASS"
node_version: ">=12"
}

0 comments on commit 2c73103

Please sign in to comment.