Skip to content

Commit

Permalink
feat: #669 - Add past parent as second parameter to getParentWhile
Browse files Browse the repository at this point in the history
  • Loading branch information
Validark authored and dsherret committed Sep 1, 2019
1 parent b0398f8 commit 287158e
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/compiler/ast/common/Node.ts
Expand Up @@ -1110,14 +1110,14 @@ export class Node<NodeType extends ts.Node = ts.Node> {
* Throws if the initial parent doesn't match the condition.
* @param condition - Condition that tests the parent to see if the expression is true.
*/
getParentWhileOrThrow<T extends Node>(condition: (node: Node) => node is T): T;
getParentWhileOrThrow<T extends Node>(condition: (parent: Node, node: Node) => parent is T): T;
/**
* Goes up the parents (ancestors) of the node while a condition is true.
* Throws if the initial parent doesn't match the condition.
* @param condition - Condition that tests the parent to see if the expression is true.
*/
getParentWhileOrThrow(condition: (node: Node) => boolean): Node;
getParentWhileOrThrow(condition: (node: Node) => boolean) {
getParentWhileOrThrow(condition: (parent: Node, node: Node) => boolean): Node;
getParentWhileOrThrow(condition: (parent: Node, node: Node) => boolean) {
return errors.throwIfNullOrUndefined(this.getParentWhile(condition), "The initial parent did not match the provided condition.");
}

Expand All @@ -1126,20 +1126,22 @@ export class Node<NodeType extends ts.Node = ts.Node> {
* Returns undefined if the initial parent doesn't match the condition.
* @param condition - Condition that tests the parent to see if the expression is true.
*/
getParentWhile<T extends Node>(condition: (node: Node) => node is T): T | undefined;
getParentWhile<T extends Node>(condition: (parent: Node, node: Node) => parent is T): T | undefined;
/**
* Goes up the parents (ancestors) of the node while a condition is true.
* Returns undefined if the initial parent doesn't match the condition.
* @param condition - Condition that tests the parent to see if the expression is true.
*/
getParentWhile(condition: (node: Node) => boolean): Node | undefined;
getParentWhile(condition: (node: Node) => boolean) {
let node: Node | undefined = undefined;
let nextParent: Node | undefined = this.getParent();
while (nextParent != null && condition(nextParent)) {
node = nextParent;
nextParent = nextParent.getParent();
}
getParentWhile(condition: (parent: Node, node: Node) => boolean): Node | undefined;
getParentWhile(condition: (parent: Node, node: Node) => boolean) {
let node: Node | undefined;
let parent: Node | undefined = this.getParent();

if (parent && condition(parent, this))
do {
node = parent;
parent = node.getParent();
} while (parent && condition(parent, node));

return node;
}
Expand Down

0 comments on commit 287158e

Please sign in to comment.