Skip to content

Commit

Permalink
fix: forEachDescendant would error when the node in the callback para…
Browse files Browse the repository at this point in the history
…meter was forgotten.
  • Loading branch information
dsherret committed Nov 11, 2018
1 parent 9576200 commit 152c785
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ export class Node<NodeType extends ts.Node = ts.Node> {
if (stop || skip || up)
return;

forEachChildForNode(node);
if (!node.wasForgotten())
forEachChildForNode(node);
};
const arrayCallback = cbNodeArray == null ? undefined : (nodes: Node[]) => {
if (stop)
Expand Down
17 changes: 17 additions & 0 deletions src/tests/compiler/common/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,23 @@ class MyClass {
"" // end of file token
]);
});

it("should not error when the current node is forgotten/removed", () => {
const {sourceFile} = getInfoFromText("class Test {} interface Test2 {}");
const nodeTexts: string[] = [];
sourceFile.forEachDescendant(node => {
nodeTexts.push(node.getText());
if (TypeGuards.isClassDeclaration(node))
node.remove();
});

expect(nodeTexts).to.deep.equal([
"class Test {}",
"interface Test2 {}",
"Test2",
"" // end of file token
]);
});
});

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

0 comments on commit 152c785

Please sign in to comment.