aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/test_other.py15
-rw-r--r--tools/js-optimizer.js35
2 files changed, 47 insertions, 3 deletions
diff --git a/tests/test_other.py b/tests/test_other.py
index 88a5c9c5..19d1790d 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -2637,3 +2637,18 @@ int main()
}
''', [6, 3, 3])
+ test(r'''
+ #include <stdio.h>
+ #include <stdlib.h>
+
+ int main(int argc, char *argv[]) {
+ if (getenv("A") && getenv("B")) {
+ printf("hello world\n");
+ } else {
+ printf("goodnight moon\n");
+ }
+ printf("and that's that\n");
+ return 0;
+ }
+ ''', [3, 1, 1])
+
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index d5b30476..88bcf503 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -850,9 +850,34 @@ function simplifyIfs(ast) {
var stats = body[1];
if (stats.length === 0) break;
var other = stats[stats.length-1];
- if (other[0] !== 'if') break;
+ if (other[0] !== 'if') {
+ // our if block does not end with an if. perhaps if have an else we can flip
+ if (node[3] && node[3][0] === 'block') {
+ var stats = node[3][1];
+ if (stats.length === 0) break;
+ var other = stats[stats.length-1];
+ if (other[0] === 'if') {
+ // flip node
+ node[1] = flipCondition(node[1]);
+ node[2] = node[3];
+ node[3] = body;
+ } else break;
+ } else break;
+ }
// we can handle elses, but must be fully identical
- if (!astCompare(node[3], other[3])) break;
+ if (node[3] || other[3]) {
+ if (!node[3] || !other[3]) break;
+ if (!astCompare(node[3], other[3])) {
+ // the elses are different, but perhaps if we flipped a condition we can do better
+ if (astCompare(node[3], other[2])) {
+ // flip other
+ other[1] = flipCondition(other[1]);
+ var temp = other[2];
+ other[2] = other[3];
+ other[3] = temp;
+ } else break;
+ }
+ }
if (stats.length > 1) {
// try to commaify - turn everything between the ifs into a comma operator inside the second if
var ok = true;
@@ -1323,6 +1348,10 @@ function simplifyNotCompsDirect(node) {
if (!simplifyNotCompsPass) return node;
}
+function flipCondition(cond) {
+ return simplifyNotCompsDirect(['unary-prefix', '!', cond]);
+}
+
var simplifyNotCompsPass = false;
function simplifyNotComps(ast) {
@@ -1589,7 +1618,7 @@ function hoistMultiples(ast) {
var temp = node[3];
node[3] = node[2];
node[2] = temp;
- node[1] = simplifyNotCompsDirect(['unary-prefix', '!', node[1]]);
+ node[1] = flipCondition(node[1]);
stat1 = node[2][1];
stat2 = node[3][1];
}