Skip to content

feat: fix the logic short-circuit bug #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 Expand Up @@ -270,7 +273,7 @@
}

// Added functions to inject Custom Binary Operators (and override existing ones)
function addBinaryOp(operator: string, precedence_or_fn: number | binaryCallback, _function: binaryCallback): void {

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^16)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^16)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^18)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^18)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^20)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^20)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^16)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^16)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^18)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^18)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^20)

This line has a length of 116. Maximum allowed is 100

Check warning on line 276 in index.ts

View workflow job for this annotation

GitHub Actions / test (^20)

This line has a length of 116. Maximum allowed is 100
if (_function) {
jsep.addBinaryOp(operator, precedence_or_fn as number);
binops[operator] = _function;
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
Loading