From: Alex Lam S.L Date: Thu, 3 May 2018 22:05:38 +0000 (+0800) Subject: fix `reduce_vars` on nested invocations (#3118) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=c4cebb4b01e3d1dc75da50a9e3fb05d4d342407f;p=UglifyJS.git fix `reduce_vars` on nested invocations (#3118) --- diff --git a/lib/compress.js b/lib/compress.js index 49abc54b..fec4b951 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -340,10 +340,10 @@ merge(Compressor.prototype, { } } - (function(def){ + (function(def) { def(AST_Node, noop); - function reset_def(compressor, def) { + function reset_def(tw, compressor, def) { def.assignments = 0; def.chained = false; def.direct_access = false; @@ -355,15 +355,25 @@ merge(Compressor.prototype, { } else { def.fixed = false; } + if (def.init instanceof AST_Defun && !all(def.references, same_defun_scope)) { + tw.defun_ids[def.id] = undefined; + } def.recursive_refs = 0; def.references = []; def.should_replace = undefined; def.single_use = undefined; + + function same_defun_scope(ref) { + var scope = ref.scope; + do { + if (def.scope === scope) return true; + } while (scope instanceof AST_Function && (scope = scope.parent_scope)); + } } function reset_variables(tw, compressor, scope) { scope.variables.each(function(def) { - reset_def(compressor, def); + reset_def(tw, compressor, def); if (def.fixed === null) { def.safe_ids = tw.safe_ids; mark(tw, def, true); @@ -374,22 +384,14 @@ merge(Compressor.prototype, { }); } - function same_defun_scope(def, ref) { - var scope = ref.scope; - do { - if (def.scope === scope) return true; - } while (scope instanceof AST_Function && (scope = scope.parent_scope)); - } - - function walk_defun(tw, ref) { - var def = ref.definition(); - if (tw.in_loop || !same_defun_scope(def, ref)) { - if (tw.defun_ids[def.id] !== false) tw.defun_ids[def.id] = undefined; - return; - } + function walk_defun(tw, def) { if (def.id in tw.defun_ids) return; - tw.defun_ids[def.id] = true; - def.fixed.walk(tw); + if (!tw.in_loop) { + tw.defun_ids[def.id] = true; + def.fixed.walk(tw); + } else if (tw.defun_ids[def.id] !== false) { + tw.defun_ids[def.id] = undefined; + } } function walk_defuns(tw, scope) { @@ -534,16 +536,10 @@ merge(Compressor.prototype, { return true; }); def(AST_Call, function(tw, descend) { - var exp = this.expression; - if (!(exp instanceof AST_SymbolRef)) return; - var def = exp.definition(); - if (!(def.fixed instanceof AST_Defun)) return; - if (def.id in tw.defun_ids) return; - tw.defun_ids[def.id] = 0; descend(); - if (tw.defun_ids[def.id] === 0) { - delete tw.defun_ids[def.id]; - walk_defun(tw, exp); + var exp = this.expression; + if (exp instanceof AST_SymbolRef && exp.fixed_value() instanceof AST_Defun) { + walk_defun(tw, exp.definition()); } return true; }); @@ -708,11 +704,15 @@ merge(Compressor.prototype, { } } mark_escaped(tw, d, this.scope, this, value, 0, 1); - if (d.fixed instanceof AST_Defun) walk_defun(tw, this); + var parent; + if (d.fixed instanceof AST_Defun + && !((parent = tw.parent()) instanceof AST_Call && parent.expression === this)) { + walk_defun(tw, d); + } }); def(AST_Toplevel, function(tw, descend, compressor) { this.globals.each(function(def) { - reset_def(compressor, def); + reset_def(tw, compressor, def); }); push(tw); reset_variables(tw, compressor, this); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index c4308d38..1d6d1896 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -5939,3 +5939,62 @@ issue_3113_3: { } expect_stdout: "1" } + +issue_3113_4: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + } + input: { + var a = 0, b = 0; + function f() { + b += a; + } + f(f(), ++a); + console.log(a, b); + } + expect: { + var a = 0, b = 0; + function f() { + b += a; + } + f(f(), ++a); + console.log(a, b); + } + expect_stdout: "1 1" +} + +issue_3113_5: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + } + input: { + function f() { + console.log(a); + } + function g() { + f(); + } + while (g()); + var a = 1; + f(); + } + expect: { + function f() { + console.log(a); + } + function g() { + f(); + } + while (g()); + var a = 1; + f(); + } + expect_stdout: [ + "undefined", + "1", + ] +}