diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:01:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:02:09 +0200 |
commit | 363723fc84f7b8477592e0105aeb331ec9a017af (patch) | |
tree | 29f92724f34131bac64d6a318dd7e30612e631c7 /node_modules/uglify-js/lib/propmangle.js | |
parent | 5634e77ad96bfe1818f6b6ee70b7379652e5487f (diff) |
node_modules
Diffstat (limited to 'node_modules/uglify-js/lib/propmangle.js')
-rw-r--r-- | node_modules/uglify-js/lib/propmangle.js | 121 |
1 files changed, 49 insertions, 72 deletions
diff --git a/node_modules/uglify-js/lib/propmangle.js b/node_modules/uglify-js/lib/propmangle.js index b62229903..36a67e80a 100644 --- a/node_modules/uglify-js/lib/propmangle.js +++ b/node_modules/uglify-js/lib/propmangle.js @@ -43,16 +43,16 @@ "use strict"; -function find_builtins() { +function find_builtins(reserved) { // NaN will be included due to Number.NaN - var a = [ + [ "null", "true", "false", "Infinity", "-Infinity", "undefined", - ]; + ].forEach(add); [ Object, Array, Function, Number, String, Boolean, Error, Math, Date, RegExp @@ -63,24 +63,52 @@ function find_builtins() { } }); function add(name) { - push_uniq(a, name); + push_uniq(reserved, name); } - return a; +} + +function reserve_quoted_keys(ast, reserved) { + function add(name) { + push_uniq(reserved, name); + } + + ast.walk(new TreeWalker(function(node) { + if (node instanceof AST_ObjectKeyVal && node.quote) { + add(node.key); + } else if (node instanceof AST_Sub) { + addStrings(node.property, add); + } + })); +} + +function addStrings(node, add) { + node.walk(new TreeWalker(function(node) { + if (node instanceof AST_Sequence) { + addStrings(node.expressions[node.expressions.length - 1], add); + } else if (node instanceof AST_String) { + add(node.value); + } else if (node instanceof AST_Conditional) { + addStrings(node.consequent, add); + addStrings(node.alternative, add); + } + return true; + })); } function mangle_properties(ast, options) { options = defaults(options, { + builtins: false, cache: null, debug: false, - ignore_quoted: false, + keep_quoted: false, only_cache: false, regex: null, reserved: null, - }); + }, true); var reserved = options.reserved; - if (reserved == null) - reserved = find_builtins(); + if (!Array.isArray(reserved)) reserved = []; + if (!options.builtins) find_builtins(reserved); var cache = options.cache; if (cache == null) { @@ -91,12 +119,11 @@ 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 = options.debug !== false; var debug_name_suffix; if (debug) { debug_name_suffix = (options.debug === true ? "" : options.debug); @@ -104,12 +131,11 @@ function mangle_properties(ast, options) { var names_to_mangle = []; var unmangleable = []; - var ignored = {}; // step 1: find candidates to mangle ast.walk(new TreeWalker(function(node){ if (node instanceof AST_ObjectKeyVal) { - add(node.key, ignore_quoted && node.quote); + add(node.key); } else if (node instanceof AST_ObjectProperty) { // setter or getter, since KeyVal is handled above @@ -119,15 +145,14 @@ function mangle_properties(ast, options) { add(node.property); } else if (node instanceof AST_Sub) { - addStrings(node.property, ignore_quoted); + addStrings(node.property, add); } })); // step 2: transform the tree, renaming properties return ast.transform(new TreeTransformer(function(node){ if (node instanceof AST_ObjectKeyVal) { - if (!(ignore_quoted && node.quote)) - node.key = mangle(node.key); + node.key = mangle(node.key); } else if (node instanceof AST_ObjectProperty) { // setter or getter @@ -136,22 +161,9 @@ function mangle_properties(ast, options) { else if (node instanceof AST_Dot) { node.property = mangle(node.property); } - else if (node instanceof AST_Sub) { - if (!ignore_quoted) - node.property = mangleStrings(node.property); + else if (!options.keep_quoted && node instanceof AST_Sub) { + node.property = mangleStrings(node.property); } - // else if (node instanceof AST_String) { - // if (should_mangle(node.value)) { - // AST_Node.warn( - // "Found \"{prop}\" property candidate for mangling in an arbitrary string [{file}:{line},{col}]", { - // file : node.start.file, - // line : node.start.line, - // col : node.start.col, - // prop : node.value - // } - // ); - // } - // } })); // only function declarations after this line @@ -167,19 +179,13 @@ function mangle_properties(ast, options) { } function should_mangle(name) { - if (ignore_quoted && name in ignored) return false; if (regex && !regex.test(name)) return false; if (reserved.indexOf(name) >= 0) return false; return cache.props.has(name) || names_to_mangle.indexOf(name) >= 0; } - function add(name, ignore) { - if (ignore) { - ignored[name] = true; - return; - } - + function add(name) { if (can_mangle(name)) push_uniq(names_to_mangle, name); @@ -199,19 +205,16 @@ 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_name_suffix + "_"; - if (can_mangle(debug_mangled) && !(ignore_quoted && debug_mangled in ignored)) { + if (can_mangle(debug_mangled)) { 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)); + } while (!can_mangle(mangled)); } cache.props.set(name, mangled); @@ -219,36 +222,11 @@ function mangle_properties(ast, options) { return mangled; } - function addStrings(node, ignore) { - var out = {}; - try { - (function walk(node){ - node.walk(new TreeWalker(function(node){ - if (node instanceof AST_Seq) { - walk(node.cdr); - return true; - } - if (node instanceof AST_String) { - add(node.value, ignore); - return true; - } - if (node instanceof AST_Conditional) { - walk(node.consequent); - walk(node.alternative); - return true; - } - throw out; - })); - })(node); - } catch(ex) { - if (ex !== out) throw ex; - } - } - function mangleStrings(node) { return node.transform(new TreeTransformer(function(node){ - if (node instanceof AST_Seq) { - node.cdr = mangleStrings(node.cdr); + if (node instanceof AST_Sequence) { + var last = node.expressions.length - 1; + node.expressions[last] = mangleStrings(node.expressions[last]); } else if (node instanceof AST_String) { node.value = mangle(node.value); @@ -260,5 +238,4 @@ function mangle_properties(ast, options) { return node; })); } - } |