Skip to content

Commit

Permalink
extend join_vars & sequences (#2798)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jan 17, 2018
1 parent 224c14d commit 79cfac7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 43 deletions.
88 changes: 49 additions & 39 deletions lib/compress.js
Expand Up @@ -1662,37 +1662,34 @@ merge(Compressor.prototype, {
for (var i = 0; i < statements.length; i++) {
var stat = statements[i];
if (prev) {
if (stat instanceof AST_For && !(stat.init instanceof AST_Definitions)) {
var abort = false;
prev.body.walk(new TreeWalker(function(node) {
if (abort || node instanceof AST_Scope) return true;
if (node instanceof AST_Binary && node.operator == "in") {
abort = true;
return true;
}
}));
if (!abort) {
if (stat.init) stat.init = cons_seq(stat.init);
else {
stat.init = prev.body;
n--;
CHANGED = true;
if (stat instanceof AST_Exit) {
stat.value = cons_seq(stat.value || make_node(AST_Undefined, stat).transform(compressor));
} else if (stat instanceof AST_For) {
if (!(stat.init instanceof AST_Definitions)) {
var abort = false;
prev.body.walk(new TreeWalker(function(node) {
if (abort || node instanceof AST_Scope) return true;
if (node instanceof AST_Binary && node.operator == "in") {
abort = true;
return true;
}
}));
if (!abort) {
if (stat.init) stat.init = cons_seq(stat.init);
else {
stat.init = prev.body;
n--;
CHANGED = true;
}
}
}
}
else if (stat instanceof AST_If) {
} else if (stat instanceof AST_ForIn) {
stat.object = cons_seq(stat.object);
} else if (stat instanceof AST_If) {
stat.condition = cons_seq(stat.condition);
}
else if (stat instanceof AST_With) {
} else if (stat instanceof AST_Switch) {
stat.expression = cons_seq(stat.expression);
}
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) {
} else if (stat instanceof AST_With) {
stat.expression = cons_seq(stat.expression);
}
}
Expand Down Expand Up @@ -1775,18 +1772,7 @@ merge(Compressor.prototype, {
defs = stat;
}
} else if (stat instanceof AST_Exit) {
var exprs = join_object_assignments(prev, 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;
stat.value = extract_object_assignments(stat.value);
} else if (stat instanceof AST_For) {
var exprs = join_object_assignments(prev, stat.init);
if (exprs) {
Expand All @@ -1808,6 +1794,10 @@ merge(Compressor.prototype, {
} else {
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) {
var exprs = join_object_assignments(prev, stat.body);
if (exprs) {
Expand All @@ -1816,11 +1806,31 @@ merge(Compressor.prototype, {
stat.body = make_sequence(stat.body, exprs);
}
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 {
statements[++j] = stat;
}
}
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;
}
}
}

Expand Down
48 changes: 44 additions & 4 deletions test/compress/properties.js
Expand Up @@ -1189,7 +1189,7 @@ join_object_assignments_3: {
expect_stdout: "PASS"
}

join_object_assignments_4: {
join_object_assignments_return_1: {
options = {
join_vars: true,
}
Expand All @@ -1213,7 +1213,7 @@ join_object_assignments_4: {
expect_stdout: "foo"
}

join_object_assignments_5: {
join_object_assignments_return_2: {
options = {
join_vars: true,
}
Expand All @@ -1239,7 +1239,7 @@ join_object_assignments_5: {
expect_stdout: "bar"
}

join_object_assignments_6: {
join_object_assignments_return_3: {
options = {
join_vars: true,
}
Expand Down Expand Up @@ -1271,7 +1271,7 @@ join_object_assignments_6: {
]
}

join_object_assignments_7: {
join_object_assignments_for: {
options = {
join_vars: true,
}
Expand All @@ -1298,3 +1298,43 @@ join_object_assignments_7: {
"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"
}
18 changes: 18 additions & 0 deletions test/compress/sequences.js
Expand Up @@ -859,3 +859,21 @@ for_init_var: {
}
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"
}

0 comments on commit 79cfac7

Please sign in to comment.