Compare commits
20 Commits
harmony-v3
...
harmony-v3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94b19a9c46 | ||
|
|
bcf95ac02c | ||
|
|
e11cec1ab8 | ||
|
|
38bfb73f06 | ||
|
|
bbedbf4ea0 | ||
|
|
2cfb5aa7da | ||
|
|
6c45101870 | ||
|
|
2c2fd89e34 | ||
|
|
0c43519097 | ||
|
|
352a7de204 | ||
|
|
df9c8dfd72 | ||
|
|
f46281e2b7 | ||
|
|
25a18883f5 | ||
|
|
5b4b07e9a7 | ||
|
|
a8aa28a7a6 | ||
|
|
fe5a68f9d5 | ||
|
|
71e61153b1 | ||
|
|
c8b6f4733d | ||
|
|
29bbc41dfe | ||
|
|
a48f87abf2 |
393
lib/compress.js
393
lib/compress.js
@@ -156,7 +156,9 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
var passes = +this.options.passes || 1;
|
||||
var last_count = 1 / 0;
|
||||
var mangle = { ie8: this.option("ie8") };
|
||||
for (var pass = 0; pass < passes; pass++) {
|
||||
node.figure_out_scope(mangle);
|
||||
if (pass > 0 || this.option("reduce_vars"))
|
||||
node.reset_opt_flags(this);
|
||||
node = node.transform(this);
|
||||
@@ -291,7 +293,7 @@ merge(Compressor.prototype, {
|
||||
self.transform(tt);
|
||||
});
|
||||
|
||||
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) {
|
||||
AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) {
|
||||
var reduce_vars = compressor.option("reduce_vars");
|
||||
var unused = compressor.option("unused");
|
||||
// Stack of look-up tables to keep track of whether a `SymbolDef` has been
|
||||
@@ -322,12 +324,18 @@ merge(Compressor.prototype, {
|
||||
d.fixed = false;
|
||||
} else if (d.fixed) {
|
||||
var value = node.fixed_value();
|
||||
if (unused && !compressor.exposed(d)) {
|
||||
d.single_use = value
|
||||
&& d.references.length == 1
|
||||
&& loop_ids[d.id] === in_loop
|
||||
&& d.scope === node.scope
|
||||
&& value.is_constant_expression();
|
||||
if (value && ref_once(d) && !compressor.exposed(d)) {
|
||||
if (value instanceof AST_Lambda) {
|
||||
d.single_use = d.scope === node.scope
|
||||
&& !(d.orig[0] instanceof AST_SymbolFunarg)
|
||||
|| value.is_constant_expression(node.scope);
|
||||
} else {
|
||||
d.single_use = d.scope === node.scope
|
||||
&& loop_ids[d.id] === in_loop
|
||||
&& value.is_constant_expression();
|
||||
}
|
||||
} else {
|
||||
d.single_use = false;
|
||||
}
|
||||
if (is_modified(node, value, 0, is_immutable(value))) {
|
||||
if (d.single_use) {
|
||||
@@ -390,6 +398,10 @@ merge(Compressor.prototype, {
|
||||
} else {
|
||||
d.fixed = node;
|
||||
mark(d, true);
|
||||
if (ref_once(d)) {
|
||||
d.single_use = d.scope === d.references[0].scope
|
||||
|| node.is_constant_expression(d.references[0].scope);
|
||||
}
|
||||
}
|
||||
var save_ids = safe_ids;
|
||||
safe_ids = Object.create(null);
|
||||
@@ -542,6 +554,7 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return def.fixed instanceof AST_Defun;
|
||||
}
|
||||
|
||||
function safe_to_assign(def, value) {
|
||||
@@ -567,7 +580,7 @@ merge(Compressor.prototype, {
|
||||
function reset_def(def) {
|
||||
def.direct_access = false;
|
||||
def.escaped = false;
|
||||
if (def.scope.uses_eval) {
|
||||
if (def.scope.uses_eval || def.scope.uses_with) {
|
||||
def.fixed = false;
|
||||
} else if (def.orig[0] instanceof AST_SymbolConst || !compressor.exposed(def)) {
|
||||
def.fixed = undefined;
|
||||
@@ -579,8 +592,15 @@ merge(Compressor.prototype, {
|
||||
def.single_use = undefined;
|
||||
}
|
||||
|
||||
function ref_once(def) {
|
||||
return unused && !def.scope.uses_eval && !def.scope.uses_with && def.references.length == 1;
|
||||
}
|
||||
|
||||
function is_immutable(value) {
|
||||
return value && (value.is_constant() || value instanceof AST_Lambda);
|
||||
if (!value) return false;
|
||||
return value.is_constant()
|
||||
|| value instanceof AST_Lambda
|
||||
|| value instanceof AST_This;
|
||||
}
|
||||
|
||||
function read_property(obj, key) {
|
||||
@@ -608,7 +628,7 @@ merge(Compressor.prototype, {
|
||||
|| !immutable
|
||||
&& parent instanceof AST_Call
|
||||
&& parent.expression === node
|
||||
&& (!(value instanceof AST_Function) || value.contains_this())) {
|
||||
&& (!(value instanceof AST_Function) || value.contains_this(parent))) {
|
||||
return true;
|
||||
} else if (parent instanceof AST_Array || parent instanceof AST_Object) {
|
||||
return is_modified(parent, parent, level + 1);
|
||||
@@ -619,7 +639,11 @@ merge(Compressor.prototype, {
|
||||
|
||||
function mark_escaped(d, node, value, level) {
|
||||
var parent = tw.parent(level);
|
||||
if (value instanceof AST_Constant || value instanceof AST_Function) return;
|
||||
if (value instanceof AST_Constant
|
||||
|| value instanceof AST_Function
|
||||
|| value instanceof AST_ClassExpression) {
|
||||
return;
|
||||
}
|
||||
if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right
|
||||
|| parent instanceof AST_Call && node !== parent.expression
|
||||
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|
||||
@@ -811,6 +835,14 @@ merge(Compressor.prototype, {
|
||||
|| compressor.option("unsafe") && global_names(this.name);
|
||||
});
|
||||
|
||||
function drop_decl(def) {
|
||||
def._eliminiated = (def._eliminiated || 0) + 1;
|
||||
if (def.orig.length == def._eliminiated) {
|
||||
def.scope.functions.del(def.name);
|
||||
def.scope.variables.del(def.name);
|
||||
}
|
||||
}
|
||||
|
||||
function tighten_body(statements, compressor) {
|
||||
var CHANGED, max_iter = 10;
|
||||
do {
|
||||
@@ -844,6 +876,7 @@ merge(Compressor.prototype, {
|
||||
function collapse(statements, compressor) {
|
||||
var scope = compressor.find_parent(AST_Scope).get_defun_scope();
|
||||
if (scope.uses_eval || scope.uses_with) return statements;
|
||||
var args;
|
||||
var candidates = [];
|
||||
var stat_index = statements.length;
|
||||
while (--stat_index >= 0) {
|
||||
@@ -864,7 +897,7 @@ merge(Compressor.prototype, {
|
||||
var one_off = lhs instanceof AST_Symbol && lhs.definition().references.length == 1;
|
||||
var side_effects = value_has_side_effects(candidate);
|
||||
var hit = candidate.name instanceof AST_SymbolFunarg;
|
||||
var abort = false, replaced = false;
|
||||
var abort = false, replaced = false, can_replace = !args || !hit;
|
||||
var tt = new TreeTransformer(function(node, descend) {
|
||||
if (abort) return node;
|
||||
// Skip nodes before `candidate` as quickly as possible
|
||||
@@ -891,7 +924,8 @@ merge(Compressor.prototype, {
|
||||
return node;
|
||||
}
|
||||
// Replace variable with assignment when found
|
||||
if (!(node instanceof AST_SymbolDeclaration)
|
||||
if (can_replace
|
||||
&& !(node instanceof AST_SymbolDeclaration)
|
||||
&& !is_lhs(node, parent)
|
||||
&& lhs.equivalent_to(node)) {
|
||||
CHANGED = replaced = abort = true;
|
||||
@@ -942,6 +976,12 @@ merge(Compressor.prototype, {
|
||||
// Skip (non-executed) functions and (leading) default case in switch statements
|
||||
if (node instanceof AST_Default || node instanceof AST_Scope) return node;
|
||||
});
|
||||
if (!can_replace) {
|
||||
for (var j = compressor.self().argnames.lastIndexOf(candidate.__name || candidate.name) + 1; j < args.length; j++) {
|
||||
args[j].transform(tt);
|
||||
}
|
||||
can_replace = true;
|
||||
}
|
||||
for (var i = stat_index; !abort && i < statements.length; i++) {
|
||||
statements[i].transform(tt);
|
||||
}
|
||||
@@ -987,9 +1027,16 @@ merge(Compressor.prototype, {
|
||||
})) {
|
||||
var fn_strict = compressor.has_directive("use strict");
|
||||
if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false;
|
||||
var len = fn.argnames.length;
|
||||
args = iife.args.slice(len);
|
||||
var names = Object.create(null);
|
||||
for (var i = fn.argnames.length; --i >= 0;) {
|
||||
for (var i = len; --i >= 0;) {
|
||||
var sym = fn.argnames[i];
|
||||
var arg = iife.args[i];
|
||||
args.unshift(make_node(AST_VarDef, sym, {
|
||||
name: sym,
|
||||
value: arg
|
||||
}));
|
||||
if (sym.name in names) continue;
|
||||
names[sym.name] = true;
|
||||
if (sym instanceof AST_Expansion) {
|
||||
@@ -1003,9 +1050,9 @@ merge(Compressor.prototype, {
|
||||
elements: elements
|
||||
})
|
||||
}));
|
||||
candidates[0].__name = sym;
|
||||
}
|
||||
} else {
|
||||
var arg = iife.args[i];
|
||||
if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor);
|
||||
else if (has_overlapping_symbol(fn, arg, fn_strict)) arg = null;
|
||||
if (arg) candidates.unshift(make_node(AST_VarDef, sym, {
|
||||
@@ -1037,7 +1084,8 @@ merge(Compressor.prototype, {
|
||||
function get_lhs(expr) {
|
||||
if (expr instanceof AST_VarDef && expr.name instanceof AST_SymbolDeclaration) {
|
||||
var def = expr.name.definition();
|
||||
if (def.orig.length > 1 && !(expr.name instanceof AST_SymbolFunarg)
|
||||
if (def.orig.length - (def._eliminiated || 0) > 1
|
||||
&& !(expr.name instanceof AST_SymbolFunarg)
|
||||
|| def.references.length == 1 && !compressor.exposed(def)) {
|
||||
return make_node(AST_SymbolRef, expr.name, expr.name);
|
||||
}
|
||||
@@ -1047,6 +1095,10 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
|
||||
function get_rvalue(expr) {
|
||||
return expr[expr instanceof AST_Assign ? "right" : "value"];
|
||||
}
|
||||
|
||||
function get_lvalues(expr) {
|
||||
var lvalues = Object.create(null);
|
||||
if (expr instanceof AST_Unary) return lvalues;
|
||||
@@ -1057,7 +1109,7 @@ merge(Compressor.prototype, {
|
||||
lvalues[sym.name] = lvalues[sym.name] || is_lhs(node, tw.parent());
|
||||
}
|
||||
});
|
||||
expr[expr instanceof AST_Assign ? "right" : "value"].walk(tw);
|
||||
get_rvalue(expr).walk(tw);
|
||||
return lvalues;
|
||||
}
|
||||
|
||||
@@ -1086,7 +1138,9 @@ merge(Compressor.prototype, {
|
||||
if (node === expr) {
|
||||
found = true;
|
||||
if (node instanceof AST_VarDef) {
|
||||
remove(node.name.definition().orig, node.name);
|
||||
drop_decl(node.name.definition());
|
||||
node.value = null;
|
||||
return node;
|
||||
}
|
||||
return in_list ? MAP.skip : null;
|
||||
}
|
||||
@@ -1095,16 +1149,13 @@ merge(Compressor.prototype, {
|
||||
case 0: return null;
|
||||
case 1: return node.expressions[0];
|
||||
}
|
||||
if (node instanceof AST_Definitions && node.definitions.length == 0
|
||||
|| node instanceof AST_SimpleStatement && !node.body) {
|
||||
return null;
|
||||
}
|
||||
if (node instanceof AST_SimpleStatement && !node.body) return null;
|
||||
}));
|
||||
}
|
||||
|
||||
function value_has_side_effects(expr) {
|
||||
if (expr instanceof AST_Unary) return false;
|
||||
return expr[expr instanceof AST_Assign ? "right" : "value"].has_side_effects(compressor);
|
||||
return get_rvalue(expr).has_side_effects(compressor);
|
||||
}
|
||||
|
||||
function references_in_scope(def) {
|
||||
@@ -2261,18 +2312,22 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
def(AST_Node, return_false);
|
||||
def(AST_Constant, return_true);
|
||||
def(AST_Function, function(){
|
||||
def(AST_Lambda, function(scope){
|
||||
var self = this;
|
||||
var result = true;
|
||||
self.walk(new TreeWalker(function(node) {
|
||||
if (!result) return true;
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var def = node.definition();
|
||||
if (self.enclosed.indexOf(def) >= 0
|
||||
&& self.variables.get(def.name) !== def) {
|
||||
if (member(def, self.enclosed)
|
||||
&& !self.variables.has(def.name)) {
|
||||
if (scope) {
|
||||
var scope_def = scope.find_variable(node);
|
||||
if (def.undeclared ? !scope_def : scope_def === def) return true;
|
||||
}
|
||||
result = false;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}));
|
||||
return result;
|
||||
@@ -2353,7 +2408,7 @@ merge(Compressor.prototype, {
|
||||
|| can_be_evicted_from_block(self.body[0])) {
|
||||
return self.body[0];
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case 0: return make_node(AST_EmptyStatement, self);
|
||||
}
|
||||
return self;
|
||||
@@ -2389,75 +2444,80 @@ merge(Compressor.prototype, {
|
||||
// this scope (not in nested scopes).
|
||||
var scope = this;
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
if (node !== self) {
|
||||
if (node instanceof AST_Defun || node instanceof AST_DefClass) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
if (in_export || !drop_funcs && scope === self) {
|
||||
var node_def = node.name.definition();
|
||||
if (node_def.global && !(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
}
|
||||
initializations.add(node.name.name, node);
|
||||
return true; // don't go in nested scopes
|
||||
}
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
node.definitions.forEach(function(def){
|
||||
if (def.name instanceof AST_SymbolVar) {
|
||||
var_defs_by_id.add(def.name.definition().id, def);
|
||||
}
|
||||
if (in_export || !drop_vars) {
|
||||
def.name.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_SymbolDeclaration) {
|
||||
var def = node.definition();
|
||||
if ((in_export || def.global) && !(def.id in in_use_ids)) {
|
||||
in_use_ids[def.id] = true;
|
||||
in_use.push(def);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (def.value) {
|
||||
if (def.name instanceof AST_Destructuring) {
|
||||
var destructuring_cache = destructuring_value;
|
||||
destructuring_value = def.value;
|
||||
def.walk(tw);
|
||||
destructuring_value = destructuring_cache;
|
||||
} else {
|
||||
initializations.add(def.name.name, def.value);
|
||||
}
|
||||
if (def.value.has_side_effects(compressor)) {
|
||||
def.value.walk(tw);
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (assign_as_unused(node) instanceof AST_SymbolRef && scope === self
|
||||
&& !is_ref_of(node.left, AST_SymbolBlockDeclaration)) {
|
||||
if (node instanceof AST_Assign) node.right.walk(tw);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var node_def = node.definition();
|
||||
if (!(node_def.id in in_use_ids)) {
|
||||
if (node === self) return;
|
||||
if (node instanceof AST_Defun || node instanceof AST_DefClass) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
if (in_export || !drop_funcs && scope === self) {
|
||||
var node_def = node.name.definition();
|
||||
if (node_def.global && !(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
var save_scope = scope;
|
||||
scope = node;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
return true;
|
||||
}
|
||||
if (node.destructuring && destructuring_value) {
|
||||
initializations.add(node.name, destructuring_value);
|
||||
initializations.add(node.name.name, node);
|
||||
return true; // don't go in nested scopes
|
||||
}
|
||||
if (node instanceof AST_SymbolFunarg && scope === self) {
|
||||
var_defs_by_id.add(node.definition().id, node);
|
||||
}
|
||||
if (node instanceof AST_Definitions && scope === self) {
|
||||
var in_export = tw.parent() instanceof AST_Export;
|
||||
node.definitions.forEach(function(def){
|
||||
if (def.name instanceof AST_SymbolVar) {
|
||||
var_defs_by_id.add(def.name.definition().id, def);
|
||||
}
|
||||
if (in_export || !drop_vars) {
|
||||
def.name.walk(new TreeWalker(function(node) {
|
||||
if (node instanceof AST_SymbolDeclaration) {
|
||||
var def = node.definition();
|
||||
if ((in_export || def.global) && !(def.id in in_use_ids)) {
|
||||
in_use_ids[def.id] = true;
|
||||
in_use.push(def);
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
if (def.value) {
|
||||
if (def.name instanceof AST_Destructuring) {
|
||||
var destructuring_cache = destructuring_value;
|
||||
destructuring_value = def.value;
|
||||
def.walk(tw);
|
||||
destructuring_value = destructuring_cache;
|
||||
} else {
|
||||
initializations.add(def.name.name, def.value);
|
||||
}
|
||||
if (def.value.has_side_effects(compressor)) {
|
||||
def.value.walk(tw);
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
var sym;
|
||||
if (scope === self
|
||||
&& (sym = assign_as_unused(node)) instanceof AST_SymbolRef
|
||||
&& !is_ref_of(node.left, AST_SymbolBlockDeclaration)
|
||||
&& self.variables.get(sym.name) === sym.definition()) {
|
||||
if (node instanceof AST_Assign) node.right.walk(tw);
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var node_def = node.definition();
|
||||
if (!(node_def.id in in_use_ids)) {
|
||||
in_use_ids[node_def.id] = true;
|
||||
in_use.push(node_def);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (node instanceof AST_Scope) {
|
||||
var save_scope = scope;
|
||||
scope = node;
|
||||
descend();
|
||||
scope = save_scope;
|
||||
return true;
|
||||
}
|
||||
if (node.destructuring && destructuring_value) {
|
||||
initializations.add(node.name, destructuring_value);
|
||||
}
|
||||
});
|
||||
self.walk(tw);
|
||||
@@ -2522,9 +2582,11 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
if ((node instanceof AST_Defun || node instanceof AST_DefClass) && node !== self) {
|
||||
var keep = (node.name.definition().id in in_use_ids) || !drop_funcs && node.name.definition().global;
|
||||
var def = node.name.definition();
|
||||
var keep = (def.id in in_use_ids) || !drop_funcs && def.global;
|
||||
if (!keep) {
|
||||
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
|
||||
drop_decl(def);
|
||||
return make_node(AST_EmptyStatement, node);
|
||||
}
|
||||
return node;
|
||||
@@ -2549,7 +2611,7 @@ merge(Compressor.prototype, {
|
||||
if (var_defs.length > 1 && !def.value) {
|
||||
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
|
||||
remove(var_defs, def);
|
||||
remove(sym.orig, def.name);
|
||||
drop_decl(sym);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -2582,7 +2644,7 @@ merge(Compressor.prototype, {
|
||||
} else {
|
||||
compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
|
||||
}
|
||||
remove(sym.orig, def.name);
|
||||
drop_decl(sym);
|
||||
}
|
||||
});
|
||||
if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
|
||||
@@ -2591,7 +2653,7 @@ merge(Compressor.prototype, {
|
||||
var def = tail.pop();
|
||||
compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
|
||||
remove(var_defs, def);
|
||||
remove(def.name.definition().orig, def.name);
|
||||
drop_decl(def.name.definition());
|
||||
side_effects.unshift(make_node(AST_Assign, def, {
|
||||
operator: "=",
|
||||
left: make_node(AST_SymbolRef, def.name, def.name),
|
||||
@@ -2623,8 +2685,7 @@ merge(Compressor.prototype, {
|
||||
var def = assign_as_unused(node);
|
||||
if (def instanceof AST_SymbolRef
|
||||
&& !((def = def.definition()).id in in_use_ids)
|
||||
&& (drop_vars || !def.global)
|
||||
&& self.variables.get(def.name) === def) {
|
||||
&& (drop_vars || !def.global)) {
|
||||
if (node instanceof AST_Assign) {
|
||||
return maintain_this_binding(parent, node, node.right.transform(tt));
|
||||
}
|
||||
@@ -3440,7 +3501,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
a.push(var_);
|
||||
}
|
||||
remove(def.name.definition().orig, def.name);
|
||||
drop_decl(def.name.definition());
|
||||
return a;
|
||||
}, []);
|
||||
if (assignments.length == 0) return null;
|
||||
@@ -3723,6 +3784,7 @@ merge(Compressor.prototype, {
|
||||
&& !exp.uses_arguments
|
||||
&& !exp.uses_eval
|
||||
&& (exp.body instanceof AST_Node || exp.body.length == 1)
|
||||
&& !exp.contains_this()
|
||||
&& all(exp.argnames, function(arg) {
|
||||
if (arg instanceof AST_Expansion) return arg.expression.__unused;
|
||||
return arg.__unused;
|
||||
@@ -3737,23 +3799,6 @@ merge(Compressor.prototype, {
|
||||
expression: stat.body
|
||||
});
|
||||
}
|
||||
if (value) {
|
||||
var tw = new TreeWalker(function(node) {
|
||||
if (!value) return true;
|
||||
if (node instanceof AST_SymbolRef) {
|
||||
var ref = node.scope.find_variable(node);
|
||||
if (ref && ref.scope.parent_scope === fn.parent_scope) {
|
||||
value = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_This && !tw.find_parent(AST_Scope)) {
|
||||
value = null;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
value.walk(tw);
|
||||
}
|
||||
if (value) {
|
||||
var args = self.args.concat(value);
|
||||
return make_sequence(self, args).optimize(compressor);
|
||||
@@ -4043,7 +4088,8 @@ merge(Compressor.prototype, {
|
||||
function is_object(node) {
|
||||
return node instanceof AST_Array
|
||||
|| node instanceof AST_Lambda
|
||||
|| node instanceof AST_Object;
|
||||
|| node instanceof AST_Object
|
||||
|| node instanceof AST_Class;
|
||||
}
|
||||
|
||||
OPT(AST_Binary, function(self, compressor){
|
||||
@@ -4448,49 +4494,64 @@ merge(Compressor.prototype, {
|
||||
if (fixed instanceof AST_Defun) {
|
||||
d.fixed = fixed = make_node(AST_Function, fixed, fixed);
|
||||
}
|
||||
if (compressor.option("unused")
|
||||
&& fixed
|
||||
&& d.references.length == 1
|
||||
&& (d.single_use || is_func_expr(fixed)
|
||||
&& !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg)
|
||||
&& !d.scope.uses_eval
|
||||
&& compressor.find_parent(AST_Scope) === fixed.parent_scope)) {
|
||||
var value = fixed.optimize(compressor);
|
||||
return value === fixed ? fixed.clone(true) : value;
|
||||
}
|
||||
if (compressor.option("evaluate") && fixed) {
|
||||
if (d.should_replace === undefined) {
|
||||
var init = fixed.evaluate(compressor);
|
||||
if (init !== fixed && (compressor.option("unsafe_regexp") || !(init instanceof RegExp))) {
|
||||
init = make_node_from_constant(init, fixed);
|
||||
var value_length = init.optimize(compressor).print_to_string().length;
|
||||
var fn;
|
||||
if (has_symbol_ref(fixed)) {
|
||||
fn = function() {
|
||||
var result = init.optimize(compressor);
|
||||
return result === init ? result.clone(true) : result;
|
||||
};
|
||||
} else {
|
||||
value_length = Math.min(value_length, fixed.print_to_string().length);
|
||||
fn = function() {
|
||||
var result = best_of_expression(init.optimize(compressor), fixed);
|
||||
return result === init || result === fixed ? result.clone(true) : result;
|
||||
};
|
||||
if (fixed && d.single_use) {
|
||||
var recurse;
|
||||
if (fixed instanceof AST_Function) {
|
||||
for (var i = 0; recurse = compressor.parent(i); i++) {
|
||||
if (recurse instanceof AST_Lambda) {
|
||||
var name = recurse.name;
|
||||
if (name && name.definition() === d) break;
|
||||
}
|
||||
var name_length = d.name.length;
|
||||
var overhead = 0;
|
||||
if (compressor.option("unused") && !compressor.exposed(d)) {
|
||||
overhead = (name_length + 2 + value_length) / d.references.length;
|
||||
}
|
||||
d.should_replace = value_length <= name_length + overhead ? fn : false;
|
||||
} else {
|
||||
d.should_replace = false;
|
||||
}
|
||||
}
|
||||
if (d.should_replace) {
|
||||
return d.should_replace();
|
||||
if (!recurse) {
|
||||
var value = fixed.optimize(compressor);
|
||||
return value === fixed ? fixed.clone(true) : value;
|
||||
}
|
||||
}
|
||||
if (fixed && d.should_replace === undefined) {
|
||||
var init;
|
||||
if (fixed instanceof AST_This) {
|
||||
if (!(d.orig[0] instanceof AST_SymbolFunarg)
|
||||
&& all(d.references, function(ref) {
|
||||
return d.scope === ref.scope;
|
||||
})) {
|
||||
init = fixed;
|
||||
}
|
||||
} else {
|
||||
var ev = fixed.evaluate(compressor);
|
||||
if (ev !== fixed && (compressor.option("unsafe_regexp") || !(ev instanceof RegExp))) {
|
||||
init = make_node_from_constant(ev, fixed);
|
||||
}
|
||||
}
|
||||
if (init) {
|
||||
var value_length = init.optimize(compressor).print_to_string().length;
|
||||
var fn;
|
||||
if (has_symbol_ref(fixed)) {
|
||||
fn = function() {
|
||||
var result = init.optimize(compressor);
|
||||
return result === init ? result.clone(true) : result;
|
||||
};
|
||||
} else {
|
||||
value_length = Math.min(value_length, fixed.print_to_string().length);
|
||||
fn = function() {
|
||||
var result = best_of_expression(init.optimize(compressor), fixed);
|
||||
return result === init || result === fixed ? result.clone(true) : result;
|
||||
};
|
||||
}
|
||||
var name_length = d.name.length;
|
||||
var overhead = 0;
|
||||
if (compressor.option("unused") && !compressor.exposed(d)) {
|
||||
overhead = (name_length + 2 + value_length) / d.references.length;
|
||||
}
|
||||
d.should_replace = value_length <= name_length + overhead ? fn : false;
|
||||
} else {
|
||||
d.should_replace = false;
|
||||
}
|
||||
}
|
||||
if (d.should_replace) {
|
||||
return d.should_replace();
|
||||
}
|
||||
}
|
||||
return self;
|
||||
|
||||
@@ -4827,11 +4888,11 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
if (is_lhs(self, compressor.parent())) return self;
|
||||
if (compressor.option("properties") && key !== prop) {
|
||||
var node = self.flatten_object(property, compressor);
|
||||
if (node) {
|
||||
expr = self.expression = node.expression;
|
||||
prop = self.property = node.property;
|
||||
if (key !== prop) {
|
||||
var sub = self.flatten_object(property, compressor);
|
||||
if (sub) {
|
||||
expr = self.expression = sub.expression;
|
||||
prop = self.property = sub.property;
|
||||
}
|
||||
}
|
||||
if (compressor.option("properties") && compressor.option("side_effects")
|
||||
@@ -4877,7 +4938,8 @@ merge(Compressor.prototype, {
|
||||
return self;
|
||||
});
|
||||
|
||||
AST_Lambda.DEFMETHOD("contains_this", function() {
|
||||
AST_Lambda.DEFMETHOD("contains_this", function(grandparent) {
|
||||
if (grandparent instanceof AST_New) return false;
|
||||
var result;
|
||||
var self = this;
|
||||
self.walk(new TreeWalker(function(node) {
|
||||
@@ -4889,6 +4951,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
AST_PropAccess.DEFMETHOD("flatten_object", function(key, compressor) {
|
||||
if (!compressor.option("properties")) return;
|
||||
var arrows = compressor.option("unsafe_arrows") && compressor.option("ecma") >= 6;
|
||||
var expr = this.expression;
|
||||
if (expr instanceof AST_Object) {
|
||||
@@ -4902,7 +4965,7 @@ merge(Compressor.prototype, {
|
||||
})) break;
|
||||
var value = prop.value;
|
||||
if ((value instanceof AST_Accessor || value instanceof AST_Function)
|
||||
&& value.contains_this()) break;
|
||||
&& value.contains_this(compressor.parent())) break;
|
||||
return make_node(AST_Sub, this, {
|
||||
expression: make_node(AST_Array, expr, {
|
||||
elements: props.map(function(prop) {
|
||||
@@ -4952,10 +5015,8 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
}
|
||||
if (is_lhs(self, compressor.parent())) return self;
|
||||
if (compressor.option("properties")) {
|
||||
var node = self.flatten_object(self.property, compressor);
|
||||
if (node) return node.optimize(compressor);
|
||||
}
|
||||
var sub = self.flatten_object(self.property, compressor);
|
||||
if (sub) return sub.optimize(compressor);
|
||||
var ev = self.evaluate(compressor);
|
||||
if (ev !== self) {
|
||||
ev = make_node_from_constant(ev, self).optimize(compressor);
|
||||
|
||||
@@ -141,11 +141,9 @@ function minify(files, options) {
|
||||
if (options.wrap) {
|
||||
toplevel = toplevel.wrap_commonjs(options.wrap);
|
||||
}
|
||||
if (timings) timings.scope1 = Date.now();
|
||||
if (options.compress) toplevel.figure_out_scope(options.mangle);
|
||||
if (timings) timings.compress = Date.now();
|
||||
if (options.compress) toplevel = new Compressor(options.compress).compress(toplevel);
|
||||
if (timings) timings.scope2 = Date.now();
|
||||
if (timings) timings.scope = Date.now();
|
||||
if (options.mangle) toplevel.figure_out_scope(options.mangle);
|
||||
if (timings) timings.mangle = Date.now();
|
||||
if (options.mangle) {
|
||||
@@ -203,9 +201,9 @@ function minify(files, options) {
|
||||
if (timings) {
|
||||
timings.end = Date.now();
|
||||
result.timings = {
|
||||
parse: 1e-3 * (timings.scope1 - timings.parse),
|
||||
scope: 1e-3 * (timings.compress - timings.scope1 + timings.mangle - timings.scope2),
|
||||
compress: 1e-3 * (timings.scope2 - timings.compress),
|
||||
parse: 1e-3 * (timings.compress - timings.parse),
|
||||
compress: 1e-3 * (timings.scope - timings.compress),
|
||||
scope: 1e-3 * (timings.mangle - timings.scope),
|
||||
mangle: 1e-3 * (timings.properties - timings.mangle),
|
||||
properties: 1e-3 * (timings.output - timings.properties),
|
||||
output: 1e-3 * (timings.end - timings.output),
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony",
|
||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||
"license": "BSD-2-Clause",
|
||||
"version": "3.1.6",
|
||||
"version": "3.1.8",
|
||||
"engines": {
|
||||
"node": ">=0.8.0"
|
||||
},
|
||||
|
||||
@@ -314,17 +314,12 @@ issue_2105_1: {
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
(() => {
|
||||
var quux = () => {
|
||||
({
|
||||
prop() {
|
||||
console.log;
|
||||
console.log("PASS");
|
||||
};
|
||||
return {
|
||||
prop() {
|
||||
console.log;
|
||||
quux();
|
||||
}
|
||||
};
|
||||
})().prop();
|
||||
}
|
||||
}).prop();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
node_version: ">=4"
|
||||
@@ -360,17 +355,12 @@ issue_2105_2: {
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
(() => {
|
||||
var quux = () => {
|
||||
({
|
||||
prop: () => {
|
||||
console.log;
|
||||
console.log("PASS");
|
||||
};
|
||||
return {
|
||||
prop: () => {
|
||||
console.log;
|
||||
quux();
|
||||
}
|
||||
};
|
||||
})().prop();
|
||||
}
|
||||
}).prop();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
node_version: ">=6"
|
||||
|
||||
@@ -1487,6 +1487,7 @@ issue_1605_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
toplevel: false,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function foo(x) {
|
||||
@@ -1509,6 +1510,7 @@ issue_1605_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
toplevel: "vars",
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function foo(x) {
|
||||
@@ -1636,6 +1638,7 @@ issue_1631_3: {
|
||||
var_side_effects_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var print = console.log.bind(console);
|
||||
@@ -1658,6 +1661,7 @@ var_side_effects_1: {
|
||||
var_side_effects_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var print = console.log.bind(console);
|
||||
@@ -1683,6 +1687,7 @@ var_side_effects_3: {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var print = console.log.bind(console);
|
||||
@@ -1758,6 +1763,7 @@ iife_2: {
|
||||
}(foo);
|
||||
}
|
||||
expect: {
|
||||
var foo;
|
||||
!function(x) {
|
||||
console.log(x);
|
||||
}(bar());
|
||||
@@ -2044,6 +2050,7 @@ ref_scope: {
|
||||
chained_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 2;
|
||||
@@ -2060,6 +2067,7 @@ chained_1: {
|
||||
chained_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a;
|
||||
@@ -2160,6 +2168,7 @@ inner_lvalues: {
|
||||
double_def: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = x, a = a && y;
|
||||
@@ -2174,6 +2183,7 @@ double_def: {
|
||||
toplevel_single_reference: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a;
|
||||
@@ -2183,9 +2193,10 @@ toplevel_single_reference: {
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
var a;
|
||||
for (var b in x)
|
||||
for (var b in x) {
|
||||
var a;
|
||||
b(a = b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2215,10 +2226,11 @@ unused_orig: {
|
||||
expect: {
|
||||
var a = 1;
|
||||
console.log(function(b) {
|
||||
var a;
|
||||
var c = b;
|
||||
for (var d in c)
|
||||
for (var d in c) {
|
||||
var a;
|
||||
return --b + (a = c[0]);
|
||||
}
|
||||
a && a.NaN;
|
||||
}([2]), a);
|
||||
}
|
||||
@@ -2565,7 +2577,9 @@ issue_2250_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
conditionals: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(x) {
|
||||
@@ -2607,6 +2621,7 @@ issue_2250_2: {
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
{
|
||||
@@ -3163,6 +3178,7 @@ pure_getters_chain: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function o(t, r) {
|
||||
@@ -3183,6 +3199,7 @@ pure_getters_chain: {
|
||||
conditional_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
@@ -3207,6 +3224,7 @@ conditional_1: {
|
||||
conditional_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f(a, b) {
|
||||
@@ -3223,3 +3241,117 @@ conditional_2: {
|
||||
}
|
||||
expect_stdout: "5 5"
|
||||
}
|
||||
|
||||
issue_2425_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 8;
|
||||
(function(b) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 8;
|
||||
(function(b) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "15"
|
||||
}
|
||||
|
||||
issue_2425_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 8;
|
||||
(function(b, c) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 8;
|
||||
(function(b, c) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "15"
|
||||
}
|
||||
|
||||
issue_2425_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var a = 8;
|
||||
(function(b, b) {
|
||||
b.toString();
|
||||
})(--a, a |= 10);
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 8;
|
||||
(function(b, b) {
|
||||
(a |= 10).toString();
|
||||
})(--a);
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: "15"
|
||||
}
|
||||
|
||||
issue_2437: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
conditionals: true,
|
||||
inline: true,
|
||||
join_vars: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
sequences: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function foo() {
|
||||
bar();
|
||||
}
|
||||
function bar() {
|
||||
if (xhrDesc) {
|
||||
var req = new XMLHttpRequest();
|
||||
var result = !!req.onreadystatechange;
|
||||
Object.defineProperty(XMLHttpRequest.prototype, 'onreadystatechange', xhrDesc || {});
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
var req = new XMLHttpRequest();
|
||||
var detectFunc = function () { };
|
||||
req.onreadystatechange = detectFunc;
|
||||
var result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc;
|
||||
req.onreadystatechange = null;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
foo();
|
||||
}
|
||||
expect: {
|
||||
!function() {
|
||||
if (xhrDesc)
|
||||
return result = !!(req = new XMLHttpRequest()).onreadystatechange,
|
||||
Object.defineProperty(XMLHttpRequest.prototype, "onreadystatechange", xhrDesc || {}),
|
||||
result;
|
||||
var req = new XMLHttpRequest(), detectFunc = function() {};
|
||||
req.onreadystatechange = detectFunc;
|
||||
var result = req[SYMBOL_FAKE_ONREADYSTATECHANGE_1] === detectFunc;
|
||||
req.onreadystatechange = null;
|
||||
}();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1294,11 +1294,11 @@ issue_2063: {
|
||||
}
|
||||
}
|
||||
|
||||
issue_2105: {
|
||||
issue_2105_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
passes: 3,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unused: true,
|
||||
@@ -1324,17 +1324,50 @@ issue_2105: {
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
(function() {
|
||||
var quux = function() {
|
||||
({
|
||||
prop: function() {
|
||||
console.log;
|
||||
console.log("PASS");
|
||||
};
|
||||
return {
|
||||
prop: function() {
|
||||
console.log;
|
||||
quux();
|
||||
}
|
||||
}).prop();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
issue_2105_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
properties: true,
|
||||
pure_getters: "strict",
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function(factory) {
|
||||
factory();
|
||||
}( function() {
|
||||
return function(fn) {
|
||||
fn()().prop();
|
||||
}( function() {
|
||||
function bar() {
|
||||
var quux = function() {
|
||||
console.log("PASS");
|
||||
}, foo = function() {
|
||||
console.log;
|
||||
quux();
|
||||
};
|
||||
return { prop: foo };
|
||||
}
|
||||
};
|
||||
})().prop();
|
||||
return bar;
|
||||
} );
|
||||
});
|
||||
}
|
||||
expect: {
|
||||
console.log("PASS");
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
@@ -508,3 +508,41 @@ issue_2114_2: {
|
||||
}
|
||||
expect_stdout: "2"
|
||||
}
|
||||
|
||||
issue_2428: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
pure_getters: "strict",
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unsafe: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function bar(k) {
|
||||
console.log(k);
|
||||
}
|
||||
function foo(x) {
|
||||
return bar(x);
|
||||
}
|
||||
function baz(a) {
|
||||
foo(a);
|
||||
}
|
||||
baz(42);
|
||||
baz("PASS");
|
||||
}
|
||||
expect: {
|
||||
function baz(a) {
|
||||
console.log(a);
|
||||
}
|
||||
baz(42);
|
||||
baz("PASS");
|
||||
}
|
||||
expect_stdout: [
|
||||
"42",
|
||||
"PASS",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -369,3 +369,145 @@ contains_this_3: {
|
||||
}
|
||||
expect_stdout: "1 1 true"
|
||||
}
|
||||
|
||||
hoist_class: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
keep_fnames: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function run(c, v) {
|
||||
return new c(v).value;
|
||||
}
|
||||
var o = {
|
||||
p: class Foo {
|
||||
constructor(value) {
|
||||
this.value = value * 10;
|
||||
}
|
||||
},
|
||||
x: 1,
|
||||
y: 2,
|
||||
};
|
||||
console.log(o.p.name, o.p === o.p, run(o.p, o.x), run(o.p, o.y));
|
||||
}
|
||||
expect: {
|
||||
function run(c, v) {
|
||||
return new c(v).value;
|
||||
}
|
||||
var o_p = class Foo {
|
||||
constructor(value) {
|
||||
this.value = 10 * value;
|
||||
}
|
||||
};
|
||||
console.log(o_p.name, true, run(o_p, 1), run(o_p, 2));
|
||||
}
|
||||
node_version: ">=6"
|
||||
expect_stdout: "Foo true 10 20"
|
||||
}
|
||||
|
||||
hoist_class_with_new: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
keep_fnames: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
p: class Foo {
|
||||
constructor(value) {
|
||||
this.value = value * 10;
|
||||
}
|
||||
},
|
||||
x: 1,
|
||||
y: 2,
|
||||
};
|
||||
console.log(o.p.name, o.p === o.p, new o.p(o.x).value, new o.p(o.y).value);
|
||||
}
|
||||
expect: {
|
||||
// FIXME: class `o.p` not hoisted due to `new`
|
||||
var o = {
|
||||
p: class Foo {
|
||||
constructor(value) {
|
||||
this.value = 10 * value;
|
||||
}
|
||||
},
|
||||
x: 1,
|
||||
y: 2
|
||||
};
|
||||
console.log(o.p.name, o.p == o.p, new o.p(o.x).value, new o.p(o.y).value);
|
||||
}
|
||||
node_version: ">=6"
|
||||
expect_stdout: "Foo true 10 20"
|
||||
}
|
||||
|
||||
hoist_function_with_call: {
|
||||
options = {
|
||||
comparisons: true,
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
keep_fnames: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
p: function Foo(value) {
|
||||
return 10 * value;
|
||||
},
|
||||
x: 1,
|
||||
y: 2
|
||||
};
|
||||
console.log(o.p.name, o.p === o.p, o.p(o.x), o.p(o.y));
|
||||
}
|
||||
expect: {
|
||||
var o_p = function Foo(value){
|
||||
return 10 * value
|
||||
};
|
||||
console.log(o_p.name, true, o_p(1), o_p(2));
|
||||
}
|
||||
expect_stdout: "Foo true 10 20"
|
||||
}
|
||||
|
||||
new_this: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
hoist_props: true,
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
var o = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
f: function(a) {
|
||||
this.b = a;
|
||||
}
|
||||
};
|
||||
console.log(new o.f(o.a).b, o.b);
|
||||
}
|
||||
expect: {
|
||||
console.log(new function(a) {
|
||||
this.b = a;
|
||||
}(1).b, 2);
|
||||
}
|
||||
expect_stdout: "1 2"
|
||||
}
|
||||
|
||||
@@ -177,6 +177,7 @@ export_mangle_2: {
|
||||
export_mangle_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
mangle = {
|
||||
toplevel: true,
|
||||
@@ -195,6 +196,7 @@ export_mangle_3: {
|
||||
export_mangle_4: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
mangle = {
|
||||
toplevel: true,
|
||||
|
||||
@@ -51,6 +51,7 @@ this_binding_collapse_vars: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
};
|
||||
input: {
|
||||
var c = a; c();
|
||||
|
||||
@@ -1282,3 +1282,22 @@ computed_property: {
|
||||
]
|
||||
node_version: ">=4"
|
||||
}
|
||||
|
||||
new_this: {
|
||||
options = {
|
||||
properties: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
new {
|
||||
f: function(a) {
|
||||
this.a = a;
|
||||
}
|
||||
}.f(42);
|
||||
}
|
||||
expect: {
|
||||
new function(a) {
|
||||
this.a = a;
|
||||
}(42);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2131,14 +2131,13 @@ redefine_farg_1: {
|
||||
}
|
||||
expect: {
|
||||
function f(a) {
|
||||
var a;
|
||||
return typeof a;
|
||||
}
|
||||
function g() {
|
||||
return"number";
|
||||
return "number";
|
||||
}
|
||||
function h(a, b) {
|
||||
var a = b;
|
||||
a = b;
|
||||
return typeof a;
|
||||
}
|
||||
console.log(f([]), g([]), h([]));
|
||||
@@ -2173,10 +2172,9 @@ redefine_farg_2: {
|
||||
}
|
||||
expect: {
|
||||
console.log(function(a) {
|
||||
var a;
|
||||
return typeof a;
|
||||
}([]), "number",function(a, b) {
|
||||
var a = b;
|
||||
a = b;
|
||||
return typeof a;
|
||||
}([]));
|
||||
}
|
||||
@@ -2185,11 +2183,13 @@ redefine_farg_2: {
|
||||
|
||||
redefine_farg_3: {
|
||||
options = {
|
||||
cascade: true,
|
||||
evaluate: true,
|
||||
inline: true,
|
||||
keep_fargs: false,
|
||||
passes: 3,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
@@ -3537,3 +3537,596 @@ escaped_prop: {
|
||||
}
|
||||
expect_stdout: "2"
|
||||
}
|
||||
|
||||
issue_2420_1: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function run() {
|
||||
var self = this;
|
||||
if (self.count++)
|
||||
self.foo();
|
||||
else
|
||||
self.bar();
|
||||
}
|
||||
var o = {
|
||||
count: 0,
|
||||
foo: function() { console.log("foo"); },
|
||||
bar: function() { console.log("bar"); },
|
||||
};
|
||||
run.call(o);
|
||||
run.call(o);
|
||||
}
|
||||
expect: {
|
||||
function run() {
|
||||
if (this.count++)
|
||||
this.foo();
|
||||
else
|
||||
this.bar();
|
||||
}
|
||||
var o = {
|
||||
count: 0,
|
||||
foo: function() { console.log("foo"); },
|
||||
bar: function() { console.log("bar"); },
|
||||
};
|
||||
run.call(o);
|
||||
run.call(o);
|
||||
}
|
||||
expect_stdout: [
|
||||
"bar",
|
||||
"foo",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2420_2: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f() {
|
||||
var that = this;
|
||||
if (that.bar)
|
||||
that.foo();
|
||||
else
|
||||
!function(that, self) {
|
||||
console.log(this === that, self === this, that === self);
|
||||
}(that, this);
|
||||
}
|
||||
f.call({
|
||||
bar: 1,
|
||||
foo: function() { console.log("foo", this.bar); },
|
||||
});
|
||||
f.call({});
|
||||
}
|
||||
expect: {
|
||||
function f() {
|
||||
if (this.bar)
|
||||
this.foo();
|
||||
else
|
||||
!function(that, self) {
|
||||
console.log(this === that, self === this, that === self);
|
||||
}(this, this);
|
||||
}
|
||||
f.call({
|
||||
bar: 1,
|
||||
foo: function() { console.log("foo", this.bar); },
|
||||
});
|
||||
f.call({});
|
||||
}
|
||||
expect_stdout: [
|
||||
"foo 1",
|
||||
"false false true",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2420_3: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f() {
|
||||
var that = this;
|
||||
if (that.bar)
|
||||
that.foo();
|
||||
else
|
||||
((that, self) => {
|
||||
console.log(this === that, self === this, that === self);
|
||||
})(that, this);
|
||||
}
|
||||
f.call({
|
||||
bar: 1,
|
||||
foo: function() { console.log("foo", this.bar); },
|
||||
});
|
||||
f.call({});
|
||||
}
|
||||
expect: {
|
||||
function f() {
|
||||
if (this.bar)
|
||||
this.foo();
|
||||
else
|
||||
((that, self) => {
|
||||
console.log(this === that, self === this, that === self);
|
||||
})(this, this);
|
||||
}
|
||||
f.call({
|
||||
bar: 1,
|
||||
foo: function() { console.log("foo", this.bar); },
|
||||
});
|
||||
f.call({});
|
||||
}
|
||||
expect_stdout: [
|
||||
"foo 1",
|
||||
"true true true",
|
||||
]
|
||||
node_version: ">=4"
|
||||
}
|
||||
|
||||
issue_2423_1: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function c() { return 1; }
|
||||
function p() { console.log(c()); }
|
||||
p();
|
||||
p();
|
||||
}
|
||||
expect: {
|
||||
function p() { console.log(function() { return 1; }()); }
|
||||
p();
|
||||
p();
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"1",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2423_2: {
|
||||
options = {
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function c() { return 1; }
|
||||
function p() { console.log(c()); }
|
||||
p();
|
||||
p();
|
||||
}
|
||||
expect: {
|
||||
function p() { console.log(1); }
|
||||
p();
|
||||
p();
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"1",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2423_3: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function c() { return 1; }
|
||||
function p() { console.log(c()); }
|
||||
p();
|
||||
}
|
||||
expect: {
|
||||
(function() { console.log(function() { return 1; }()); })();
|
||||
}
|
||||
expect_stdout: "1"
|
||||
}
|
||||
|
||||
issue_2423_4: {
|
||||
options = {
|
||||
inline: true,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function c() { return 1; }
|
||||
function p() { console.log(c()); }
|
||||
p();
|
||||
}
|
||||
expect: {
|
||||
console.log(1);
|
||||
}
|
||||
expect_stdout: "1"
|
||||
}
|
||||
|
||||
issue_2423_5: {
|
||||
options = {
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function x() {
|
||||
y();
|
||||
}
|
||||
function y() {
|
||||
console.log(1);
|
||||
}
|
||||
function z() {
|
||||
function y() {
|
||||
console.log(2);
|
||||
}
|
||||
x();
|
||||
}
|
||||
z();
|
||||
z();
|
||||
}
|
||||
expect: {
|
||||
function z() {
|
||||
console.log(1);
|
||||
}
|
||||
z();
|
||||
z();
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"1",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2423_6: {
|
||||
options = {
|
||||
inline: true,
|
||||
passes: 2,
|
||||
reduce_vars: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function x() {
|
||||
y();
|
||||
}
|
||||
function y() {
|
||||
console.log(1);
|
||||
}
|
||||
function z() {
|
||||
function y() {
|
||||
console.log(2);
|
||||
}
|
||||
x();
|
||||
y();
|
||||
}
|
||||
z();
|
||||
z();
|
||||
}
|
||||
expect: {
|
||||
function z(){
|
||||
console.log(1);
|
||||
console.log(2);
|
||||
}
|
||||
z();
|
||||
z();
|
||||
}
|
||||
expect_stdout: [
|
||||
"1",
|
||||
"2",
|
||||
"1",
|
||||
"2",
|
||||
]
|
||||
}
|
||||
|
||||
issue_2440_eval_1: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
exec = function() {
|
||||
return eval("foo()");
|
||||
};
|
||||
}
|
||||
expect: {
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
exec = function() {
|
||||
return eval("foo()");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
issue_2440_eval_2: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
exec = function() {
|
||||
return eval("foo()");
|
||||
};
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
exec = function() {
|
||||
return eval("foo()");
|
||||
};
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_2440_with_1: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
with (o) whatever();
|
||||
}
|
||||
expect: {
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
with (o) whatever();
|
||||
}
|
||||
}
|
||||
|
||||
issue_2440_with_2: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
with (o) whatever();
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
}
|
||||
expect: {
|
||||
baz = {
|
||||
quux: foo
|
||||
};
|
||||
with (o) whatever();
|
||||
function foo() {
|
||||
return bar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_2442: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function foo() {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
expect: {}
|
||||
}
|
||||
|
||||
recursive_inlining_1: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function() {
|
||||
function foo() { bar(); }
|
||||
function bar() { foo(); }
|
||||
console.log("PASS");
|
||||
}();
|
||||
}
|
||||
expect: {
|
||||
!function() {
|
||||
console.log("PASS");
|
||||
}();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
recursive_inlining_2: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function() {
|
||||
function foo() { qux(); }
|
||||
function bar() { foo(); }
|
||||
function qux() { bar(); }
|
||||
console.log("PASS");
|
||||
}();
|
||||
}
|
||||
expect: {
|
||||
!function() {
|
||||
console.log("PASS");
|
||||
}();
|
||||
}
|
||||
expect_stdout: "PASS"
|
||||
}
|
||||
|
||||
recursive_inlining_3: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function() {
|
||||
function foo(x) { console.log("foo", x); if (x) bar(x-1); }
|
||||
function bar(x) { console.log("bar", x); if (x) qux(x-1); }
|
||||
function qux(x) { console.log("qux", x); if (x) foo(x-1); }
|
||||
qux(4);
|
||||
}();
|
||||
}
|
||||
expect: {
|
||||
!function() {
|
||||
function qux(x) {
|
||||
console.log("qux", x);
|
||||
if (x) (function(x) {
|
||||
console.log("foo", x);
|
||||
if (x) (function(x) {
|
||||
console.log("bar", x);
|
||||
if (x) qux(x - 1);
|
||||
})(x - 1);
|
||||
})(x - 1);
|
||||
}
|
||||
qux(4);
|
||||
}();
|
||||
}
|
||||
expect_stdout: [
|
||||
"qux 4",
|
||||
"foo 3",
|
||||
"bar 2",
|
||||
"qux 1",
|
||||
"foo 0",
|
||||
]
|
||||
}
|
||||
|
||||
recursive_inlining_4: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function() {
|
||||
function foo(x) { console.log("foo", x); if (x) bar(x-1); }
|
||||
function bar(x) { console.log("bar", x); if (x) qux(x-1); }
|
||||
function qux(x) { console.log("qux", x); if (x) foo(x-1); }
|
||||
qux(4);
|
||||
bar(5);
|
||||
}();
|
||||
}
|
||||
expect: {
|
||||
!function() {
|
||||
function bar(x) {
|
||||
console.log("bar", x);
|
||||
if (x) qux(x - 1);
|
||||
}
|
||||
function qux(x) {
|
||||
console.log("qux", x);
|
||||
if (x) (function(x) {
|
||||
console.log("foo", x);
|
||||
if (x) bar(x - 1);
|
||||
})(x - 1);
|
||||
}
|
||||
qux(4);
|
||||
bar(5);
|
||||
}();
|
||||
}
|
||||
expect_stdout: [
|
||||
"qux 4",
|
||||
"foo 3",
|
||||
"bar 2",
|
||||
"qux 1",
|
||||
"foo 0",
|
||||
"bar 5",
|
||||
"qux 4",
|
||||
"foo 3",
|
||||
"bar 2",
|
||||
"qux 1",
|
||||
"foo 0",
|
||||
]
|
||||
}
|
||||
|
||||
recursive_inlining_5: {
|
||||
options = {
|
||||
reduce_vars: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
!function() {
|
||||
function foo(x) { console.log("foo", x); if (x) bar(x-1); }
|
||||
function bar(x) { console.log("bar", x); if (x) qux(x-1); }
|
||||
function qux(x) { console.log("qux", x); if (x) foo(x-1); }
|
||||
qux(4);
|
||||
bar(5);
|
||||
foo(3);
|
||||
}();
|
||||
}
|
||||
expect: {
|
||||
!function() {
|
||||
function foo(x) {
|
||||
console.log("foo", x);
|
||||
if (x) bar(x - 1);
|
||||
}
|
||||
function bar(x) {
|
||||
console.log("bar", x);
|
||||
if (x) qux(x - 1);
|
||||
}
|
||||
function qux(x) {
|
||||
console.log("qux", x);
|
||||
if (x) foo(x - 1);
|
||||
}
|
||||
qux(4);
|
||||
bar(5);
|
||||
foo(3);
|
||||
}();
|
||||
}
|
||||
expect_stdout: [
|
||||
"qux 4",
|
||||
"foo 3",
|
||||
"bar 2",
|
||||
"qux 1",
|
||||
"foo 0",
|
||||
"bar 5",
|
||||
"qux 4",
|
||||
"foo 3",
|
||||
"bar 2",
|
||||
"qux 1",
|
||||
"foo 0",
|
||||
"foo 3",
|
||||
"bar 2",
|
||||
"qux 1",
|
||||
"foo 0",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -162,6 +162,7 @@ var VALUES = [
|
||||
'"object"',
|
||||
'"number"',
|
||||
'"function"',
|
||||
'this',
|
||||
];
|
||||
|
||||
var BINARY_OPS_NO_COMMA = [
|
||||
@@ -349,10 +350,10 @@ function createParams() {
|
||||
return params.join(', ');
|
||||
}
|
||||
|
||||
function createArgs() {
|
||||
function createArgs(recurmax, stmtDepth, canThrow) {
|
||||
var args = [];
|
||||
for (var n = rng(4); --n >= 0;) {
|
||||
args.push(createValue());
|
||||
args.push(rng(2) ? createValue() : createExpression(recurmax - 1, COMMA_OK, stmtDepth, canThrow));
|
||||
}
|
||||
return args.join(', ');
|
||||
}
|
||||
@@ -390,9 +391,10 @@ function createFunction(recurmax, inGlobal, noDecl, canThrow, stmtDepth) {
|
||||
|
||||
VAR_NAMES.length = namesLenBefore;
|
||||
|
||||
if (noDecl) s = 'var ' + createVarName(MANDATORY) + ' = ' + s + '(' + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + ');';
|
||||
if (noDecl) s = 'var ' + createVarName(MANDATORY) + ' = ' + s;
|
||||
// avoid "function statements" (decl inside statements)
|
||||
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name + '(' + createArgs() + ');';
|
||||
else if (inGlobal || rng(10) > 0) s += 'var ' + createVarName(MANDATORY) + ' = ' + name;
|
||||
s += '(' + createArgs(recurmax, stmtDepth, canThrow) + ');';
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -626,6 +628,9 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
|
||||
case p++:
|
||||
case p++:
|
||||
return createValue();
|
||||
case p++:
|
||||
case p++:
|
||||
return getVarName();
|
||||
case p++:
|
||||
return createExpression(recurmax, COMMA_OK, stmtDepth, canThrow);
|
||||
case p++:
|
||||
|
||||
Reference in New Issue
Block a user