Skip to content

Commit

Permalink
fix corner case in unused (#5877)
Browse files Browse the repository at this point in the history
fixes #5876
  • Loading branch information
alexlamsl authored Jul 12, 2024
1 parent 2c73103 commit 0a33da4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6317,7 +6317,7 @@ Compressor.prototype.compress = function(node) {
if (typeof this.key != "string") return false;
var value = this.value;
if (!value) return true;
return walk_scoped(value, scope);
return this.static ? value.is_constant_expression(scope) : walk_scoped(value, scope);
});
def(AST_Constant, return_true);
def(AST_Lambda, function(scope) {
Expand All @@ -6326,6 +6326,9 @@ Compressor.prototype.compress = function(node) {
def(AST_Object, function(scope) {
return all_constant(this.properties, scope);
});
def(AST_ObjectIdentity, function(scope) {
return this.scope.resolve() === scope;
});
def(AST_ObjectProperty, function(scope) {
return typeof this.key == "string" && this.value.is_constant_expression(scope);
});
Expand Down
55 changes: 54 additions & 1 deletion test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3196,7 +3196,7 @@ issue_5082_2: {
node_version: ">=12"
}

issue_5082_2_static: {
issue_5082_2_strict: {
options = {
inline: true,
passes: 2,
Expand Down Expand Up @@ -4059,3 +4059,56 @@ issue_5874: {
expect_stdout: "PASS"
node_version: ">=12"
}

issue_5876_1: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
class A {
static p = this.q;
f() {}
}
if (A)
console.log("PASS");
}
expect: {
class A {
static p = this.q;
f() {}
}
if (A)
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=12"
}

issue_5876_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
class A {
static p = console.log("foo");
}
if (A)
console.log("bar");
}
expect: {
class A {
static p = console.log("foo");
}
if (A)
console.log("bar");
}
expect_stdout: [
"foo",
"bar",
]
node_version: ">=12"
}
4 changes: 1 addition & 3 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8242,9 +8242,7 @@ issue_5328: {
})(this);
}
expect: {
(function(arguments) {
console.log(Object.keys(arguments).join());
})(this);
void console.log(Object.keys(this).join());
}
expect_stdout: ""
}
Expand Down
4 changes: 1 addition & 3 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -6882,9 +6882,7 @@ issue_3622: {
}
expect: {
var c = "FAIL";
var a;
a = this,
!void (a && (c = "PASS")),
!void (this && (c = "PASS")),
console.log(c);
}
expect_stdout: "PASS"
Expand Down

0 comments on commit 0a33da4

Please sign in to comment.