feat: expose findAncestor. (#40325)

* feat: add closest node util

* chore: add definition to baseline file

* chore: alias findAncestor to getClosestNode

* move findAncestor to public

* move findAncestor to public
This commit is contained in:
Septs 2020-09-25 05:02:56 +08:00 committed by GitHub
parent 03b70e6231
commit e6fdcce2bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 22 deletions

View File

@ -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<T extends Node>(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<T>(node: Node, callback: (n: Node) => T | undefined | "quit"): T | undefined {
while (true) {
const res = callback(node);

View File

@ -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<T extends Node>(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.
*

View File

@ -4050,6 +4050,14 @@ declare namespace ts {
function getOriginalNode<T extends Node>(node: Node, nodeTest: (node: Node) => node is T): T;
function getOriginalNode(node: Node | undefined): Node | undefined;
function getOriginalNode<T extends Node>(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<T extends Node>(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.
*

View File

@ -4050,6 +4050,14 @@ declare namespace ts {
function getOriginalNode<T extends Node>(node: Node, nodeTest: (node: Node) => node is T): T;
function getOriginalNode(node: Node | undefined): Node | undefined;
function getOriginalNode<T extends Node>(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<T extends Node>(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.
*