diff options
author | Ashley (Scirra) <ashley@scirra.com> | 2016-10-27 11:23:04 -0400 |
---|---|---|
committer | Richard van Velzen <rvanvelzen1@gmail.com> | 2016-11-29 20:25:39 +0100 |
commit | 2a9989dd18c01081c486fe9089e3bb64079c773b (patch) | |
tree | be59842ce6761ae625bd9a0d7f79b88c6f38ae2b /lib/propmangle.js | |
parent | 79b98a9fe87f950607c601a45a3566a46c32f425 (diff) | |
download | tracifyjs-2a9989dd18c01081c486fe9089e3bb64079c773b.tar.gz tracifyjs-2a9989dd18c01081c486fe9089e3bb64079c773b.zip |
Add --mangle-props-debug and fix --mangle-props=unquoted collision
Patch by @AshleyScirra
Based on: PR #1316
Renamed the CLI debug option to --mangle-props-debug
Fixes: #1321 name collision in --mangle-props=unquoted
Diffstat (limited to 'lib/propmangle.js')
-rw-r--r-- | lib/propmangle.js | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/lib/propmangle.js b/lib/propmangle.js index 3923baa6..f2777475 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -66,7 +66,8 @@ function mangle_properties(ast, options) { cache : null, only_cache : false, regex : null, - ignore_quoted : false + ignore_quoted : false, + debug : false }); var reserved = options.reserved; @@ -84,6 +85,15 @@ function mangle_properties(ast, options) { var regex = options.regex; var ignore_quoted = options.ignore_quoted; + // note debug is either false (disabled), or a string of the debug suffix to use (enabled). + // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true' + // the same as passing an empty string. + var debug = (options.debug !== false); + var debug_name_suffix; + if (debug) { + debug_name_suffix = (options.debug === true ? "" : options.debug); + } + var names_to_mangle = []; var unmangleable = []; var ignored = {}; @@ -177,9 +187,25 @@ function mangle_properties(ast, options) { var mangled = cache.props.get(name); if (!mangled) { - do { - mangled = base54(++cache.cname); - } while (!can_mangle(mangled)); + if (debug) { + // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_. + var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_"; + + if (can_mangle(debug_mangled) && !(ignore_quoted && debug_mangled in ignored)) { + mangled = debug_mangled; + } + } + + // 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) || (ignore_quoted && mangled in ignored)); + } + cache.props.set(name, mangled); } return mangled; |