Skip to content

Commit 0cd4a19

Browse files
authored
fix corner case in conditionals (#4599)
fixes #4598
1 parent 35435d4 commit 0cd4a19

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/compress.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4913,10 +4913,10 @@ merge(Compressor.prototype, {
49134913
return self;
49144914
});
49154915

4916-
function trim_block(node) {
4916+
function trim_block(node, in_list) {
49174917
switch (node.body.length) {
49184918
case 0:
4919-
return make_node(AST_EmptyStatement, node);
4919+
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
49204920
case 1:
49214921
var stat = node.body[0];
49224922
if (!(stat instanceof AST_Const || stat instanceof AST_Let)) return stat;
@@ -5983,12 +5983,8 @@ merge(Compressor.prototype, {
59835983
return node;
59845984
}
59855985
}, function(node, in_list) {
5986-
if (node instanceof AST_BlockStatement) switch (node.body.length) {
5987-
case 0:
5988-
return in_list ? List.skip : make_node(AST_EmptyStatement, node);
5989-
case 1:
5990-
var stat = node.body[0];
5991-
if (!(stat instanceof AST_Const || stat instanceof AST_Let)) return stat;
5986+
if (node instanceof AST_BlockStatement) {
5987+
return trim_block(node, in_list);
59925988
} else if (node instanceof AST_For) {
59935989
// Certain combination of unused name + side effect leads to invalid AST:
59945990
// https://github.com/mishoo/UglifyJS/issues/44
@@ -7515,7 +7511,7 @@ merge(Compressor.prototype, {
75157511
var exprs = [];
75167512
for (var i = 0; i < stat.body.length; i++) {
75177513
var line = stat.body[i];
7518-
if (line instanceof AST_Defun) {
7514+
if (is_defun(line)) {
75197515
defuns.push(line);
75207516
} else if (line instanceof AST_EmptyStatement) {
75217517
continue;
@@ -7532,7 +7528,7 @@ merge(Compressor.prototype, {
75327528
}
75337529
return exprs;
75347530
}
7535-
if (stat instanceof AST_Defun) {
7531+
if (is_defun(stat)) {
75367532
defuns.push(stat);
75377533
return [];
75387534
}

test/compress/awaits.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,3 +1047,20 @@ issue_4595: {
10471047
expect_stdout: "0"
10481048
node_version: ">=8"
10491049
}
1050+
1051+
issue_4598: {
1052+
options = {
1053+
conditionals: true,
1054+
}
1055+
input: {
1056+
if (console.log("PASS")) {
1057+
async function f() {}
1058+
}
1059+
}
1060+
expect: {
1061+
async function f() {}
1062+
console.log("PASS");
1063+
}
1064+
expect_stdout: "PASS"
1065+
node_version: ">=8"
1066+
}

0 commit comments

Comments
 (0)