fix object literal tracing in reduce_vars (#2461)

This commit is contained in:
Alex Lam S.L
2017-11-10 05:47:10 +08:00
committed by GitHub
parent 1127a2caf3
commit 94525d859f
2 changed files with 45 additions and 3 deletions

View File

@@ -609,8 +609,11 @@ merge(Compressor.prototype, {
&& parent.expression === node && parent.expression === node
&& (!(value instanceof AST_Function) || value.contains_this(parent))) { && (!(value instanceof AST_Function) || value.contains_this(parent))) {
return true; return true;
} else if (parent instanceof AST_Array || parent instanceof AST_Object) { } else if (parent instanceof AST_Array) {
return is_modified(parent, parent, level + 1); return is_modified(parent, parent, level + 1);
} else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
var obj = tw.parent(level + 1);
return is_modified(obj, obj, level + 2);
} else if (parent instanceof AST_PropAccess && parent.expression === node) { } else if (parent instanceof AST_PropAccess && parent.expression === node) {
return !immutable && is_modified(parent, read_property(value, parent.property), level + 1); return !immutable && is_modified(parent, read_property(value, parent.property), level + 1);
} }
@@ -626,8 +629,11 @@ merge(Compressor.prototype, {
|| parent instanceof AST_VarDef && node === parent.value) { || parent instanceof AST_VarDef && node === parent.value) {
d.escaped = true; d.escaped = true;
return; return;
} else if (parent instanceof AST_Array || parent instanceof AST_Object) { } else if (parent instanceof AST_Array) {
mark_escaped(d, parent, parent, level + 1); mark_escaped(d, parent, parent, level + 1);
} else if (parent instanceof AST_ObjectKeyVal && node === parent.value) {
var obj = tw.parent(level + 1);
mark_escaped(d, obj, obj, level + 2);
} else if (parent instanceof AST_PropAccess && node === parent.expression) { } else if (parent instanceof AST_PropAccess && node === parent.expression) {
value = read_property(value, parent.property); value = read_property(value, parent.property);
mark_escaped(d, parent, value, level + 1); mark_escaped(d, parent, value, level + 1);

View File

@@ -286,6 +286,7 @@ unsafe_evaluate_modified: {
function inc() { this.p++; } function inc() { this.p++; }
console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }()); console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }());
console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }()); console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }());
console.log(function(){ var o={p:8}; console.log({q:o}.q.p++); return o.p; }());
} }
expect: { expect: {
console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }()); console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }());
@@ -296,6 +297,7 @@ unsafe_evaluate_modified: {
function inc() { this.p++; } function inc() { this.p++; }
console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }()); console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }());
console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }()); console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }());
console.log(function(){ var o={p:8}; console.log({q:o}.q.p++); return o.p; }());
} }
expect_stdout: true expect_stdout: true
} }
@@ -3269,7 +3271,7 @@ const_expr_2: {
expect_stdout: "2 2" expect_stdout: "2 2"
} }
escaped_prop: { escaped_prop_1: {
options = { options = {
collapse_vars: true, collapse_vars: true,
evaluate: true, evaluate: true,
@@ -3298,6 +3300,40 @@ escaped_prop: {
expect_stdout: "2" expect_stdout: "2"
} }
escaped_prop_2: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a;
function f(b) {
if (a) console.log(a === b.c);
a = b.c;
}
function g() {}
function h() {
f({ c: g });
}
h();
h();
}
expect: {
var a;
function g() {}
function h() {
(function(b) {
if (a) console.log(a === b.c);
a = b.c;
})({ c: g });
}
h();
h();
}
expect_stdout: "true"
}
issue_2420_1: { issue_2420_1: {
options = { options = {
reduce_vars: true, reduce_vars: true,