diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 531fcafb7f1..51ba41c5ca7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -100,28 +100,6 @@ namespace ts { !isJsonEqual(getCompilerOptionValue(oldOptions, o), getCompilerOptionValue(newOptions, o))); } - /** - * Iterates through the parent chain of a node and performs the callback on each parent until the callback - * returns a truthy value, then returns that value. - * 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 | undefined, callback: (element: Node) => element is T): T | undefined; - export function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined; - export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node | undefined { - while (node) { - const result = callback(node); - if (result === "quit") { - return undefined; - } - else if (result) { - return node; - } - node = node.parent; - } - return undefined; - } - export function forEachAncestor(node: Node, callback: (n: Node) => T | undefined | "quit"): T | undefined { while (true) { const res = callback(node); diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index e9d28e8fab5..0f702eba480 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -403,6 +403,28 @@ namespace ts { return !nodeTest || nodeTest(node) ? node : undefined; } + /** + * Iterates through the parent chain of a node and performs the callback on each parent until the callback + * returns a truthy value, then returns that value. + * 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 | undefined, callback: (element: Node) => element is T): T | undefined; + export function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined; + export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node | undefined { + while (node) { + const result = callback(node); + if (result === "quit") { + return undefined; + } + else if (result) { + return node; + } + node = node.parent; + } + return undefined; + } + /** * Gets a value indicating whether a node originated in the parse tree. * diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ad725a4faee..a4f5a9a74de 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -4050,6 +4050,14 @@ declare namespace ts { function getOriginalNode(node: Node, nodeTest: (node: Node) => node is T): T; function getOriginalNode(node: Node | undefined): Node | undefined; function getOriginalNode(node: Node | undefined, nodeTest: (node: Node | undefined) => node is T): T | undefined; + /** + * Iterates through the parent chain of a node and performs the callback on each parent until the callback + * returns a truthy value, then returns that value. + * 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. + */ + function findAncestor(node: Node | undefined, callback: (element: Node) => element is T): T | undefined; + function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined; /** * Gets a value indicating whether a node originated in the parse tree. * diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 220231cfa94..5bff8c8059e 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -4050,6 +4050,14 @@ declare namespace ts { function getOriginalNode(node: Node, nodeTest: (node: Node) => node is T): T; function getOriginalNode(node: Node | undefined): Node | undefined; function getOriginalNode(node: Node | undefined, nodeTest: (node: Node | undefined) => node is T): T | undefined; + /** + * Iterates through the parent chain of a node and performs the callback on each parent until the callback + * returns a truthy value, then returns that value. + * 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. + */ + function findAncestor(node: Node | undefined, callback: (element: Node) => element is T): T | undefined; + function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined; /** * Gets a value indicating whether a node originated in the parse tree. *