diff --git a/packages/compiler/react/evaluator.ts b/packages/compiler/react/evaluator.ts index 2b4bedd971..afd848e165 100644 --- a/packages/compiler/react/evaluator.ts +++ b/packages/compiler/react/evaluator.ts @@ -160,25 +160,10 @@ export const evaluateAstNode = ( } if (t.isBinaryExpression(ast)) { - if (ast.operator === '+') { - const left = evaluateAstNode(ast.left, staticContext) as number; - const right = evaluateAstNode(ast.right, staticContext) as number; - return left + right; - } else if (ast.operator === '-') { - return ( - evaluateAstNode(ast.left, staticContext) - - evaluateAstNode(ast.right, staticContext) - ); - } else if (ast.operator === '*') { - return ( - evaluateAstNode(ast.left, staticContext) * - evaluateAstNode(ast.right, staticContext) - ); - } else if (ast.operator === '/') { - return ( - evaluateAstNode(ast.left, staticContext) / - evaluateAstNode(ast.right, staticContext) - ); + const left = evaluateAstNode(ast.left, staticContext); + const right = evaluateAstNode(ast.right, staticContext); + if (typeof left === 'number' && typeof right === 'number') { + return evaluateBinaryExpression(left, right, ast.operator); } } @@ -210,3 +195,40 @@ export const valueToAst = (value: unknown) => { throw new Error(`Cannot convert value to AST: ${String(value)}`); } }; + +function evaluateBinaryExpression( + left: number, + right: number, + operator: t.BinaryExpression['operator'], +): any { + switch (operator) { + case '+': + return left + right; + case '-': + return left - right; + case '*': + return left * right; + case '/': + return left / right; + case '<': + return left < right; + case '>': + return left > right; + case '<=': + return left <= right; + case '>=': + return left >= right; + case '==': + // eslint-disable-next-line eqeqeq + return left == right; + case '!=': + // eslint-disable-next-line eqeqeq + return left != right; + case '===': + return left === right; + case '!==': + return left !== right; + default: + return undefined; + } +}