Skip to content

Commit

Permalink
fix: short-circuiting not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
LuanRT committed Aug 25, 2022
1 parent 475fc89 commit 8b610d1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/nodes/LogicalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ export default class LogicalExpression {
static visit(node: any, visitor: Visitor) {
const operator: string = node.operator;

if (node.operator !== '&&' && node.operator !== '||')
throw Error(`Unsupported logical operator: ${operator}`);

switch (operator) {
case '&&': {
const left_side = visitor.visitNode(node.left);
return left_side && visitor.visitNode(node.right);
if (left_side === true)
return visitor.visitNode(node.right);
return left_side;
}
case '||': {
const left_side = visitor.visitNode(node.left);
return left_side || visitor.visitNode(node.right);
}
default:
throw Error(`Unsupported logical operator: ${operator}`);
}
}
}

0 comments on commit 8b610d1

Please sign in to comment.