fix unsafe reduce_vars on arrays & objects (#2397)

This commit is contained in:
Alex Lam S.L
2017-10-24 22:10:36 +08:00
committed by GitHub
parent 1968203d83
commit 74ae16f9f8
2 changed files with 33 additions and 4 deletions

View File

@@ -567,18 +567,18 @@ merge(Compressor.prototype, {
function read_property(obj, key) {
if (key instanceof AST_Constant) key = key.getValue();
if (key instanceof AST_Node) return null;
var value;
if (obj instanceof AST_Array) {
return obj.elements[key];
value = 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;
}
return value instanceof AST_SymbolRef ? value.fixed_value() : value;
}
function is_modified(node, value, level, immutable) {
@@ -589,6 +589,8 @@ merge(Compressor.prototype, {
&& parent.expression === node
&& (!(value instanceof AST_Function) || value.contains_this())) {
return true;
} else if (parent instanceof AST_Array || parent instanceof AST_Object) {
return is_modified(parent, parent, level + 1);
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
return !immutable && is_modified(parent, read_property(value, parent.property), level + 1);
}
@@ -602,6 +604,8 @@ merge(Compressor.prototype, {
|| 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_Array || parent instanceof AST_Object) {
mark_escaped(d, parent, parent, level + 1);
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
mark_escaped(d, parent, read_property(value, parent.property), level + 1);
}