fix corner case in evaluate (#4341)

fixes #4340
This commit is contained in:
Alex Lam S.L
2020-12-07 08:05:11 +00:00
committed by GitHub
parent 2f31f95095
commit fbecedf94c
4 changed files with 32 additions and 11 deletions

View File

@@ -569,6 +569,10 @@ var AST_Function = DEFNODE("Function", "inlined", {
}, },
}, AST_Lambda); }, AST_Lambda);
function is_defun(node) {
return node instanceof AST_AsyncDefun || node instanceof AST_Defun;
}
var AST_AsyncDefun = DEFNODE("AsyncDefun", null, { var AST_AsyncDefun = DEFNODE("AsyncDefun", null, {
$documentation: "An asynchronous function definition", $documentation: "An asynchronous function definition",
_validate: function() { _validate: function() {

View File

@@ -5115,7 +5115,7 @@ merge(Compressor.prototype, {
} }
if (node === self) return; if (node === self) return;
if (scope === self) { if (scope === self) {
if (node instanceof AST_AsyncDefun || node instanceof AST_Defun) { if (is_defun(node)) {
var def = node.name.definition(); var def = node.name.definition();
if (!drop_funcs && !(def.id in in_use_ids)) { if (!drop_funcs && !(def.id in in_use_ids)) {
in_use_ids[def.id] = true; in_use_ids[def.id] = true;
@@ -5266,7 +5266,7 @@ merge(Compressor.prototype, {
if (node instanceof AST_Call) calls_to_drop_args.push(node); if (node instanceof AST_Call) calls_to_drop_args.push(node);
if (scope !== self) return; if (scope !== self) return;
if (node instanceof AST_Lambda) { if (node instanceof AST_Lambda) {
if (drop_funcs && node !== self && (node instanceof AST_AsyncDefun || node instanceof AST_Defun)) { if (drop_funcs && node !== self && is_defun(node)) {
var def = node.name.definition(); var def = node.name.definition();
if (!(def.id in in_use_ids)) { if (!(def.id in in_use_ids)) {
log(node.name, "Dropping unused function {name}"); log(node.name, "Dropping unused function {name}");
@@ -7826,7 +7826,7 @@ merge(Compressor.prototype, {
in_order = null; in_order = null;
return; return;
} }
if (def.init instanceof AST_AsyncDefun || def.init instanceof AST_Defun) return abort = true; if (is_defun(def.init)) return abort = true;
if (is_lhs(node, this.parent())) return abort = true; if (is_lhs(node, this.parent())) return abort = true;
var index = resolve_index(def); var index = resolve_index(def);
if (!(begin < index)) begin = index; if (!(begin < index)) begin = index;
@@ -7877,7 +7877,7 @@ merge(Compressor.prototype, {
function can_inject_vars(defined, used, safe_to_inject) { function can_inject_vars(defined, used, safe_to_inject) {
for (var i = 0; i < fn.body.length; i++) { for (var i = 0; i < fn.body.length; i++) {
var stat = fn.body[i]; var stat = fn.body[i];
if (stat instanceof AST_AsyncDefun || stat instanceof AST_Defun) { if (is_defun(stat)) {
if (!safe_to_inject || var_exists(used, stat.name.name)) return false; if (!safe_to_inject || var_exists(used, stat.name.name)) return false;
if (!all(stat.enclosed, function(def) { if (!all(stat.enclosed, function(def) {
return def.scope === stat || !defined[def.name]; return def.scope === stat || !defined[def.name];
@@ -7925,9 +7925,7 @@ merge(Compressor.prototype, {
fn.walk(new TreeWalker(function(node) { fn.walk(new TreeWalker(function(node) {
if (found) return true; if (found) return true;
if (node instanceof AST_Scope && node !== fn) { if (node instanceof AST_Scope && node !== fn) {
if (node instanceof AST_AsyncDefun || node instanceof AST_Defun) { if (is_defun(node) && node.name.name == "await") found = true;
if (node.name.name == "await") found = true;
}
return true; return true;
} }
if (node instanceof AST_Symbol && node.name == "await" && node !== fn.name) return found = true; if (node instanceof AST_Symbol && node.name == "await" && node !== fn.name) return found = true;
@@ -8027,7 +8025,7 @@ merge(Compressor.prototype, {
flatten_vars(decls, expressions); flatten_vars(decls, expressions);
expressions.push(value); expressions.push(value);
var args = fn.body.filter(function(stat) { var args = fn.body.filter(function(stat) {
if (stat instanceof AST_AsyncDefun || stat instanceof AST_Defun) { if (is_defun(stat)) {
var def = stat.name.definition(); var def = stat.name.definition();
scope.functions.set(def.name, def); scope.functions.set(def.name, def);
scope.variables.set(def.name, def); scope.variables.set(def.name, def);

View File

@@ -112,7 +112,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
var next_def_id = 0; var next_def_id = 0;
var scope = self.parent_scope = null; var scope = self.parent_scope = null;
var tw = new TreeWalker(function(node, descend) { var tw = new TreeWalker(function(node, descend) {
if (node instanceof AST_AsyncDefun || node instanceof AST_Defun) { if (is_defun(node)) {
node.name.walk(tw); node.name.walk(tw);
walk_scope(function() { walk_scope(function() {
node.argnames.forEach(function(argname) { node.argnames.forEach(function(argname) {
@@ -397,7 +397,7 @@ AST_BlockScope.DEFMETHOD("find_variable", function(name) {
AST_BlockScope.DEFMETHOD("def_function", function(symbol, init) { AST_BlockScope.DEFMETHOD("def_function", function(symbol, init) {
var def = this.def_variable(symbol, init); var def = this.def_variable(symbol, init);
if (!def.init || def.init instanceof AST_Defun) def.init = init; if (!def.init || is_defun(def.init)) def.init = init;
this.functions.set(symbol.name, def); this.functions.set(symbol.name, def);
return def; return def;
}); });
@@ -406,7 +406,7 @@ AST_BlockScope.DEFMETHOD("def_variable", function(symbol, init) {
var def = this.variables.get(symbol.name); var def = this.variables.get(symbol.name);
if (def) { if (def) {
def.orig.push(symbol); def.orig.push(symbol);
if (def.init instanceof AST_Function) def.init = init; if (is_function(def.init)) def.init = init;
} else { } else {
def = this.make_def(symbol, init); def = this.make_def(symbol, init);
this.variables.set(symbol.name, def); this.variables.set(symbol.name, def);

View File

@@ -287,3 +287,22 @@ issue_4337: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=8" node_version: ">=8"
} }
issue_4340: {
options = {
evaluate: true,
reduce_vars: true,
}
input: {
(async function a(a) {
console.log(a || "PASS");
})();
}
expect: {
(async function a(a) {
console.log(a || "PASS");
})();
}
expect_stdout: "PASS"
node_version: ">=8"
}