Skip to content

Commit 44adc06

Browse files
committed
expose getAncestor method
1 parent 1a58afb commit 44adc06

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,41 +3469,6 @@ module ts {
34693469
return getAncestor(node, kind) !== undefined;
34703470
}
34713471

3472-
function getAncestor(node: Node, kind: SyntaxKind): Node {
3473-
switch (kind) {
3474-
// special-cases that can be come first
3475-
case SyntaxKind.ClassDeclaration:
3476-
while (node) {
3477-
switch (node.kind) {
3478-
case SyntaxKind.ClassDeclaration:
3479-
return <ClassDeclaration>node;
3480-
case SyntaxKind.EnumDeclaration:
3481-
case SyntaxKind.InterfaceDeclaration:
3482-
case SyntaxKind.ModuleDeclaration:
3483-
case SyntaxKind.ImportDeclaration:
3484-
// early exit cases - declarations cannot be nested in classes
3485-
return undefined;
3486-
default:
3487-
node = node.parent;
3488-
continue;
3489-
}
3490-
}
3491-
break;
3492-
default:
3493-
while (node) {
3494-
if (node.kind === kind) {
3495-
return node;
3496-
}
3497-
else {
3498-
node = node.parent;
3499-
}
3500-
}
3501-
break;
3502-
}
3503-
3504-
return undefined;
3505-
}
3506-
35073472
// EXPRESSION TYPE CHECKING
35083473

35093474
function checkIdentifier(node: Identifier): Type {

src/compiler/parser.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,41 @@ module ts {
505505
return false;
506506
}
507507

508+
export function getAncestor(node: Node, kind: SyntaxKind): Node {
509+
switch (kind) {
510+
// special-cases that can be come first
511+
case SyntaxKind.ClassDeclaration:
512+
while (node) {
513+
switch (node.kind) {
514+
case SyntaxKind.ClassDeclaration:
515+
return <ClassDeclaration>node;
516+
case SyntaxKind.EnumDeclaration:
517+
case SyntaxKind.InterfaceDeclaration:
518+
case SyntaxKind.ModuleDeclaration:
519+
case SyntaxKind.ImportDeclaration:
520+
// early exit cases - declarations cannot be nested in classes
521+
return undefined;
522+
default:
523+
node = node.parent;
524+
continue;
525+
}
526+
}
527+
break;
528+
default:
529+
while (node) {
530+
if (node.kind === kind) {
531+
return node;
532+
}
533+
else {
534+
node = node.parent;
535+
}
536+
}
537+
break;
538+
}
539+
540+
return undefined;
541+
}
542+
508543
enum ParsingContext {
509544
SourceElements, // Elements in source file
510545
ModuleElements, // Elements in module declaration

0 commit comments

Comments
 (0)