Skip to content

Commit

Permalink
fix corner cases in inline (#5252)
Browse files Browse the repository at this point in the history
fixes #5249
fixes #5250
  • Loading branch information
alexlamsl committed Jan 2, 2022
1 parent 87a7426 commit aa6eb0d
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
33 changes: 31 additions & 2 deletions lib/compress.js
Expand Up @@ -12831,7 +12831,36 @@ Compressor.prototype.compress = function(node) {
def(AST_Node, noop);
def(AST_Assign, noop);
def(AST_Await, function(compressor, scope, no_return, in_loop) {
return this.expression.try_inline(compressor, scope, no_return, in_loop);
var self = this;
var inlined = sync(self.expression).try_inline(compressor, scope, no_return, in_loop);
if (!inlined) return;
return aborts(inlined) ? inlined : make_node(AST_BlockStatement, self, {
body: [ inlined, make_node(AST_SimpleStatement, self, {
body: make_node(AST_Await, self, { expression: make_node(AST_Number, self, { value: 0 })}),
}) ],
});

function sync(node) {
if (!no_return) return node;
if (node.TYPE != "Call") return node;
var fn = node.expression;
switch (fn.CTOR) {
case AST_AsyncArrow:
fn = make_node(AST_Arrow, fn, fn);
break;
case AST_AsyncFunction:
fn = make_node(AST_Function, fn, fn);
break;
case AST_AsyncGeneratorFunction:
fn = make_node(AST_GeneratorFunction, fn, fn);
break;
default:
return node;
}
node = node.clone();
node.expression = fn;
return node;
}
});
def(AST_Binary, function(compressor, scope, no_return, in_loop) {
if (no_return === undefined) return;
Expand All @@ -12843,7 +12872,7 @@ Compressor.prototype.compress = function(node) {
return make_node(AST_If, self, {
condition: make_condition(self.left),
body: inlined,
alternative: null,
alternative: no_return ? null : make_node(AST_Return, self, { value: null }),
});

function make_condition(cond) {
Expand Down
65 changes: 65 additions & 0 deletions test/compress/awaits.js
Expand Up @@ -512,6 +512,42 @@ inline_block_await: {
}

inline_block_await_async: {
options = {
inline: true,
}
input: {
(async function() {
console.log("foo");
await (async function() {
while (await console.log("bar"));
console.log("baz");
})();
console.log("moo");
})().then(console.log);
console.log("moz");
}
expect: {
(async function() {
console.log("foo");
while (await console.log("bar"));
console.log("baz");
await 0;
console.log("moo");
})().then(console.log);
console.log("moz");
}
expect_stdout: [
"foo",
"bar",
"moz",
"baz",
"moo",
"undefined",
]
node_version: ">=8"
}

inline_block_await_async_return: {
options = {
awaits: true,
if_return: true,
Expand Down Expand Up @@ -2540,3 +2576,32 @@ issue_5177: {
expect_stdout: "function"
node_version: ">=8"
}

issue_5250: {
options = {
inline: true,
}
input: {
(async function() {
await function() {
while (console.log("foo"));
}();
console.log("bar");
})();
console.log("baz");
}
expect: {
(async function() {
while (console.log("foo"));
await 0;
console.log("bar");
})();
console.log("baz");
}
expect_stdout: [
"foo",
"baz",
"bar",
]
node_version: ">=8"
}
66 changes: 65 additions & 1 deletion test/compress/functions.js
Expand Up @@ -628,7 +628,8 @@ inline_binary_and: {
while (console.log("baz"));
return void "moo";
return;
}
} else
return;
}());
}
expect_stdout: [
Expand Down Expand Up @@ -7686,3 +7687,66 @@ issue_5240_2: {
}
expect_stdout: "undefined"
}

issue_5249_1: {
options = {
inline: true,
}
input: {
console.log(function() {
if (!console)
var a = "FAIL 1";
else
return void (a && function() {
while (console.log("FAIL 2"));
}());
throw "FAIL 3";
}());
}
expect: {
console.log(function() {
if (!console)
var a = "FAIL 1";
else if (a) {
while (console.log("FAIL 2"));
return;
} else
return;
throw "FAIL 3";
}());
}
expect_stdout: "undefined"
}

issue_5249_2: {
options = {
conditionals: true,
dead_code: true,
evaluate: true,
if_return: true,
inline: true,
passes: 3,
reduce_vars: true,
sequences: true,
side_effects: true,
unused: true,
}
input: {
console.log(function() {
if (!console)
var a = "FAIL 1";
else
return void (a && function() {
while (console.log("FAIL 2"));
}());
throw "FAIL 3";
}());
}
expect: {
console.log(function() {
if (!console)
throw "FAIL 3";
}());
}
expect_stdout: "undefined"
}

0 comments on commit aa6eb0d

Please sign in to comment.