aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-07-05 04:57:58 +0100
committerGitHub <noreply@github.com>2021-07-05 11:57:58 +0800
commit6961c57d1e2849b4c7c9e43295015d0bee44daa5 (patch)
treeaf7adfaa209a5d1d3052bfe1484af7ae28e9ac57
parentf5dbb672b90b0591a824935c4855afc99adc2e59 (diff)
downloadtracifyjs-6961c57d1e2849b4c7c9e43295015d0bee44daa5.tar.gz
tracifyjs-6961c57d1e2849b4c7c9e43295015d0bee44daa5.zip
fix corner case in `reduce_vars` (#5051)
fixes #5050
-rw-r--r--lib/compress.js32
-rw-r--r--test/compress/reduce_vars.js29
2 files changed, 42 insertions, 19 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 4aefb8db..e0b7597b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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;
@@ -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;
}
@@ -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) {
@@ -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) {
@@ -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) {
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 735688ea..4af4945f 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -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",
+ ]
+}