fix corner case in inline (#5306)

fixes #5305
This commit is contained in:
Alex Lam S.L
2022-01-19 00:14:19 +00:00
committed by GitHub
parent 8c2b76eff9
commit efed55f42d
2 changed files with 146 additions and 9 deletions

View File

@@ -1869,22 +1869,26 @@ Compressor.prototype.compress = function(node) {
if (handle_if_return(statements, compressor)) changed = 3; if (handle_if_return(statements, compressor)) changed = 3;
if (!changed && last_changed == 3) break; if (!changed && last_changed == 3) break;
} }
if (compressor.option("inline") >= 4) { if (compressor.option("awaits") && compressor.option("side_effects")) {
if (inline_iife(statements, compressor)) changed = 4; if (trim_awaits(statements, compressor)) changed = 4;
if (!changed && last_changed == 4) break; if (!changed && last_changed == 4) break;
} }
if (compressor.sequences_limit > 0) { if (compressor.option("inline") >= 4) {
if (sequencesize(statements, compressor)) changed = 5; if (inline_iife(statements, compressor)) changed = 5;
if (!changed && last_changed == 5) break; if (!changed && last_changed == 5) break;
if (sequencesize_2(statements, compressor)) changed = 6;
if (!changed && last_changed == 6) break;
} }
if (compressor.option("join_vars")) { if (compressor.sequences_limit > 0) {
if (join_consecutive_vars(statements)) changed = 7; if (sequencesize(statements, compressor)) changed = 6;
if (!changed && last_changed == 6) break;
if (sequencesize_2(statements, compressor)) changed = 7;
if (!changed && last_changed == 7) break; if (!changed && last_changed == 7) break;
} }
if (compressor.option("join_vars")) {
if (join_consecutive_vars(statements)) changed = 8;
if (!changed && last_changed == 8) break;
}
if (compressor.option("collapse_vars")) { if (compressor.option("collapse_vars")) {
if (collapse(statements, compressor)) changed = 8; if (collapse(statements, compressor)) changed = 9;
} }
} while (changed && max_iter-- > 0); } while (changed && max_iter-- > 0);
return statements; return statements;
@@ -3524,6 +3528,27 @@ Compressor.prototype.compress = function(node) {
return statements.length != len; return statements.length != len;
} }
function trim_awaits(statements, compressor) {
if (!in_lambda || in_try && in_try.bfinally) return;
var changed = false;
for (var index = statements.length; --index >= 0;) {
var stat = statements[index];
if (!(stat instanceof AST_SimpleStatement)) break;
var node = stat.body;
if (!(node instanceof AST_Await)) break;
var exp = node.expression;
if (!is_primitive(compressor, exp)) break;
changed = true;
exp = exp.drop_side_effect_free(compressor, true);
if (exp) {
stat.body = exp;
break;
}
}
statements.length = index + 1;
return changed;
}
function inline_iife(statements, compressor) { function inline_iife(statements, compressor) {
var changed = false; var changed = false;
var index = statements.length - 1; var index = statements.length - 1;
@@ -12987,6 +13012,14 @@ Compressor.prototype.compress = function(node) {
var self = this; var self = this;
var inlined = sync(self.expression).try_inline(compressor, scope, no_return, in_loop); var inlined = sync(self.expression).try_inline(compressor, scope, no_return, in_loop);
if (!inlined) return; if (!inlined) return;
if (!no_return) scan_local_returns(inlined, function(node) {
node.in_bool = false;
var value = node.value;
if (value instanceof AST_Await) return;
node.value = make_node(AST_Await, self, {
expression: value || make_node(AST_Undefined, node).transform(compressor),
});
});
return aborts(inlined) ? inlined : make_node(AST_BlockStatement, self, { return aborts(inlined) ? inlined : make_node(AST_BlockStatement, self, {
body: [ inlined, make_node(AST_SimpleStatement, self, { body: [ inlined, make_node(AST_SimpleStatement, self, {
body: make_node(AST_Await, self, { expression: make_node(AST_Number, self, { value: 0 })}), body: make_node(AST_Await, self, { expression: make_node(AST_Number, self, { value: 0 })}),

View File

@@ -496,6 +496,7 @@ inline_block_await: {
awaits: true, awaits: true,
if_return: true, if_return: true,
inline: true, inline: true,
side_effects: true,
} }
input: { input: {
console.log("foo"); console.log("foo");
@@ -567,6 +568,7 @@ inline_block_await_async_return: {
awaits: true, awaits: true,
if_return: true, if_return: true,
inline: true, inline: true,
side_effects: true,
} }
input: { input: {
console.log("foo"); console.log("foo");
@@ -2855,3 +2857,105 @@ issue_5298: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=8" node_version: ">=8"
} }
issue_5305_1: {
options = {
inline: true,
}
input: {
var a = "PASS";
(async function() {
try {
return await function() {
while (!console);
}();
} finally {
a = "FAIL";
}
})();
console.log(a);
}
expect: {
var a = "PASS";
(async function() {
try {
while (!console);
return await void 0;
} finally {
a = "FAIL";
}
})();
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=8"
}
issue_5305_2: {
options = {
inline: true,
}
input: {
var a = "PASS";
(async function() {
try {
throw null;
} catch (e) {
return await function() {
while (!console);
}();
} finally {
a = "FAIL";
}
})();
console.log(a);
}
expect: {
var a = "PASS";
(async function() {
try {
throw null;
} catch (e) {
while (!console);
return await void 0;
} finally {
a = "FAIL";
}
})();
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=8"
}
issue_5305_3: {
options = {
awaits: true,
inline: true,
side_effects: true,
}
input: {
var a = "PASS";
(async function() {
try {
await function() {
while (!console);
}();
} catch (e) {
a = "FAIL";
}
})();
console.log(a);
}
expect: {
var a = "PASS";
try {
while (!console);
} catch (e) {
a = "FAIL";
}
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=8"
}