Skip to content

Commit

Permalink
feat: fix the logic short-circuit bug (#8)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
tx2002 and nodece authored Nov 19, 2024
1 parent 36d0f7b commit 7478389
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@typescript-eslint/no-unused-vars": ["warn", {"argsIgnorePattern": "^_"}],
"quotes": ["warn", "single"],
"max-len": ["warn", {"code": 100, "tabWidth": 4, "ignoreUrls": true, "ignorePattern": "^import|^export"}],
"newline-per-chained-call": [2, {"ignoreChainWithDepth": 3}]
"newline-per-chained-call": [2, {"ignoreChainWithDepth": 3}],
"no-case-declarations": "off"
},
"ignorePatterns": ["**/*.js"]
}
29 changes: 16 additions & 13 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,13 @@ function evaluate(_node: jsep.Expression, context: object) {
return node.value;

case 'LogicalExpression':
if (node.operator === '||') {
return evaluate(node.left, context) || evaluate(node.right, context);
} else if (node.operator === '&&') {
return evaluate(node.left, context) && evaluate(node.right, context);
const leftValue = evaluate(node.left, context);
if (node.operator === '||' && leftValue) {
return leftValue;
} else if (node.operator === '&&' && !leftValue) {
return leftValue;
}
return binops[node.operator](evaluate(node.left, context), evaluate(node.right, context));
return binops[node.operator](leftValue, evaluate(node.right, context));

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

case 'LogicalExpression': {
if (node.operator === '||') {
return (
(await evalAsync(node.left, context)) ||
(await evalAsync(node.right, context))
);
const left = await evalAsync(node.left, context);
if (left) {
return left;
}
return await evalAsync(node.right, context);
} else if (node.operator === '&&') {
return (
(await evalAsync(node.left, context)) &&
(await evalAsync(node.right, context))
);
const left = await evalAsync(node.left, context);
if (!left) {
return left;
}
return await evalAsync(node.right, context);
}

const [left, right] = await Promise.all([
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ const fixtures = [
{expr: '1 >= 2', expected: false },

// logical expression lazy evaluation
{expr: 'true || false', expected: true },
{expr: 'true || throw()', expected: true },
{expr: 'false || true', expected: true },
{expr: 'false && true', expected: false },
{expr: 'false && throw()', expected: false },
{expr: 'true && false', expected: false },

// member expression
Expand Down

0 comments on commit 7478389

Please sign in to comment.