[Pkg-javascript-commits] [uglifyjs] 435/491: extend `join_vars` & `sequences` (#2798)
Jonas Smedegaard
dr at jones.dk
Wed Feb 14 19:52:01 UTC 2018
This is an automated email from the git hooks/post-receive script.
js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.
commit 79cfac77bdee04021db9f60111f55005919f7b67
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date: Wed Jan 17 13:58:27 2018 +0800
extend `join_vars` & `sequences` (#2798)
---
lib/compress.js | 88 +++++++++++++++++++++++++--------------------
test/compress/properties.js | 48 ++++++++++++++++++++++---
test/compress/sequences.js | 18 ++++++++++
3 files changed, 111 insertions(+), 43 deletions(-)
diff --git a/lib/compress.js b/lib/compress.js
index cd5651a..5fd49cf 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;
+ }
}
}
diff --git a/test/compress/properties.js b/test/compress/properties.js
index 7df53d3..933774d 100644
--- a/test/compress/properties.js
+++ b/test/compress/properties.js
@@ -1189,7 +1189,7 @@ join_object_assignments_3: {
expect_stdout: "PASS"
}
-join_object_assignments_4: {
+join_object_assignments_return_1: {
options = {
join_vars: true,
}
@@ -1213,7 +1213,7 @@ join_object_assignments_4: {
expect_stdout: "foo"
}
-join_object_assignments_5: {
+join_object_assignments_return_2: {
options = {
join_vars: true,
}
@@ -1239,7 +1239,7 @@ join_object_assignments_5: {
expect_stdout: "bar"
}
-join_object_assignments_6: {
+join_object_assignments_return_3: {
options = {
join_vars: true,
}
@@ -1271,7 +1271,7 @@ join_object_assignments_6: {
]
}
-join_object_assignments_7: {
+join_object_assignments_for: {
options = {
join_vars: true,
}
@@ -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"
+}
diff --git a/test/compress/sequences.js b/test/compress/sequences.js
index 3d12fb0..2c90079 100644
--- a/test/compress/sequences.js
+++ b/test/compress/sequences.js
@@ -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"
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git
More information about the Pkg-javascript-commits
mailing list