Skip to content

Commit 152c785

Browse files
committed
fix: forEachDescendant would error when the node in the callback parameter was forgotten.
1 parent 9576200 commit 152c785

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/compiler/ast/common/Node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ export class Node<NodeType extends ts.Node = ts.Node> {
617617
if (stop || skip || up)
618618
return;
619619

620-
forEachChildForNode(node);
620+
if (!node.wasForgotten())
621+
forEachChildForNode(node);
621622
};
622623
const arrayCallback = cbNodeArray == null ? undefined : (nodes: Node[]) => {
623624
if (stop)

src/tests/compiler/common/nodeTests.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,23 @@ class MyClass {
14551455
"" // end of file token
14561456
]);
14571457
});
1458+
1459+
it("should not error when the current node is forgotten/removed", () => {
1460+
const {sourceFile} = getInfoFromText("class Test {} interface Test2 {}");
1461+
const nodeTexts: string[] = [];
1462+
sourceFile.forEachDescendant(node => {
1463+
nodeTexts.push(node.getText());
1464+
if (TypeGuards.isClassDeclaration(node))
1465+
node.remove();
1466+
});
1467+
1468+
expect(nodeTexts).to.deep.equal([
1469+
"class Test {}",
1470+
"interface Test2 {}",
1471+
"Test2",
1472+
"" // end of file token
1473+
]);
1474+
});
14581475
});
14591476

14601477
describe(nameof<Node>(n => n.getNodeProperty), () => {

0 commit comments

Comments
 (0)