From 0707d490059208ba2139679a7b00f4188abbb91a Mon Sep 17 00:00:00 2001 From: poteboy Date: Sun, 20 Aug 2023 13:34:41 +0900 Subject: [PATCH 1/2] Add support for additional binary operations in JSX evaluation --- packages/compiler/react/evaluator.ts | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/packages/compiler/react/evaluator.ts b/packages/compiler/react/evaluator.ts index 2b4bedd971..161a821e56 100644 --- a/packages/compiler/react/evaluator.ts +++ b/packages/compiler/react/evaluator.ts @@ -179,6 +179,48 @@ export const evaluateAstNode = ( 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) + ); + } 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 ( + // eslint-disable-next-line eqeqeq + evaluateAstNode(ast.left, staticContext) == + evaluateAstNode(ast.right, staticContext) + ); + } else if (ast.operator === '!=') { + return ( + // eslint-disable-next-line eqeqeq + 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) + ); } } From 11f3c85c65f884b04a9c4cc58c85683ee841a14e Mon Sep 17 00:00:00 2001 From: poteboy Date: Sun, 20 Aug 2023 14:31:45 +0900 Subject: [PATCH 2/2] refactor: Extract binary expression evaluation to its own function --- packages/compiler/react/evaluator.ts | 102 +++++++++++---------------- 1 file changed, 41 insertions(+), 61 deletions(-) diff --git a/packages/compiler/react/evaluator.ts b/packages/compiler/react/evaluator.ts index 161a821e56..afd848e165 100644 --- a/packages/compiler/react/evaluator.ts +++ b/packages/compiler/react/evaluator.ts @@ -160,67 +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) - ); - } 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) - ); - } else if (ast.operator === '>=') { - return ( - evaluateAstNode(ast.left, staticContext) >= - evaluateAstNode(ast.right, staticContext) - ); - } else if (ast.operator === '==') { - return ( - // eslint-disable-next-line eqeqeq - evaluateAstNode(ast.left, staticContext) == - evaluateAstNode(ast.right, staticContext) - ); - } else if (ast.operator === '!=') { - return ( - // eslint-disable-next-line eqeqeq - 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); } } @@ -252,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; + } +}