Skip to content

Commit 569562c

Browse files
committed
Fix void context transform !cond && ok() => cond || ok()
Close #45
1 parent 8f8c610 commit 569562c

9 files changed

+44
-7
lines changed

Diff for: lib/pass/remove-context-sensitive-expressions.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Copyright (C) 2012 Mihai Bazon <[email protected]>
23
Copyright (C) 2012 Yusuke Suzuki <[email protected]>
34
45
Redistribution and use in source and binary forms, with or without
@@ -43,13 +44,20 @@
4344
continue;
4445
}
4546
} else if (expr.type === Syntax.LogicalExpression) {
46-
if (expr.left.type === Syntax.UnaryExpression) {
47-
if (expr.left.operator === '!') {
48-
// !cond && ok() => cond || ok()
49-
modified = true;
50-
expr.left = expr.left.argument;
51-
expr.operator = (expr.operator === '||') ? '&&' : '||';
52-
}
47+
if (expr.left.type === Syntax.UnaryExpression && expr.left.operator === '!' &&
48+
expr.right.type === Syntax.UnaryExpression && expr.right.operator === '!') {
49+
// !cond && !ok() => !(cond || ok())
50+
// this introduces more optimizations
51+
modified = true;
52+
expr.left = expr.left.argument;
53+
expr.right = expr.right.argument;
54+
expr.operator = (expr.operator === '||') ? '&&' : '||';
55+
expr = common.moveLocation(expr, {
56+
type: Syntax.UnaryExpression,
57+
operator: '!',
58+
argument: expr
59+
});
60+
continue;
5361
}
5462
} else if (expr.type === Syntax.ConditionalExpression) {
5563
if (expr.test.type === Syntax.UnaryExpression && expr.test.operator === '!') {
@@ -74,6 +82,13 @@
7482
expr = expr.argument;
7583
continue;
7684
}
85+
} else if (expr.type === Syntax.LogicalExpression) {
86+
if (expr.left.type === Syntax.UnaryExpression && expr.left.operator === '!') {
87+
// !cond && ok() => cond || ok()
88+
modified = true;
89+
expr.left = expr.left.argument;
90+
expr.operator = (expr.operator === '||') ? '&&' : '||';
91+
}
7792
}
7893
break;
7994
} while (true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!ok||ok2()
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Because of global environment
2+
if (!ok || ok2());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(function(){ok&&ok2()}())
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(function () {
2+
if (!ok || ok2());
3+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(function(){while(!ok||ok2())ok3()}())
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(function () {
2+
while (!ok || ok2()) {
3+
ok3();
4+
}
5+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(function(){while(!(ok&&ok2()))ok3()}())
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// TODO(Constellation):
2+
// This transformation sometimes make script bigger size.
3+
// So we should handle it in post processing pass.
4+
(function () {
5+
while (!ok || !ok2()) {
6+
ok3();
7+
}
8+
}());

0 commit comments

Comments
 (0)