aboutsummaryrefslogtreecommitdiff
path: root/lib/compress.js
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-10-24 02:58:30 +0800
committerGitHub <noreply@github.com>2017-10-24 02:58:30 +0800
commit86ea38a25954ba4cda017fbfee5e0ef537b24bf1 (patch)
tree473853194318b15bf60df8150e837521a7742712 /lib/compress.js
parent8a713e449f551d89745d9a8105b7cb33e9893214 (diff)
downloadtracifyjs-86ea38a25954ba4cda017fbfee5e0ef537b24bf1.tar.gz
tracifyjs-86ea38a25954ba4cda017fbfee5e0ef537b24bf1.zip
enhance `unsafe` `evaluate` of arrays & objects (#2394)
Diffstat (limited to 'lib/compress.js')
-rw-r--r--lib/compress.js39
1 files changed, 31 insertions, 8 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 7085fcb3..25718a5e 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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) {
+ 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 is_modified(node, value, level, immutable) {
var parent = tw.parent(level);
if (is_lhs(node, parent)
- || !immutable && parent instanceof AST_Call && parent.expression === node) {
+ || !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, level + 1);
+ return !immutable && is_modified(parent, read_property(value, parent.property), level + 1);
}
}
- function mark_escaped(d, node, level) {
+ 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;
}