Skip to content

Commit e6fdcce

Browse files
authored
feat: expose findAncestor. (microsoft#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
1 parent 03b70e6 commit e6fdcce

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

src/compiler/utilities.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,6 @@ namespace ts {
100100
!isJsonEqual(getCompilerOptionValue(oldOptions, o), getCompilerOptionValue(newOptions, o)));
101101
}
102102

103-
/**
104-
* Iterates through the parent chain of a node and performs the callback on each parent until the callback
105-
* returns a truthy value, then returns that value.
106-
* If no such value is found, it applies the callback until the parent pointer is undefined or the callback returns "quit"
107-
* At that point findAncestor returns undefined.
108-
*/
109-
export function findAncestor<T extends Node>(node: Node | undefined, callback: (element: Node) => element is T): T | undefined;
110-
export function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined;
111-
export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node | undefined {
112-
while (node) {
113-
const result = callback(node);
114-
if (result === "quit") {
115-
return undefined;
116-
}
117-
else if (result) {
118-
return node;
119-
}
120-
node = node.parent;
121-
}
122-
return undefined;
123-
}
124-
125103
export function forEachAncestor<T>(node: Node, callback: (n: Node) => T | undefined | "quit"): T | undefined {
126104
while (true) {
127105
const res = callback(node);

src/compiler/utilitiesPublic.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,28 @@ namespace ts {
403403
return !nodeTest || nodeTest(node) ? node : undefined;
404404
}
405405

406+
/**
407+
* Iterates through the parent chain of a node and performs the callback on each parent until the callback
408+
* returns a truthy value, then returns that value.
409+
* If no such value is found, it applies the callback until the parent pointer is undefined or the callback returns "quit"
410+
* At that point findAncestor returns undefined.
411+
*/
412+
export function findAncestor<T extends Node>(node: Node | undefined, callback: (element: Node) => element is T): T | undefined;
413+
export function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined;
414+
export function findAncestor(node: Node, callback: (element: Node) => boolean | "quit"): Node | undefined {
415+
while (node) {
416+
const result = callback(node);
417+
if (result === "quit") {
418+
return undefined;
419+
}
420+
else if (result) {
421+
return node;
422+
}
423+
node = node.parent;
424+
}
425+
return undefined;
426+
}
427+
406428
/**
407429
* Gets a value indicating whether a node originated in the parse tree.
408430
*

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,6 +4050,14 @@ declare namespace ts {
40504050
function getOriginalNode<T extends Node>(node: Node, nodeTest: (node: Node) => node is T): T;
40514051
function getOriginalNode(node: Node | undefined): Node | undefined;
40524052
function getOriginalNode<T extends Node>(node: Node | undefined, nodeTest: (node: Node | undefined) => node is T): T | undefined;
4053+
/**
4054+
* Iterates through the parent chain of a node and performs the callback on each parent until the callback
4055+
* returns a truthy value, then returns that value.
4056+
* If no such value is found, it applies the callback until the parent pointer is undefined or the callback returns "quit"
4057+
* At that point findAncestor returns undefined.
4058+
*/
4059+
function findAncestor<T extends Node>(node: Node | undefined, callback: (element: Node) => element is T): T | undefined;
4060+
function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined;
40534061
/**
40544062
* Gets a value indicating whether a node originated in the parse tree.
40554063
*

tests/baselines/reference/api/typescript.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4050,6 +4050,14 @@ declare namespace ts {
40504050
function getOriginalNode<T extends Node>(node: Node, nodeTest: (node: Node) => node is T): T;
40514051
function getOriginalNode(node: Node | undefined): Node | undefined;
40524052
function getOriginalNode<T extends Node>(node: Node | undefined, nodeTest: (node: Node | undefined) => node is T): T | undefined;
4053+
/**
4054+
* Iterates through the parent chain of a node and performs the callback on each parent until the callback
4055+
* returns a truthy value, then returns that value.
4056+
* If no such value is found, it applies the callback until the parent pointer is undefined or the callback returns "quit"
4057+
* At that point findAncestor returns undefined.
4058+
*/
4059+
function findAncestor<T extends Node>(node: Node | undefined, callback: (element: Node) => element is T): T | undefined;
4060+
function findAncestor(node: Node | undefined, callback: (element: Node) => boolean | "quit"): Node | undefined;
40534061
/**
40544062
* Gets a value indicating whether a node originated in the parse tree.
40554063
*

0 commit comments

Comments
 (0)