Skip to content

Commit

Permalink
fix multiple nested function substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Nov 9, 2017
1 parent 4c0b017 commit 07cb641
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
45 changes: 24 additions & 21 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,14 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
var value;
if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") {
d.fixed = false;
} else if (d.fixed) {
var value = node.fixed_value();
value = node.fixed_value();
if (value && ref_once(d)) {
if (value instanceof AST_Lambda) {
d.single_use = d.scope === node.scope
&& !(d.orig[0] instanceof AST_SymbolFunarg)
|| value.is_constant_expression(node.scope);
} else {
d.single_use = d.scope === node.scope
&& value.is_constant_expression();
}
d.single_use = value instanceof AST_Lambda
|| d.scope === node.scope && value.is_constant_expression();
} else {
d.single_use = false;
}
Expand All @@ -335,10 +330,9 @@ merge(Compressor.prototype, {
} else {
d.fixed = false;
}
} else {
mark_escaped(d, node, value, 0);
}
}
mark_escaped(d, node, value, 0);
}
if (node instanceof AST_SymbolCatch) {
node.definition().fixed = false;
Expand Down Expand Up @@ -385,10 +379,7 @@ merge(Compressor.prototype, {
d.fixed = node;
loop_ids[d.id] = in_loop;
mark(d, true);
if (ref_once(d)) {
d.single_use = d.scope === d.references[0].scope
|| node.is_constant_expression(d.references[0].scope);
}
d.single_use = ref_once(d);
}
var save_ids = safe_ids;
safe_ids = Object.create(null);
Expand Down Expand Up @@ -2210,7 +2201,10 @@ merge(Compressor.prototype, {
&& !self.variables.has(def.name)) {
if (scope) {
var scope_def = scope.find_variable(node);
if (def.undeclared ? !scope_def : scope_def === def) return true;
if (def.undeclared ? !scope_def : scope_def === def) {
result = "f";
return true;
}
}
result = false;
}
Expand Down Expand Up @@ -4257,11 +4251,20 @@ merge(Compressor.prototype, {
if (fixed instanceof AST_Defun) {
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
}
if (fixed
&& d.single_use
&& !(fixed instanceof AST_Function
&& (d.escaped && d.scope !== self.scope
|| recursive_ref(compressor, d)))) {
if (d.single_use && fixed instanceof AST_Function) {
if (d.escaped && d.scope !== self.scope || recursive_ref(compressor, d)) {
d.single_use = false;
} else if (d.scope !== self.scope || d.orig[0] instanceof AST_SymbolFunarg) {
d.single_use = fixed.is_constant_expression(self.scope);
if (d.single_use == "f") {
var scope = self.scope;
do {
if (scope.name) scope.name.definition().single_use = false;
} while (scope = scope.parent_scope);
}
}
}
if (d.single_use && fixed) {
var value = fixed.optimize(compressor);
return value === fixed ? fixed.clone(true) : value;
}
Expand Down
1 change: 1 addition & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -3036,6 +3036,7 @@ issue_2437: {
conditionals: true,
inline: true,
join_vars: true,
passes: 2,
reduce_vars: true,
side_effects: true,
sequences: true,
Expand Down
4 changes: 2 additions & 2 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ issue_2105_1: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
passes: 3,
reduce_vars: true,
side_effects: true,
unused: true,
Expand Down Expand Up @@ -1153,7 +1153,7 @@ issue_2105_2: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
passes: 3,
properties: true,
pure_getters: "strict",
reduce_vars: true,
Expand Down
2 changes: 1 addition & 1 deletion test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ issue_2428: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
passes: 3,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
Expand Down
36 changes: 36 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -3714,6 +3714,7 @@ recursive_inlining_2: {

recursive_inlining_3: {
options = {
passes: 2,
reduce_vars: true,
unused: true,
}
Expand Down Expand Up @@ -3989,3 +3990,38 @@ issue_2450_5: {
"true",
]
}

issue_2449: {
options = {
passes: 10,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = "PASS";
function f() {
return a;
}
function g() {
return f();
}
(function() {
var a = "FAIL";
if (a == a) console.log(g());
})();
}
expect: {
var a = "PASS";
function g() {
return function() {
return a;
}();
}
(function() {
var a = "FAIL";
if (a == a) console.log(g());
})();
}
expect_stdout: "PASS"
}

0 comments on commit 07cb641

Please sign in to comment.