From badfcbfb43f594710020481662fa2ae680ddfecd Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 16 May 2017 09:45:01 -0700 Subject: [PATCH] findAncestor: Add generic overload for use with type predicates --- src/compiler/checker.ts | 4 ++-- src/compiler/core.ts | 2 ++ src/compiler/utilities.ts | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7a22c841450..5b816656183 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1300,7 +1300,7 @@ namespace ts { return node; } - return findAncestor(node, n => n.kind === SyntaxKind.ImportDeclaration) as ImportDeclaration; + return findAncestor(node, isImportDeclaration); } } @@ -22638,7 +22638,7 @@ namespace ts { const symbolIsUmdExport = symbolFile !== referenceFile; return symbolIsUmdExport ? undefined : symbolFile; } - return findAncestor(node.parent, n => isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) as ModuleDeclaration | EnumDeclaration; + return findAncestor(node.parent, (n): n is ModuleDeclaration | EnumDeclaration => isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol); } } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 5e4afce98c8..60553fdab01 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -230,6 +230,8 @@ namespace ts { * If no such value is found, it applies the callback until the parent pointer is undefined or the callback returns "quit" * At that point findAncestor returns undefined. */ + export function findAncestor(node: Node, callback: (element: Node) => element is T): T | undefined; + export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node | undefined; export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node { while (node) { const result = callback(node); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 5046b44d094..43a144f9045 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4010,6 +4010,10 @@ namespace ts { return node.kind === SyntaxKind.ImportEqualsDeclaration; } + export function isImportDeclaration(node: Node): node is ImportDeclaration { + return node.kind === SyntaxKind.ImportDeclaration; + } + export function isImportClause(node: Node): node is ImportClause { return node.kind === SyntaxKind.ImportClause; }