Skip to content

Commit

Permalink
fix corner case in reduce_vars (#5051)
Browse files Browse the repository at this point in the history
fixes #5050
  • Loading branch information
alexlamsl committed Jul 5, 2021
1 parent f5dbb67 commit 6961c57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
32 changes: 13 additions & 19 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ merge(Compressor.prototype, {
if (fn.parent_scope.resolve().may_call_this === return_true) return;
if (marker) {
var visited = member(fn, tw.fn_visited);
if (marker === tw.safe_ids) return !visited && fn;
if (marker === tw.safe_ids) return !visited && walk_fn_def(tw, fn);
if (visited) {
fn.enclosed.forEach(function(d) {
if (fn.variables.get(d.name) === d) return;
Expand All @@ -539,7 +539,7 @@ merge(Compressor.prototype, {
}
} else if (!tw.in_loop && !(tw.fn_scanning && tw.fn_scanning !== def.scope.resolve())) {
fn.safe_ids = tw.safe_ids;
return fn;
return walk_fn_def(tw, fn);
}
fn.safe_ids = false;
}
Expand Down Expand Up @@ -1013,21 +1013,18 @@ merge(Compressor.prototype, {
}
}
exp.walk(tw);
var fixed = exp instanceof AST_SymbolRef && exp.fixed_value();
var optional = node.optional;
var fn;
if (fixed instanceof AST_Lambda) {
fn = mark_fn_def(tw, exp.definition(), fixed);
optional = false;
} else {
tw.find_parent(AST_Scope).may_call_this();
}
if (optional) push(tw);
node.args.forEach(function(arg) {
arg.walk(tw);
});
if (optional) pop(tw);
if (fn) walk_fn_def(tw, fn);
var fixed = exp instanceof AST_SymbolRef && exp.fixed_value();
if (fixed instanceof AST_Lambda) {
mark_fn_def(tw, exp.definition(), fixed);
} else {
tw.find_parent(AST_Scope).may_call_this();
}
return true;
});
def(AST_Class, function(tw, descend, compressor) {
Expand Down Expand Up @@ -1283,8 +1280,7 @@ merge(Compressor.prototype, {
var parent;
if (value instanceof AST_Lambda
&& !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) {
var fn = mark_fn_def(tw, d, value);
if (fn) walk_fn_def(tw, fn);
mark_fn_def(tw, d, value);
}
});
def(AST_Template, function(tw, descend) {
Expand All @@ -1299,17 +1295,15 @@ merge(Compressor.prototype, {
return true;
}
tag.walk(tw);
node.expressions.forEach(function(exp) {
exp.walk(tw);
});
var fixed = tag instanceof AST_SymbolRef && tag.fixed_value();
var fn;
if (fixed instanceof AST_Lambda) {
fn = mark_fn_def(tw, tag.definition(), fixed);
mark_fn_def(tw, tag.definition(), fixed);
} else {
tw.find_parent(AST_Scope).may_call_this();
}
node.expressions.forEach(function(exp) {
exp.walk(tw);
});
if (fn) walk_fn_def(tw, fn);
return true;
});
def(AST_Toplevel, function(tw, descend, compressor) {
Expand Down
29 changes: 29 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -7696,3 +7696,32 @@ issue_5048: {
}
expect_stdout: "undefined"
}

issue_5050: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
}
input: {
function f() {
console.log(a);
}
this;
var a = 1;
f(console.log(2), f(), a = 3);
}
expect: {
function f() {
console.log(a);
}
this;
var a = 1;
f(console.log(2), f(), a = 3);
}
expect_stdout: [
"2",
"1",
"3",
]
}

0 comments on commit 6961c57

Please sign in to comment.