Skip to content

Commit 3eb2207

Browse files
committed
Implement short circuit evaluation
1 parent 16f9c46 commit 3eb2207

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,22 @@ module.exports = function (ast, vars) {
4040
}
4141
else if (node.type === 'BinaryExpression' ||
4242
node.type === 'LogicalExpression') {
43+
var op = node.operator;
44+
45+
if (op === '&&') {
46+
var l = walk(node.left);
47+
if (l === FAIL) return FAIL;
48+
if (!l) return l;
49+
var r = walk(node.right);
50+
if (r === FAIL) return FAIL;
51+
return r;
52+
}
53+
4354
var l = walk(node.left);
4455
if (l === FAIL) return FAIL;
4556
var r = walk(node.right);
4657
if (r === FAIL) return FAIL;
4758

48-
var op = node.operator;
4959
if (op === '==') return l == r;
5060
if (op === '===') return l === r;
5161
if (op === '!=') return l != r;
@@ -62,7 +72,6 @@ module.exports = function (ast, vars) {
6272
if (op === '|') return l | r;
6373
if (op === '&') return l & r;
6474
if (op === '^') return l ^ r;
65-
if (op === '&&') return l && r;
6675
if (op === '||') return l || r;
6776

6877
return FAIL;

0 commit comments

Comments
 (0)