enhance unsafe evaluate of arrays & objects (#2394)

This commit is contained in:
Alex Lam S.L
2017-10-24 02:58:30 +08:00
committed by GitHub
parent 8a713e449f
commit 86ea38a259
2 changed files with 323 additions and 14 deletions

View File

@@ -311,7 +311,7 @@ merge(Compressor.prototype, {
d.references.push(node);
if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") {
d.fixed = false;
} else {
} else if (d.fixed) {
var value = node.fixed_value();
if (unused) {
d.single_use = value
@@ -320,14 +320,14 @@ merge(Compressor.prototype, {
&& d.scope === node.scope
&& value.is_constant_expression();
}
if (is_modified(node, 0, is_immutable(value))) {
if (is_modified(node, value, 0, is_immutable(value))) {
if (d.single_use) {
d.single_use = "m";
} else {
d.fixed = false;
}
} else {
mark_escaped(d, node, 0);
mark_escaped(d, node, value, 0);
}
}
}
@@ -564,25 +564,46 @@ merge(Compressor.prototype, {
return value && (value.is_constant() || value instanceof AST_Lambda);
}
function is_modified(node, level, immutable) {
var parent = tw.parent(level);
if (is_lhs(node, parent)
|| !immutable && parent instanceof AST_Call && parent.expression === node) {
return true;
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
return !immutable && is_modified(parent, level + 1);
function read_property(obj, key) {
if (key instanceof AST_Constant) key = key.getValue();
if (key instanceof AST_Node) return null;
if (obj instanceof AST_Array) {
return obj.elements[key];
} else if (obj instanceof AST_Object) {
var props = obj.properties;
var value;
for (var i = props.length; --i >= 0;) {
var prop = props[i];
if (!(prop instanceof AST_ObjectKeyVal)) return;
if (!value && props[i].key === key) value = props[i].value;
}
return value;
}
}
function mark_escaped(d, node, level) {
function is_modified(node, value, level, immutable) {
var parent = tw.parent(level);
if (is_lhs(node, parent)
|| !immutable
&& parent instanceof AST_Call
&& parent.expression === node
&& (!(value instanceof AST_Function) || value.contains_this())) {
return true;
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
return !immutable && is_modified(parent, read_property(value, parent.property), level + 1);
}
}
function mark_escaped(d, node, value, level) {
var parent = tw.parent(level);
if (value instanceof AST_Constant || value instanceof AST_Function) 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
|| parent instanceof AST_VarDef && node === parent.value) {
d.escaped = true;
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
mark_escaped(d, parent, level + 1);
mark_escaped(d, parent, read_property(value, parent.property), level + 1);
}
}
});
@@ -1660,6 +1681,7 @@ merge(Compressor.prototype, {
var elements = [];
for (var i = 0, len = this.elements.length; i < len; i++) {
var element = this.elements[i];
if (element instanceof AST_Function) continue;
var value = ev(element, compressor);
if (element === value) return this;
elements.push(value);
@@ -1683,6 +1705,7 @@ merge(Compressor.prototype, {
if (typeof Object.prototype[key] === 'function') {
return this;
}
if (prop.value instanceof AST_Function) continue;
val[key] = ev(prop.value, compressor);
if (val[key] === prop.value) return this;
}