From 316f968821e0027ce3be1c8c8f9d6ead10501f1d Mon Sep 17 00:00:00 2001 From: ChouUn Date: Wed, 22 Jan 2025 20:19:47 +0800 Subject: [PATCH] fix: ensure parent pointers are correctly set for transformed nodes Previously, the `setParent` function was applied to `updatedBlock` instead of `result`, which could lead to incorrect parent pointers in the final transformed AST. This fix ensures that parent pointers are correctly set for the final transformed node (`result`) and its subtree, maintaining the integrity of the AST during transformations. --- .../pre-transformers/using-transformer.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/transformation/pre-transformers/using-transformer.ts b/src/transformation/pre-transformers/using-transformer.ts index d78f6206a..d6121a33f 100644 --- a/src/transformation/pre-transformers/using-transformer.ts +++ b/src/transformation/pre-transformers/using-transformer.ts @@ -10,10 +10,10 @@ export function usingTransformer(context: TransformationContext): ts.Transformer if (hasUsings) { // Recurse visitor into updated block to find further usings const updatedBlock = ts.factory.updateBlock(node, newStatements); - const result = ts.visitEachChild(updatedBlock, visit, ctx); + const visitedBlock = ts.visitEachChild(updatedBlock, visit, ctx); // Set all the synthetic node parents to something that makes sense - const parent: ts.Node[] = [updatedBlock]; + const parent: ts.Node[] = []; function setParent(node2: ts.Node): ts.Node { ts.setParent(node2, parent[parent.length - 1]); parent.push(node2); @@ -21,10 +21,11 @@ export function usingTransformer(context: TransformationContext): ts.Transformer parent.pop(); return node2; } - ts.visitEachChild(updatedBlock, setParent, ctx); - ts.setParent(updatedBlock, node.parent); + parent.push(visitedBlock); + ts.visitEachChild(visitedBlock, setParent, ctx); + ts.setParent(visitedBlock, node.parent); - return result; + return visitedBlock; } } return ts.visitEachChild(node, visit, ctx);