Skip to content

Commit 7478389

Browse files
tx2002nodece
andauthored
feat: fix the logic short-circuit bug (#8)
* feat: fix the logic short-circuit bug * fix: apply suggestions from code review Co-authored-by: Zixuan Liu <[email protected]> --------- Co-authored-by: Zixuan Liu <[email protected]>
1 parent 36d0f7b commit 7478389

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

Diff for: .eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"@typescript-eslint/no-unused-vars": ["warn", {"argsIgnorePattern": "^_"}],
2424
"quotes": ["warn", "single"],
2525
"max-len": ["warn", {"code": 100, "tabWidth": 4, "ignoreUrls": true, "ignorePattern": "^import|^export"}],
26-
"newline-per-chained-call": [2, {"ignoreChainWithDepth": 3}]
26+
"newline-per-chained-call": [2, {"ignoreChainWithDepth": 3}],
27+
"no-case-declarations": "off"
2728
},
2829
"ignorePatterns": ["**/*.js"]
2930
}

Diff for: index.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ function evaluate(_node: jsep.Expression, context: object) {
150150
return node.value;
151151

152152
case 'LogicalExpression':
153-
if (node.operator === '||') {
154-
return evaluate(node.left, context) || evaluate(node.right, context);
155-
} else if (node.operator === '&&') {
156-
return evaluate(node.left, context) && evaluate(node.right, context);
153+
const leftValue = evaluate(node.left, context);
154+
if (node.operator === '||' && leftValue) {
155+
return leftValue;
156+
} else if (node.operator === '&&' && !leftValue) {
157+
return leftValue;
157158
}
158-
return binops[node.operator](evaluate(node.left, context), evaluate(node.right, context));
159+
return binops[node.operator](leftValue, evaluate(node.right, context));
159160

160161
case 'MemberExpression':
161162
return evaluateMember(node, context)[1];
@@ -222,15 +223,17 @@ async function evalAsync(_node: jsep.Expression, context: object) {
222223

223224
case 'LogicalExpression': {
224225
if (node.operator === '||') {
225-
return (
226-
(await evalAsync(node.left, context)) ||
227-
(await evalAsync(node.right, context))
228-
);
226+
const left = await evalAsync(node.left, context);
227+
if (left) {
228+
return left;
229+
}
230+
return await evalAsync(node.right, context);
229231
} else if (node.operator === '&&') {
230-
return (
231-
(await evalAsync(node.left, context)) &&
232-
(await evalAsync(node.right, context))
233-
);
232+
const left = await evalAsync(node.left, context);
233+
if (!left) {
234+
return left;
235+
}
236+
return await evalAsync(node.right, context);
234237
}
235238

236239
const [left, right] = await Promise.all([

Diff for: test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ const fixtures = [
6262
{expr: '1 >= 2', expected: false },
6363

6464
// logical expression lazy evaluation
65-
{expr: 'true || false', expected: true },
65+
{expr: 'true || throw()', expected: true },
6666
{expr: 'false || true', expected: true },
67-
{expr: 'false && true', expected: false },
67+
{expr: 'false && throw()', expected: false },
6868
{expr: 'true && false', expected: false },
6969

7070
// member expression

0 commit comments

Comments
 (0)