Skip to content

Commit 5996f67

Browse files
Refactor: extract replaceIdentifierReferences and remove this binding from ASTCallbacks
1 parent 347c10d commit 5996f67

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
const ASTCallbacks = {
239263
UnaryExpression(node, state, ancestors) {
240264
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
@@ -896,8 +920,8 @@ const ASTCallbacks = {
896920
// Replace the update expression with the assignment expression
897921
Object.assign(node, assignmentExpr);
898922
delete node.prefix;
899-
this.BinaryExpression(node.right, state, [...ancestors, node]);
900-
this.AssignmentExpression(node, state, ancestors);
923+
ASTCallbacks.BinaryExpression(node.right, state, [...ancestors, node]);
924+
ASTCallbacks.AssignmentExpression(node, state, ancestors);
901925
},
902926
ForStatement(node, state, ancestors) {
903927
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
@@ -954,7 +978,7 @@ const ASTCallbacks = {
954978
// Replace loop variable references with the parameter
955979
if (node.init?.type === 'VariableDeclaration') {
956980
const loopVarName = node.init.declarations[0].id.name;
957-
conditionBody = this.replaceIdentifierReferences(conditionBody, loopVarName, uniqueLoopVar);
981+
conditionBody = replaceIdentifierReferences(conditionBody, loopVarName, uniqueLoopVar);
958982
}
959983
const conditionAst = { type: 'Program', body: [{ type: 'ExpressionStatement', expression: conditionBody }] };
960984
conditionBody = conditionAst.body[0].expression;
@@ -972,7 +996,7 @@ const ASTCallbacks = {
972996
// Replace loop variable references with the parameter
973997
if (node.init?.type === 'VariableDeclaration') {
974998
const loopVarName = node.init.declarations[0].id.name;
975-
updateExpr = this.replaceIdentifierReferences(updateExpr, loopVarName, uniqueLoopVar);
999+
updateExpr = replaceIdentifierReferences(updateExpr, loopVarName, uniqueLoopVar);
9761000
}
9771001
const updateAst = { type: 'Program', body: [{ type: 'ExpressionStatement', expression: updateExpr }] };
9781002
updateExpr = updateAst.body[0].expression;
@@ -1011,7 +1035,7 @@ const ASTCallbacks = {
10111035
// Replace loop variable references in the body
10121036
if (node.init?.type === 'VariableDeclaration') {
10131037
const loopVarName = node.init.declarations[0].id.name;
1014-
bodyBlock = this.replaceIdentifierReferences(bodyBlock, loopVarName, uniqueLoopVar);
1038+
bodyBlock = replaceIdentifierReferences(bodyBlock, loopVarName, uniqueLoopVar);
10151039
}
10161040

10171041
const bodyFunction = {
@@ -1247,33 +1271,8 @@ const ASTCallbacks = {
12471271
delete node.update;
12481272
},
12491273

1250-
// Helper method to replace identifier references in AST nodes
1251-
replaceIdentifierReferences(node, oldName, newName) {
1252-
if (!node || typeof node !== 'object') return node;
1253-
1254-
const replaceInNode = (n) => {
1255-
if (!n || typeof n !== 'object') return n;
1256-
1257-
if (n.type === 'Identifier' && n.name === oldName) {
1258-
return { ...n, name: newName };
1259-
}
1260-
1261-
// Create a copy and recursively process properties
1262-
const newNode = { ...n };
1263-
for (const key in n) {
1264-
if (n.hasOwnProperty(key) && key !== 'parent') {
1265-
if (Array.isArray(n[key])) {
1266-
newNode[key] = n[key].map(replaceInNode);
1267-
} else if (typeof n[key] === 'object') {
1268-
newNode[key] = replaceInNode(n[key]);
1269-
}
1270-
}
1271-
}
1272-
return newNode;
1273-
};
1274-
1275-
return replaceInNode(node);
1276-
}
1274+
1275+
12771276
}
12781277

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

0 commit comments

Comments
 (0)