Merge branch 'master' into harmony-v2.8.8

This commit is contained in:
alexlamsl
2017-03-07 20:25:52 +08:00
12 changed files with 900 additions and 145 deletions

View File

@@ -224,6 +224,7 @@ merge(Compressor.prototype, {
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan){
var reduce_vars = rescan && compressor.option("reduce_vars");
var toplevel = compressor.option("toplevel");
var ie8 = !compressor.option("screw_ie8");
var safe_ids = [];
push();
@@ -245,7 +246,8 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
if (!d.fixed || isModified(node, 0) || !is_safe(d)) {
if (!d.fixed || !is_safe(d)
|| is_modified(node, 0, d.fixed instanceof AST_Lambda)) {
d.fixed = false;
}
}
@@ -265,6 +267,21 @@ merge(Compressor.prototype, {
}
}
}
if (node instanceof AST_Defun) {
var d = node.name.definition();
if (!toplevel && d.global || is_safe(d)) {
d.fixed = false;
} else {
d.fixed = node;
mark_as_safe(d);
}
var save_ids = safe_ids;
safe_ids = [];
push();
descend();
safe_ids = save_ids;
return true;
}
var iife;
if (node instanceof AST_Function
&& (iife = tw.parent()) instanceof AST_Call
@@ -339,18 +356,22 @@ merge(Compressor.prototype, {
}
function reset_def(def) {
def.fixed = undefined;
if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
def.fixed = undefined;
} else {
def.fixed = false;
}
def.references = [];
def.should_replace = undefined;
}
function isModified(node, level) {
function is_modified(node, level, func) {
var parent = tw.parent(level);
if (isLHS(node, parent)
|| parent instanceof AST_Call && parent.expression === node) {
|| !func && parent instanceof AST_Call && parent.expression === node) {
return true;
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
return isModified(parent, level + 1);
return !func && is_modified(parent, level + 1);
}
}
});
@@ -548,11 +569,14 @@ merge(Compressor.prototype, {
// Constant single use vars can be replaced in any scope.
if (var_decl.value.is_constant()) {
var ctt = new TreeTransformer(function(node) {
if (node === ref
&& !ctt.find_parent(AST_Destructuring)
&& !ctt.find_parent(AST_ForIn)) {
return replace_var(node, ctt.parent(), true);
if (node instanceof AST_Destructuring) return node;
var parent = ctt.parent();
if (parent instanceof AST_IterationStatement
&& (parent.condition === node || parent.init === node)) {
return node;
}
if (node === ref)
return replace_var(node, parent, true);
});
stat.transform(ctt);
continue;
@@ -1325,14 +1349,7 @@ merge(Compressor.prototype, {
def(AST_Statement, function(){
throw new Error(string_template("Cannot evaluate a statement [{file}:{line},{col}]", this.start));
});
// XXX: AST_Accessor and AST_Function both inherit from AST_Scope,
// which itself inherits from AST_Statement; however, they aren't
// really statements. This could bite in other places too. :-(
// Wish JS had multiple inheritance.
def(AST_Accessor, function(){
throw def;
});
def(AST_Function, function(){
def(AST_Lambda, function(){
throw def;
});
def(AST_Arrow, function() {
@@ -2714,6 +2731,24 @@ merge(Compressor.prototype, {
OPT(AST_Call, function(self, compressor){
var exp = self.expression;
if (compressor.option("reduce_vars")
&& exp instanceof AST_SymbolRef) {
var def = exp.definition();
if (def.fixed instanceof AST_Defun) {
def.fixed = make_node(AST_Function, def.fixed, def.fixed).clone(true);
}
if (def.fixed instanceof AST_Function) {
exp = def.fixed;
if (compressor.option("unused")
&& def.references.length == 1
&& compressor.find_parent(AST_Scope) === def.scope) {
if (!compressor.option("keep_fnames")) {
exp.name = null;
}
self.expression = exp;
}
}
}
if (compressor.option("unused")
&& exp instanceof AST_Function
&& !exp.uses_arguments