From 10cee1144baaafabd0f49a5789d03f790bd5fe07 Mon Sep 17 00:00:00 2001 From: "Ashley (Scirra)" Date: Thu, 6 Oct 2016 17:41:11 +0100 Subject: [PATCH] Fix issue #1321 Fixes name collision issue, which is not directly related to this patch but has an additional case in debug mode. Added regression test. --- lib/propmangle.js | 9 ++++++--- test/compress/issue-1321.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/compress/issue-1321.js diff --git a/lib/propmangle.js b/lib/propmangle.js index 4d29b3d0..78bb9a63 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -214,15 +214,18 @@ function mangle_properties(ast, options) { // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_. var debug_mangled = "_$" + name + "$" + debug_cache_name + "_"; - if (can_mangle(debug_mangled)) + if (can_mangle(debug_mangled) && !(ignore_quoted && debug_mangled in ignored)) mangled = debug_mangled; } - // either debug mode is off, or it is on and can_mangle returned false + // either debug mode is off, or it is on and we could not use the mangled name if (!mangled) { + // note can_mangle() does not check if the name collides with the 'ignored' set + // (filled with quoted properties when ignore_quoted set). Make sure we add this + // check so we don't collide with a quoted name. do { mangled = base54(++cache.cname); - } while (!can_mangle(mangled)); + } while (!can_mangle(mangled) || (ignore_quoted && mangled in ignored)); } cache.props.set(name, mangled); diff --git a/test/compress/issue-1321.js b/test/compress/issue-1321.js new file mode 100644 index 00000000..8642cf38 --- /dev/null +++ b/test/compress/issue-1321.js @@ -0,0 +1,36 @@ +issue_1321_no_debug: { + mangle_props = { + ignore_quoted: true + } + input: { + var x = {}; + x.foo = 1; + x["a"] = 2 * x.foo; + console.log(x.foo, x["a"]); + } + expect: { + var x = {}; + x.b = 1; + x["a"] = 2 * x.b; + console.log(x.b, x["a"]); + } +} + +issue_1321_debug: { + mangle_props = { + ignore_quoted: true, + debug: "" + } + input: { + var x = {}; + x.foo = 1; + x["_$foo$_"] = 2 * x.foo; + console.log(x.foo, x["_$foo$_"]); + } + expect: { + var x = {}; + x.a = 1; + x["_$foo$_"] = 2 * x.a; + console.log(x.a, x["_$foo$_"]); + } +}