Skip to content

Commit 21ff2cd

Browse files
author
Andy
authored
Remove unnecessary getFirstChildOfKind helper function (#20647)
1 parent e58d80a commit 21ff2cd

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

src/services/codefixes/inferFromUsage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace ts.codefix {
170170
}
171171

172172
const type = inferTypeForVariableFromUsage(getAccessorDeclaration.name, sourceFile, program, cancellationToken);
173-
const closeParenToken = getFirstChildOfKind(getAccessorDeclaration, sourceFile, SyntaxKind.CloseParenToken);
173+
const closeParenToken = findChildOfKind(getAccessorDeclaration, SyntaxKind.CloseParenToken, sourceFile);
174174
return makeFix(getAccessorDeclaration, closeParenToken.getEnd(), type, program);
175175
}
176176

@@ -209,7 +209,7 @@ namespace ts.codefix {
209209
case SyntaxKind.MethodDeclaration:
210210
const isConstructor = containingFunction.kind === SyntaxKind.Constructor;
211211
const searchToken = isConstructor ?
212-
<Token<SyntaxKind.ConstructorKeyword>>getFirstChildOfKind(containingFunction, sourceFile, SyntaxKind.ConstructorKeyword) :
212+
findChildOfKind<Token<SyntaxKind.ConstructorKeyword>>(containingFunction, SyntaxKind.ConstructorKeyword, sourceFile) :
213213
containingFunction.name;
214214
if (searchToken) {
215215
return InferFromReference.inferTypeForParametersFromReferences(getReferences(searchToken, sourceFile, program, cancellationToken), containingFunction, program.getTypeChecker(), cancellationToken);

src/services/findAllReferences.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ namespace ts.FindAllReferences.Core {
992992
*/
993993
function findOwnConstructorReferences(classSymbol: Symbol, sourceFile: SourceFile, addNode: (node: Node) => void): void {
994994
for (const decl of classSymbol.members.get(InternalSymbolName.Constructor).declarations) {
995-
const ctrKeyword = ts.findChildOfKind(decl, ts.SyntaxKind.ConstructorKeyword, sourceFile)!;
995+
const ctrKeyword = findChildOfKind(decl, ts.SyntaxKind.ConstructorKeyword, sourceFile)!;
996996
Debug.assert(decl.kind === SyntaxKind.Constructor && !!ctrKeyword);
997997
addNode(ctrKeyword);
998998
}

src/services/utilities.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ namespace ts {
445445
return position < candidate.end || !isCompletedNode(candidate, sourceFile);
446446
}
447447

448-
export function isCompletedNode(n: Node, sourceFile: SourceFile): boolean {
448+
function isCompletedNode(n: Node, sourceFile: SourceFile): boolean {
449449
if (nodeIsMissing(n)) {
450450
return false;
451451
}
@@ -512,7 +512,7 @@ namespace ts {
512512

513513
case SyntaxKind.ExpressionStatement:
514514
return isCompletedNode((<ExpressionStatement>n).expression, sourceFile) ||
515-
hasChildOfKind(n, SyntaxKind.SemicolonToken);
515+
hasChildOfKind(n, SyntaxKind.SemicolonToken, sourceFile);
516516

517517
case SyntaxKind.ArrayLiteralExpression:
518518
case SyntaxKind.ArrayBindingPattern:
@@ -540,11 +540,9 @@ namespace ts {
540540
return isCompletedNode((<IterationStatement>n).statement, sourceFile);
541541
case SyntaxKind.DoStatement:
542542
// rough approximation: if DoStatement has While keyword - then if node is completed is checking the presence of ')';
543-
const hasWhileKeyword = findChildOfKind(n, SyntaxKind.WhileKeyword, sourceFile);
544-
if (hasWhileKeyword) {
545-
return nodeEndsWith(n, SyntaxKind.CloseParenToken, sourceFile);
546-
}
547-
return isCompletedNode((<DoStatement>n).statement, sourceFile);
543+
return hasChildOfKind(n, SyntaxKind.WhileKeyword, sourceFile)
544+
? nodeEndsWith(n, SyntaxKind.CloseParenToken, sourceFile)
545+
: isCompletedNode((<DoStatement>n).statement, sourceFile);
548546

549547
case SyntaxKind.TypeQuery:
550548
return isCompletedNode((<TypeQueryNode>n).exprName, sourceFile);
@@ -619,12 +617,12 @@ namespace ts {
619617
};
620618
}
621619

622-
export function hasChildOfKind(n: Node, kind: SyntaxKind, sourceFile?: SourceFile): boolean {
620+
export function hasChildOfKind(n: Node, kind: SyntaxKind, sourceFile: SourceFile): boolean {
623621
return !!findChildOfKind(n, kind, sourceFile);
624622
}
625623

626-
export function findChildOfKind(n: Node, kind: SyntaxKind, sourceFile?: SourceFileLike): Node | undefined {
627-
return forEach(n.getChildren(sourceFile), c => c.kind === kind && c);
624+
export function findChildOfKind<T extends Node>(n: Node, kind: T["kind"], sourceFile: SourceFileLike): T | undefined {
625+
return find(n.getChildren(sourceFile), (c): c is T => c.kind === kind);
628626
}
629627

630628
export function findContainingList(node: Node): SyntaxList | undefined {
@@ -1113,10 +1111,6 @@ namespace ts {
11131111
export function singleElementArray<T>(t: T | undefined): T[] {
11141112
return t === undefined ? undefined : [t];
11151113
}
1116-
1117-
export function getFirstChildOfKind(node: Node, sourceFile: SourceFile, kind: SyntaxKind): Node | undefined {
1118-
return find(node.getChildren(sourceFile), c => c.kind === kind);
1119-
}
11201114
}
11211115

11221116
// Display-part writer helpers

0 commit comments

Comments
 (0)