Skip to content

Commit 01fa430

Browse files
authored
fix corner case in unused (#4757)
fixes #4756
1 parent f4ee0f6 commit 01fa430

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

lib/compress.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7323,31 +7323,28 @@ merge(Compressor.prototype, {
73237323
if (!base && !values) return null;
73247324
exprs = [];
73257325
}
7326-
if (values) {
7327-
var fn = make_node(AST_Arrow, this, {
7328-
argnames: [],
7329-
body: [],
7330-
value: make_sequence(this, values),
7331-
});
7332-
fn.init_vars(this.parent_scope);
7333-
exprs.push(make_node(AST_Call, this, {
7334-
args: [],
7335-
expression: fn,
7326+
if (base) {
7327+
var node = to_class_expr(this, true);
7328+
node.properties = [];
7329+
if (exprs.length) node.properties.push(make_node(AST_ClassMethod, this, {
7330+
key: make_sequence(this, exprs),
7331+
value: make_node(AST_Function, this, {
7332+
argnames: [],
7333+
body: [],
7334+
}).init_vars(node),
73367335
}));
7336+
exprs = [ node ];
7337+
73377338
}
7338-
exprs = exprs.length ? make_sequence(this, exprs) : null;
7339-
if (!base) return exprs;
7340-
var node = make_node(AST_ClassExpression, this, this);
7341-
node.name = null;
7342-
node.properties = [];
7343-
if (exprs) node.properties.push(make_node(AST_ClassMethod, this, {
7344-
key: exprs,
7345-
value: make_node(AST_Function, this, {
7339+
if (values) exprs.push(make_node(AST_Call, this, {
7340+
expression: make_node(AST_Arrow, this, {
73467341
argnames: [],
73477342
body: [],
7348-
}).init_vars(node),
7343+
value: make_sequence(this, values),
7344+
}).init_vars(this.parent_scope),
7345+
args: [],
73497346
}));
7350-
return node;
7347+
return make_sequence(this, exprs);
73517348
});
73527349
def(AST_Conditional, function(compressor) {
73537350
var consequent = this.consequent.drop_side_effect_free(compressor);

lib/scope.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ AST_Scope.DEFMETHOD("init_vars", function(parent_scope) {
438438
});
439439
AST_Arrow.DEFMETHOD("init_vars", function(parent_scope) {
440440
init_scope_vars(this, parent_scope);
441+
return this;
441442
});
442443
AST_AsyncArrow.DEFMETHOD("init_vars", function(parent_scope) {
443444
init_scope_vars(this, parent_scope);

test/compress/classes.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,3 +1244,34 @@ new_target: {
12441244
expect_stdout: "function"
12451245
node_version: ">=6"
12461246
}
1247+
1248+
issue_4756: {
1249+
options = {
1250+
toplevel: true,
1251+
unused: true,
1252+
}
1253+
input: {
1254+
try {
1255+
class A extends 42 {
1256+
static [console.log("foo")] = console.log("bar");
1257+
}
1258+
} catch (e) {
1259+
console.log("baz");
1260+
}
1261+
}
1262+
expect: {
1263+
try {
1264+
(class extends 42 {
1265+
[console.log("foo")]() {}
1266+
}),
1267+
(() => console.log("bar"))();
1268+
} catch (e) {
1269+
console.log("baz");
1270+
}
1271+
}
1272+
expect_stdout: [
1273+
"foo",
1274+
"baz",
1275+
]
1276+
node_version: ">=12"
1277+
}

0 commit comments

Comments
 (0)