From 9c794844affb178ad077ce11799dd3220bafbf65 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Sat, 21 Jan 2017 01:30:27 +0800 Subject: [PATCH] fix mangling collision with keep_fnames fixes #1423 --- lib/scope.js | 14 ++--- test/compress/issue-1423.js | 122 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 test/compress/issue-1423.js diff --git a/lib/scope.js b/lib/scope.js index bc5db90d..8d74e80f 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -224,7 +224,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ || parent instanceof AST_Assign && parent.left === node) { sym.modified = true; } - node.reference(); + node.reference(options); return true; } }); @@ -255,13 +255,18 @@ AST_Lambda.DEFMETHOD("init_scope_vars", function(){ this.variables.set(symbol.name, def); }); -AST_SymbolRef.DEFMETHOD("reference", function() { +AST_SymbolRef.DEFMETHOD("reference", function(options) { var def = this.definition(); def.references.push(this); var s = this.scope; while (s) { push_uniq(s.enclosed, def); if (s === def.scope) break; + if (options.keep_fnames) { + s.variables.each(function(d) { + push_uniq(def.scope.enclosed, d); + }); + } s = s.parent_scope; } this.frame = this.scope.nesting - def.scope.nesting; @@ -329,11 +334,6 @@ AST_Function.DEFMETHOD("next_mangled", function(options, def){ } }); -AST_Scope.DEFMETHOD("references", function(sym){ - if (sym instanceof AST_Symbol) sym = sym.definition(); - return this.enclosed.indexOf(sym) < 0 ? null : sym; -}); - AST_Symbol.DEFMETHOD("unmangleable", function(options){ return this.definition().unmangleable(options); }); diff --git a/test/compress/issue-1423.js b/test/compress/issue-1423.js new file mode 100644 index 00000000..731ebba8 --- /dev/null +++ b/test/compress/issue-1423.js @@ -0,0 +1,122 @@ +level_one: { + options = { + keep_fnames: true + } + mangle = { + keep_fnames: true + } + input: { + function f(x) { + return function() { + function n(a) { + return a * a; + } + return x(n); + }; + } + } + expect: { + function f(r) { + return function() { + function n(n) { + return n * n; + } + return r(n); + }; + } + } +} + +level_two: { + options = { + keep_fnames: true + } + mangle = { + keep_fnames: true + } + input: { + function f(x) { + return function() { + function r(a) { + return a * a; + } + return function() { + function n(a) { + return a * a; + } + return x(n); + }; + }; + } + } + expect: { + function f(t) { + return function() { + function r(n) { + return n * n; + } + return function() { + function n(n) { + return n * n; + } + return t(n); + }; + }; + } + } +} + +level_three: { + options = { + keep_fnames: true + } + mangle = { + keep_fnames: true + } + input: { + function f(x) { + return function() { + function r(a) { + return a * a; + } + return [ + function() { + function t(a) { + return a * a; + } + return t; + }, + function() { + function n(a) { + return a * a; + } + return x(n); + } + ]; + }; + } + } + expect: { + function f(t) { + return function() { + function r(n) { + return n * n; + } + return [ + function() { + function t(n) { + return n * n; + } + return t; + }, + function() { + function n(n) { + return n * n; + } + return t(n); + } + ]; + }; + } + } +}