aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2014-03-16 10:47:07 -0700
committerAlon Zakai <alonzakai@gmail.com>2014-03-17 17:56:17 -0700
commitdc70532c6a3f624f8b501d64f7e26d140da8827d (patch)
tree6e7ed156b3c272af95d322acaa7d5432e0ddc319
parentc4fc4453aacc8c1933e9cd256c04890978095003 (diff)
don't commaify if it doesn't help
-rw-r--r--tools/js-optimizer.js46
-rw-r--r--tools/test-js-optimizer-si-output.js8
-rw-r--r--tools/test-js-optimizer-si.js8
3 files changed, 36 insertions, 26 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index 4b63ab9b..e57bab0f 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -845,36 +845,30 @@ function simplifyIfs(ast) {
// recurse to handle chains
while (body[0] === 'block') {
var stats = body[1];
+ if (stats.length === 0) break;
+ var other = stats[stats.length-1];
+ if (other[0] !== 'if') break;
+ // we can handle elses, but must be fully identical
+ if (!astCompare(node[3], other[3])) break;
if (stats.length > 1) {
- var last = stats[stats.length-1];
- if (last[0] === 'if') {
- // try to commaify - turn everything between the ifs into a comma operator inside the second if
- var ok = true;
- for (var i = 0; i < stats.length-1; i++) {
- var curr = stats[i];
- if (curr[0] === 'stat') curr = curr[1];
- if (!(curr[0] in COMMABLE)) ok = false;
- }
- if (ok) {
- for (var i = stats.length-2; i >= 0; i--) {
- var curr = stats[i];
- if (curr[0] === 'stat') curr = curr[1];
- last[1] = ['seq', curr, last[1]];
- }
- stats = body[1] = [last];
- }
+ // try to commaify - turn everything between the ifs into a comma operator inside the second if
+ var ok = true;
+ for (var i = 0; i < stats.length-1; i++) {
+ var curr = stats[i];
+ if (curr[0] === 'stat') curr = curr[1];
+ if (!(curr[0] in COMMABLE)) ok = false;
}
+ if (!ok) break;
+ for (var i = stats.length-2; i >= 0; i--) {
+ var curr = stats[i];
+ if (curr[0] === 'stat') curr = curr[1];
+ other[1] = ['seq', curr, other[1]];
+ }
+ stats = body[1] = [other];
}
if (stats.length !== 1) break;
- var singleton = stats[0];
- // we can handle elses, but must be fully identical
- if (!astCompare(node[3], singleton[3])) break;
- if (singleton[0] === 'if') {
- node[1] = ['conditional', node[1], singleton[1], ['num', 0]];
- body = node[2] = singleton[2];
- } else {
- break;
- }
+ node[1] = ['conditional', node[1], other[1], ['num', 0]];
+ body = node[2] = other[2];
}
}
});
diff --git a/tools/test-js-optimizer-si-output.js b/tools/test-js-optimizer-si-output.js
index 8b93803d..97d7fe83 100644
--- a/tools/test-js-optimizer-si-output.js
+++ b/tools/test-js-optimizer-si-output.js
@@ -95,5 +95,13 @@ function a() {
} else {
label = 5;
}
+ if (x) {
+ a = 5;
+ if (y) {
+ f();
+ }
+ } else {
+ label = 5;
+ }
}
diff --git a/tools/test-js-optimizer-si.js b/tools/test-js-optimizer-si.js
index 6952b183..def2b5d9 100644
--- a/tools/test-js-optimizer-si.js
+++ b/tools/test-js-optimizer-si.js
@@ -117,5 +117,13 @@ function a() {
} else {
label = 5;
}
+ if (x) {
+ a = 5; // do not commify me
+ if (y) {
+ f();
+ }
+ } else {
+ label = 5;
+ }
}