aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2018-01-17 13:58:27 +0800
committerGitHub <noreply@github.com>2018-01-17 13:58:27 +0800
commit79cfac77bdee04021db9f60111f55005919f7b67 (patch)
treecb15ad41971a9825ea40e26631dfa2aa8cbf8836 /lib
parent224c14d49d0f007af641b8c7d358814634ea7c5f (diff)
downloadtracifyjs-79cfac77bdee04021db9f60111f55005919f7b67.tar.gz
tracifyjs-79cfac77bdee04021db9f60111f55005919f7b67.zip
extend `join_vars` & `sequences` (#2798)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js88
1 files changed, 49 insertions, 39 deletions
diff --git a/lib/compress.js b/lib/compress.js
index cd5651a2..5fd49cf1 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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);
}
}
@@ -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) {
@@ -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) {
@@ -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;
+ }
}
}