Skip to content

Commit 70147fb

Browse files
authored
Merge pull request #8728 from LalitNarayanYadav/refactor/strands-transpiler-pipeline
Refactor: Extract replaceIdentifierReferences and remove reliance on `this` in ASTCallbacks
2 parents 373ad88 + 370b85e commit 70147fb

1 file changed

Lines changed: 31 additions & 32 deletions

File tree

src/strands/strands_transpiler.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,30 @@ function replaceReferences(node, tempVarMap) {
235235
internalReplaceReferences(node);
236236
}
237237

238+
function replaceIdentifierReferences(node, oldName, newName) {
239+
if (!node || typeof node !== 'object') return node;
240+
241+
const replaceInNode = (n) => {
242+
if (!n || typeof n !== 'object') return n;
243+
if (n.type === 'Identifier' && n.name === oldName) {
244+
return { ...n, name: newName };
245+
}
246+
const newNode = { ...n };
247+
for (const key in n) {
248+
if (n.hasOwnProperty(key) && key !== 'parent') {
249+
if (Array.isArray(n[key])) {
250+
newNode[key] = n[key].map(replaceInNode);
251+
} else if (typeof n[key] === 'object') {
252+
newNode[key] = replaceInNode(n[key]);
253+
}
254+
}
255+
}
256+
return newNode;
257+
};
258+
259+
return replaceInNode(node);
260+
}
261+
238262
// Shared handler for both BinaryExpression and LogicalExpression —
239263
// both follow the same operator-to-method-call transformation pattern.
240264
function transformBinaryOrLogical(node, state, ancestors) {
@@ -863,8 +887,8 @@ const ASTCallbacks = {
863887
// Replace the update expression with the assignment expression
864888
Object.assign(node, assignmentExpr);
865889
delete node.prefix;
866-
this.BinaryExpression(node.right, state, [...ancestors, node]);
867-
this.AssignmentExpression(node, state, ancestors);
890+
ASTCallbacks.BinaryExpression(node.right, state, [...ancestors, node]);
891+
ASTCallbacks.AssignmentExpression(node, state, ancestors);
868892
},
869893
ForStatement(node, state, ancestors) {
870894
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
@@ -921,7 +945,7 @@ const ASTCallbacks = {
921945
// Replace loop variable references with the parameter
922946
if (node.init?.type === 'VariableDeclaration') {
923947
const loopVarName = node.init.declarations[0].id.name;
924-
conditionBody = this.replaceIdentifierReferences(conditionBody, loopVarName, uniqueLoopVar);
948+
conditionBody = replaceIdentifierReferences(conditionBody, loopVarName, uniqueLoopVar);
925949
}
926950
const conditionAst = { type: 'Program', body: [{ type: 'ExpressionStatement', expression: conditionBody }] };
927951
conditionBody = conditionAst.body[0].expression;
@@ -939,7 +963,7 @@ const ASTCallbacks = {
939963
// Replace loop variable references with the parameter
940964
if (node.init?.type === 'VariableDeclaration') {
941965
const loopVarName = node.init.declarations[0].id.name;
942-
updateExpr = this.replaceIdentifierReferences(updateExpr, loopVarName, uniqueLoopVar);
966+
updateExpr = replaceIdentifierReferences(updateExpr, loopVarName, uniqueLoopVar);
943967
}
944968
const updateAst = { type: 'Program', body: [{ type: 'ExpressionStatement', expression: updateExpr }] };
945969
updateExpr = updateAst.body[0].expression;
@@ -978,7 +1002,7 @@ const ASTCallbacks = {
9781002
// Replace loop variable references in the body
9791003
if (node.init?.type === 'VariableDeclaration') {
9801004
const loopVarName = node.init.declarations[0].id.name;
981-
bodyBlock = this.replaceIdentifierReferences(bodyBlock, loopVarName, uniqueLoopVar);
1005+
bodyBlock = replaceIdentifierReferences(bodyBlock, loopVarName, uniqueLoopVar);
9821006
}
9831007

9841008
const bodyFunction = {
@@ -1214,33 +1238,8 @@ const ASTCallbacks = {
12141238
delete node.update;
12151239
},
12161240

1217-
// Helper method to replace identifier references in AST nodes
1218-
replaceIdentifierReferences(node, oldName, newName) {
1219-
if (!node || typeof node !== 'object') return node;
1220-
1221-
const replaceInNode = (n) => {
1222-
if (!n || typeof n !== 'object') return n;
1223-
1224-
if (n.type === 'Identifier' && n.name === oldName) {
1225-
return { ...n, name: newName };
1226-
}
1227-
1228-
// Create a copy and recursively process properties
1229-
const newNode = { ...n };
1230-
for (const key in n) {
1231-
if (n.hasOwnProperty(key) && key !== 'parent') {
1232-
if (Array.isArray(n[key])) {
1233-
newNode[key] = n[key].map(replaceInNode);
1234-
} else if (typeof n[key] === 'object') {
1235-
newNode[key] = replaceInNode(n[key]);
1236-
}
1237-
}
1238-
}
1239-
return newNode;
1240-
};
1241-
1242-
return replaceInNode(node);
1243-
}
1241+
1242+
12441243
}
12451244

12461245
// Helper function to check if a function body contains return statements in control flow

0 commit comments

Comments
 (0)