Description
TokenVisitor.visitPre sets current = treeNode when entering a node, but there is no visitPost override to restore current to the parent node when leaving. After visiting a node's first child subtree, current still points to the deepest node visited, so subsequent children at the same level get incorrect parent IDs. This produces a malformed tree for any syntax node with more than one child.
Fix
Add a visitPost override that restores current to its parent:
override func visitPost(_ node: Syntax) {
current = current?.parentNode // or pop from a stack
}
Description
TokenVisitor.visitPresetscurrent = treeNodewhen entering a node, but there is novisitPostoverride to restorecurrentto the parent node when leaving. After visiting a node's first child subtree,currentstill points to the deepest node visited, so subsequent children at the same level get incorrect parent IDs. This produces a malformed tree for any syntax node with more than one child.Fix
Add a
visitPostoverride that restorescurrentto its parent: