aboutsummaryrefslogtreecommitdiff
path: root/lib/propmangle.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2018-07-26 16:35:43 +0800
committerGitHub <noreply@github.com>2018-07-26 16:35:43 +0800
commit304db15a20551fad9b4ee1a55c1777ea7844416a (patch)
tree923eafbb42acaf615efc4ce74ec8705c74ce1dda /lib/propmangle.js
parent7cf72b8d66ea3a504648f42a2142728d520f3141 (diff)
downloadtracifyjs-304db15a20551fad9b4ee1a55c1777ea7844416a.tar.gz
tracifyjs-304db15a20551fad9b4ee1a55c1777ea7844416a.zip
fix corner case in `ie8` & `rename` (#3223)
Diffstat (limited to 'lib/propmangle.js')
-rw-r--r--lib/propmangle.js92
1 files changed, 37 insertions, 55 deletions
diff --git a/lib/propmangle.js b/lib/propmangle.js
index 7ad4804d..9f8bc7fd 100644
--- a/lib/propmangle.js
+++ b/lib/propmangle.js
@@ -53,25 +53,30 @@ function find_builtins(reserved) {
"-Infinity",
"undefined",
].forEach(add);
- [ Object, Array, Function, Number,
- String, Boolean, Error, Math,
- Date, RegExp
+ [
+ Array,
+ Boolean,
+ Date,
+ Error,
+ Function,
+ Math,
+ Number,
+ Object,
+ RegExp,
+ String,
].forEach(function(ctor) {
Object.getOwnPropertyNames(ctor).map(add);
if (ctor.prototype) {
Object.getOwnPropertyNames(ctor.prototype).map(add);
}
});
+
function add(name) {
push_uniq(reserved, name);
}
}
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);
@@ -79,6 +84,10 @@ function reserve_quoted_keys(ast, reserved) {
addStrings(node.property, add);
}
}));
+
+ function add(name) {
+ push_uniq(reserved, name);
+ }
}
function addStrings(node, add) {
@@ -127,10 +136,8 @@ function mangle_properties(ast, options) {
// 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 debug_suffix;
+ if (debug) debug_suffix = options.debug === true ? "" : options.debug;
var names_to_mangle = [];
var unmangleable = [];
@@ -139,18 +146,14 @@ function mangle_properties(ast, options) {
ast.walk(new TreeWalker(function(node) {
if (node instanceof AST_ObjectKeyVal) {
add(node.key);
- }
- else if (node instanceof AST_ObjectProperty) {
+ } else if (node instanceof AST_ObjectProperty) {
// setter or getter, since KeyVal is handled above
add(node.key.name);
- }
- else if (node instanceof AST_Dot) {
+ } else if (node instanceof AST_Dot) {
add(node.property);
- }
- else if (node instanceof AST_Sub) {
+ } else if (node instanceof AST_Sub) {
addStrings(node.property, add);
- }
- else if (node instanceof AST_Call
+ } else if (node instanceof AST_Call
&& node.expression.print_to_string() == "Object.defineProperty") {
addStrings(node.args[1], add);
}
@@ -160,18 +163,14 @@ function mangle_properties(ast, options) {
return ast.transform(new TreeTransformer(function(node) {
if (node instanceof AST_ObjectKeyVal) {
node.key = mangle(node.key);
- }
- else if (node instanceof AST_ObjectProperty) {
+ } else if (node instanceof AST_ObjectProperty) {
// setter or getter
node.key.name = mangle(node.key.name);
- }
- else if (node instanceof AST_Dot) {
+ } else if (node instanceof AST_Dot) {
node.property = mangle(node.property);
- }
- else if (!options.keep_quoted && node instanceof AST_Sub) {
+ } else if (!options.keep_quoted && node instanceof AST_Sub) {
node.property = mangleStrings(node.property);
- }
- else if (node instanceof AST_Call
+ } else if (node instanceof AST_Call
&& node.expression.print_to_string() == "Object.defineProperty") {
node.args[1] = mangleStrings(node.args[1]);
}
@@ -182,9 +181,7 @@ function mangle_properties(ast, options) {
function can_mangle(name) {
if (unmangleable.indexOf(name) >= 0) return false;
if (reserved.indexOf(name) >= 0) return false;
- if (options.only_cache) {
- return cache.has(name);
- }
+ if (options.only_cache) return cache.has(name);
if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
return true;
}
@@ -192,42 +189,29 @@ function mangle_properties(ast, options) {
function should_mangle(name) {
if (regex && !regex.test(name)) return false;
if (reserved.indexOf(name) >= 0) return false;
- return cache.has(name)
- || names_to_mangle.indexOf(name) >= 0;
+ return cache.has(name) || names_to_mangle.indexOf(name) >= 0;
}
function add(name) {
- if (can_mangle(name))
- push_uniq(names_to_mangle, name);
-
- if (!should_mangle(name)) {
- push_uniq(unmangleable, name);
- }
+ if (can_mangle(name)) push_uniq(names_to_mangle, name);
+ if (!should_mangle(name)) push_uniq(unmangleable, name);
}
function mangle(name) {
if (!should_mangle(name)) {
return name;
}
-
var mangled = cache.get(name);
if (!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)) {
- mangled = debug_mangled;
- }
+ var debug_mangled = "_$" + name + "$" + debug_suffix + "_";
+ 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) {
- do {
- mangled = base54(++cname);
- } while (!can_mangle(mangled));
- }
-
+ if (!mangled) do {
+ mangled = base54(++cname);
+ } while (!can_mangle(mangled));
cache.set(name, mangled);
}
return mangled;
@@ -238,11 +222,9 @@ function mangle_properties(ast, options) {
if (node instanceof AST_Sequence) {
var last = node.expressions.length - 1;
node.expressions[last] = mangleStrings(node.expressions[last]);
- }
- else if (node instanceof AST_String) {
+ } else if (node instanceof AST_String) {
node.value = mangle(node.value);
- }
- else if (node instanceof AST_Conditional) {
+ } else if (node instanceof AST_Conditional) {
node.consequent = mangleStrings(node.consequent);
node.alternative = mangleStrings(node.alternative);
}