Skip to content

Commit

Permalink
fix corner case in awaits (#4973)
Browse files Browse the repository at this point in the history
fixes #4972
  • Loading branch information
alexlamsl committed May 28, 2021
1 parent 8cd95dd commit d320a6c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/compress.js
Expand Up @@ -9898,7 +9898,16 @@ merge(Compressor.prototype, {
if (is_arrow(parent)) {
if (parent.value === node) return exp.optimize(compressor);
} else if (parent instanceof AST_Return) {
return exp.optimize(compressor);
var drop = true;
do {
node = parent;
parent = compressor.parent(level++);
if (parent instanceof AST_Try && parent.bfinally && parent.bfinally !== node) {
drop = false;
break;
}
} while (parent && !(parent instanceof AST_Scope));
if (drop) return exp.optimize(compressor);
} else if (parent instanceof AST_Sequence) {
if (parent.tail_node() === node) continue;
}
Expand Down
108 changes: 108 additions & 0 deletions test/compress/awaits.js
Expand Up @@ -1574,3 +1574,111 @@ issue_4764_3: {
expect_stdout: "PASS"
node_version: ">=8"
}

issue_4972_1: {
options = {
awaits: true,
side_effects: true,
}
input: {
console.log("foo");
(async function() {
try {
return await "bar";
} finally {
console.log("baz");
}
})().then(console.log);
console.log("moo");
}
expect: {
console.log("foo");
(async function() {
try {
return await "bar";
} finally {
console.log("baz");
}
})().then(console.log);
console.log("moo");
}
expect_stdout: [
"foo",
"moo",
"baz",
"bar",
]
node_version: ">=8"
}

issue_4972_2: {
options = {
awaits: true,
side_effects: true,
}
input: {
console.log("foo");
(async function() {
try {
console.log("bar");
} finally {
return await "baz";
}
})().then(console.log);
console.log("moo");
}
expect: {
console.log("foo");
(async function() {
try {
console.log("bar");
} finally {
return "baz";
}
})().then(console.log);
console.log("moo");
}
expect_stdout: [
"foo",
"bar",
"moo",
"baz",
]
node_version: ">=8"
}

issue_4972_3: {
options = {
awaits: true,
side_effects: true,
}
input: {
console.log("foo");
try {
(async function() {
return await "bar";
})().then(console.log);
} finally {
console.log("baz");
}
console.log("moo");
}
expect: {
console.log("foo");
try {
(async function() {
return "bar";
})().then(console.log);
} finally {
console.log("baz");
}
console.log("moo");
}
expect_stdout: [
"foo",
"baz",
"moo",
"bar",
]
node_version: ">=8"
}

0 comments on commit d320a6c

Please sign in to comment.