From 324c592832b506b7f842973ac6074761c79cc951 Mon Sep 17 00:00:00 2001 From: LalitNarayanYadav Date: Thu, 23 Apr 2026 15:25:59 +0530 Subject: [PATCH] Refactor: Deduplicate BinaryExpression and LogicalExpression transformation logic --- src/strands/strands_transpiler.js | 99 +++++++++++-------------------- 1 file changed, 33 insertions(+), 66 deletions(-) diff --git a/src/strands/strands_transpiler.js b/src/strands/strands_transpiler.js index b30c3380e2..abf03ba3d6 100644 --- a/src/strands/strands_transpiler.js +++ b/src/strands/strands_transpiler.js @@ -235,6 +235,35 @@ function replaceReferences(node, tempVarMap) { internalReplaceReferences(node); } +// Shared handler for both BinaryExpression and LogicalExpression — +// both follow the same operator-to-method-call transformation pattern. +function transformBinaryOrLogical(node, state, ancestors) { + if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { + return; + } + const unsafeTypes = ['Literal', 'ArrayExpression', 'Identifier']; + if (unsafeTypes.includes(node.left.type)) { + node.left = { + type: 'CallExpression', + callee: { + type: 'Identifier', + name: '__p5.strandsNode', + }, + arguments: [node.left] + }; + } + node.type = 'CallExpression'; + node.callee = { + type: 'MemberExpression', + object: node.left, + property: { + type: 'Identifier', + name: replaceBinaryOperator(node.operator), + }, + }; + node.arguments = [node.right]; +} + const ASTCallbacks = { UnaryExpression(node, state, ancestors) { if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { @@ -509,72 +538,10 @@ const ASTCallbacks = { } } }, - BinaryExpression(node, state, ancestors) { - // Don't convert uniform default values to node methods, as - // they should be evaluated at runtime, not compiled. - if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { - return; - } - // If the left hand side of an expression is one of these types, - // we should construct a node from it. - const unsafeTypes = ['Literal', 'ArrayExpression', 'Identifier']; - if (unsafeTypes.includes(node.left.type)) { - const leftReplacementNode = { - type: 'CallExpression', - callee: { - type: 'Identifier', - name: '__p5.strandsNode', - }, - arguments: [node.left] - } - node.left = leftReplacementNode; - } - // Replace the binary operator with a call expression - // in other words a call to BaseNode.mult(), .div() etc. - node.type = 'CallExpression'; - node.callee = { - type: 'MemberExpression', - object: node.left, - property: { - type: 'Identifier', - name: replaceBinaryOperator(node.operator), - }, - }; - node.arguments = [node.right]; - }, - LogicalExpression(node, state, ancestors) { - // Don't convert uniform default values to node methods, as - // they should be evaluated at runtime, not compiled. - if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { - return; - } - // If the left hand side of an expression is one of these types, - // we should construct a node from it. - const unsafeTypes = ['Literal', 'ArrayExpression', 'Identifier']; - if (unsafeTypes.includes(node.left.type)) { - const leftReplacementNode = { - type: 'CallExpression', - callee: { - type: 'Identifier', - name: '__p5.strandsNode', - }, - arguments: [node.left] - } - node.left = leftReplacementNode; - } - // Replace the logical operator with a call expression - // in other words a call to BaseNode.or(), .and() etc. - node.type = 'CallExpression'; - node.callee = { - type: 'MemberExpression', - object: node.left, - property: { - type: 'Identifier', - name: replaceBinaryOperator(node.operator), - }, - }; - node.arguments = [node.right]; - }, + BinaryExpression: transformBinaryOrLogical, + LogicalExpression: transformBinaryOrLogical, + + ConditionalExpression(node, state, ancestors) { if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) { return;