Skip to content

Commit 7473dcc

Browse files
author
Andy
authored
Merge pull request #15882 from Microsoft/findAncestor
findAncestor: Add generic overload for use with type predicates
2 parents 2a9a6e8 + badfcbf commit 7473dcc

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ namespace ts {
13001300
return <ImportEqualsDeclaration>node;
13011301
}
13021302

1303-
return findAncestor(node, n => n.kind === SyntaxKind.ImportDeclaration) as ImportDeclaration;
1303+
return findAncestor(node, isImportDeclaration);
13041304
}
13051305
}
13061306

@@ -22638,7 +22638,7 @@ namespace ts {
2263822638
const symbolIsUmdExport = symbolFile !== referenceFile;
2263922639
return symbolIsUmdExport ? undefined : symbolFile;
2264022640
}
22641-
return findAncestor(node.parent, n => isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) as ModuleDeclaration | EnumDeclaration;
22641+
return findAncestor(node.parent, (n): n is ModuleDeclaration | EnumDeclaration => isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol);
2264222642
}
2264322643
}
2264422644
}

src/compiler/core.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ namespace ts {
230230
* If no such value is found, it applies the callback until the parent pointer is undefined or the callback returns "quit"
231231
* At that point findAncestor returns undefined.
232232
*/
233+
export function findAncestor<T extends Node>(node: Node, callback: (element: Node) => element is T): T | undefined;
234+
export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node | undefined;
233235
export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node {
234236
while (node) {
235237
const result = callback(node);

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,10 @@ namespace ts {
40104010
return node.kind === SyntaxKind.ImportEqualsDeclaration;
40114011
}
40124012

4013+
export function isImportDeclaration(node: Node): node is ImportDeclaration {
4014+
return node.kind === SyntaxKind.ImportDeclaration;
4015+
}
4016+
40134017
export function isImportClause(node: Node): node is ImportClause {
40144018
return node.kind === SyntaxKind.ImportClause;
40154019
}

0 commit comments

Comments
 (0)