extend join_vars & sequences (#2798)

This commit is contained in:
Alex Lam S.L
2018-01-17 13:58:27 +08:00
committed by GitHub
parent 224c14d49d
commit 79cfac77bd
3 changed files with 111 additions and 43 deletions

View File

@@ -1662,37 +1662,34 @@ merge(Compressor.prototype, {
for (var i = 0; i < statements.length; i++) { for (var i = 0; i < statements.length; i++) {
var stat = statements[i]; var stat = statements[i];
if (prev) { if (prev) {
if (stat instanceof AST_For && !(stat.init instanceof AST_Definitions)) { if (stat instanceof AST_Exit) {
var abort = false; stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat).transform(compressor));
prev.body.walk(new TreeWalker(function(node) { } else if (stat instanceof AST_For) {
if (abort || node instanceof AST_Scope) return true; if (!(stat.init instanceof AST_Definitions)) {
if (node instanceof AST_Binary && node.operator == "in") { var abort = false;
abort = true; prev.body.walk(new TreeWalker(function(node) {
return true; if (abort || node instanceof AST_Scope) return true;
} if (node instanceof AST_Binary && node.operator == "in") {
})); abort = true;
if (!abort) { return true;
if (stat.init) stat.init = cons_seq(stat.init); }
else { }));
stat.init = prev.body; if (!abort) {
n--; if (stat.init) stat.init = cons_seq(stat.init);
CHANGED = true; else {
stat.init = prev.body;
n--;
CHANGED = true;
}
} }
} }
} } else if (stat instanceof AST_ForIn) {
else if (stat instanceof AST_If) { stat.object = cons_seq(stat.object);
} else if (stat instanceof AST_If) {
stat.condition = cons_seq(stat.condition); stat.condition = cons_seq(stat.condition);
} } else if (stat instanceof AST_Switch) {
else if (stat instanceof AST_With) {
stat.expression = cons_seq(stat.expression); stat.expression = cons_seq(stat.expression);
} } else if (stat instanceof AST_With) {
else if (stat instanceof AST_Exit && stat.value) {
stat.value = cons_seq(stat.value);
}
else if (stat instanceof AST_Exit) {
stat.value = cons_seq(make_node(AST_Undefined, stat).transform(compressor));
}
else if (stat instanceof AST_Switch) {
stat.expression = cons_seq(stat.expression); stat.expression = cons_seq(stat.expression);
} }
} }
@@ -1775,18 +1772,7 @@ merge(Compressor.prototype, {
defs = stat; defs = stat;
} }
} else if (stat instanceof AST_Exit) { } else if (stat instanceof AST_Exit) {
var exprs = join_object_assignments(prev, stat.value); stat.value = extract_object_assignments(stat.value);
if (exprs) {
CHANGED = true;
if (exprs.length) {
stat.value = make_sequence(stat.value, exprs);
} else if (stat.value instanceof AST_Sequence) {
stat.value = stat.value.tail_node().left;
} else {
stat.value = stat.value.left;
}
}
statements[++j] = stat;
} else if (stat instanceof AST_For) { } else if (stat instanceof AST_For) {
var exprs = join_object_assignments(prev, stat.init); var exprs = join_object_assignments(prev, stat.init);
if (exprs) { if (exprs) {
@@ -1808,6 +1794,10 @@ merge(Compressor.prototype, {
} else { } else {
statements[++j] = stat; statements[++j] = stat;
} }
} else if (stat instanceof AST_ForIn) {
stat.object = extract_object_assignments(stat.object);
} else if (stat instanceof AST_If) {
stat.condition = extract_object_assignments(stat.condition);
} else if (stat instanceof AST_SimpleStatement) { } else if (stat instanceof AST_SimpleStatement) {
var exprs = join_object_assignments(prev, stat.body); var exprs = join_object_assignments(prev, stat.body);
if (exprs) { if (exprs) {
@@ -1816,11 +1806,31 @@ merge(Compressor.prototype, {
stat.body = make_sequence(stat.body, exprs); stat.body = make_sequence(stat.body, exprs);
} }
statements[++j] = stat; statements[++j] = stat;
} else if (stat instanceof AST_Switch) {
stat.expression = extract_object_assignments(stat.expression);
} else if (stat instanceof AST_With) {
stat.expression = extract_object_assignments(stat.expression);
} else { } else {
statements[++j] = stat; statements[++j] = stat;
} }
} }
statements.length = j + 1; statements.length = j + 1;
function extract_object_assignments(value) {
statements[++j] = stat;
var exprs = join_object_assignments(prev, value);
if (exprs) {
CHANGED = true;
if (exprs.length) {
return make_sequence(value, exprs);
} else if (value instanceof AST_Sequence) {
return value.tail_node().left;
} else {
return value.left;
}
}
return value;
}
} }
} }

View File

@@ -1189,7 +1189,7 @@ join_object_assignments_3: {
expect_stdout: "PASS" expect_stdout: "PASS"
} }
join_object_assignments_4: { join_object_assignments_return_1: {
options = { options = {
join_vars: true, join_vars: true,
} }
@@ -1213,7 +1213,7 @@ join_object_assignments_4: {
expect_stdout: "foo" expect_stdout: "foo"
} }
join_object_assignments_5: { join_object_assignments_return_2: {
options = { options = {
join_vars: true, join_vars: true,
} }
@@ -1239,7 +1239,7 @@ join_object_assignments_5: {
expect_stdout: "bar" expect_stdout: "bar"
} }
join_object_assignments_6: { join_object_assignments_return_3: {
options = { options = {
join_vars: true, join_vars: true,
} }
@@ -1271,7 +1271,7 @@ join_object_assignments_6: {
] ]
} }
join_object_assignments_7: { join_object_assignments_for: {
options = { options = {
join_vars: true, join_vars: true,
} }
@@ -1298,3 +1298,43 @@ join_object_assignments_7: {
"3", "3",
] ]
} }
join_object_assignments_if: {
options = {
join_vars: true,
}
input: {
console.log(function() {
var o = {};
if (o.a = "PASS") return o.a;
}())
}
expect: {
console.log(function() {
var o = { a: "PASS" };
if (o.a) return o.a;
}());
}
expect_stdout: "PASS"
}
join_object_assignments_forin: {
options = {
join_vars: true,
}
input: {
console.log(function() {
var o = {};
for (var a in o.a = "PASS", o)
return o[a];
}())
}
expect: {
console.log(function() {
var o = { a: "PASS" };
for (var a in o)
return o[a];
}());
}
expect_stdout: "PASS"
}

View File

@@ -859,3 +859,21 @@ for_init_var: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
forin: {
options = {
sequences: true,
}
input: {
var o = [];
o.push("PASS");
for (var a in o)
console.log(o[a]);
}
expect: {
var o = [];
for (var a in o.push("PASS"), o)
console.log(o[a]);
}
expect_stdout: "PASS"
}