diff --git a/src/services/codefixes/inferFromUsage.ts b/src/services/codefixes/inferFromUsage.ts index 96a0777c2c1..e74c1c74911 100644 --- a/src/services/codefixes/inferFromUsage.ts +++ b/src/services/codefixes/inferFromUsage.ts @@ -170,7 +170,7 @@ namespace ts.codefix { } const type = inferTypeForVariableFromUsage(getAccessorDeclaration.name, sourceFile, program, cancellationToken); - const closeParenToken = getFirstChildOfKind(getAccessorDeclaration, sourceFile, SyntaxKind.CloseParenToken); + const closeParenToken = findChildOfKind(getAccessorDeclaration, SyntaxKind.CloseParenToken, sourceFile); return makeFix(getAccessorDeclaration, closeParenToken.getEnd(), type, program); } @@ -209,7 +209,7 @@ namespace ts.codefix { case SyntaxKind.MethodDeclaration: const isConstructor = containingFunction.kind === SyntaxKind.Constructor; const searchToken = isConstructor ? - >getFirstChildOfKind(containingFunction, sourceFile, SyntaxKind.ConstructorKeyword) : + findChildOfKind>(containingFunction, SyntaxKind.ConstructorKeyword, sourceFile) : containingFunction.name; if (searchToken) { return InferFromReference.inferTypeForParametersFromReferences(getReferences(searchToken, sourceFile, program, cancellationToken), containingFunction, program.getTypeChecker(), cancellationToken); diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 86db21e3225..59353cb8686 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -992,7 +992,7 @@ namespace ts.FindAllReferences.Core { */ function findOwnConstructorReferences(classSymbol: Symbol, sourceFile: SourceFile, addNode: (node: Node) => void): void { for (const decl of classSymbol.members.get(InternalSymbolName.Constructor).declarations) { - const ctrKeyword = ts.findChildOfKind(decl, ts.SyntaxKind.ConstructorKeyword, sourceFile)!; + const ctrKeyword = findChildOfKind(decl, ts.SyntaxKind.ConstructorKeyword, sourceFile)!; Debug.assert(decl.kind === SyntaxKind.Constructor && !!ctrKeyword); addNode(ctrKeyword); } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 358db5e8b4b..af5ad0c1033 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -445,7 +445,7 @@ namespace ts { return position < candidate.end || !isCompletedNode(candidate, sourceFile); } - export function isCompletedNode(n: Node, sourceFile: SourceFile): boolean { + function isCompletedNode(n: Node, sourceFile: SourceFile): boolean { if (nodeIsMissing(n)) { return false; } @@ -512,7 +512,7 @@ namespace ts { case SyntaxKind.ExpressionStatement: return isCompletedNode((n).expression, sourceFile) || - hasChildOfKind(n, SyntaxKind.SemicolonToken); + hasChildOfKind(n, SyntaxKind.SemicolonToken, sourceFile); case SyntaxKind.ArrayLiteralExpression: case SyntaxKind.ArrayBindingPattern: @@ -540,11 +540,9 @@ namespace ts { return isCompletedNode((n).statement, sourceFile); case SyntaxKind.DoStatement: // rough approximation: if DoStatement has While keyword - then if node is completed is checking the presence of ')'; - const hasWhileKeyword = findChildOfKind(n, SyntaxKind.WhileKeyword, sourceFile); - if (hasWhileKeyword) { - return nodeEndsWith(n, SyntaxKind.CloseParenToken, sourceFile); - } - return isCompletedNode((n).statement, sourceFile); + return hasChildOfKind(n, SyntaxKind.WhileKeyword, sourceFile) + ? nodeEndsWith(n, SyntaxKind.CloseParenToken, sourceFile) + : isCompletedNode((n).statement, sourceFile); case SyntaxKind.TypeQuery: return isCompletedNode((n).exprName, sourceFile); @@ -619,12 +617,12 @@ namespace ts { }; } - export function hasChildOfKind(n: Node, kind: SyntaxKind, sourceFile?: SourceFile): boolean { + export function hasChildOfKind(n: Node, kind: SyntaxKind, sourceFile: SourceFile): boolean { return !!findChildOfKind(n, kind, sourceFile); } - export function findChildOfKind(n: Node, kind: SyntaxKind, sourceFile?: SourceFileLike): Node | undefined { - return forEach(n.getChildren(sourceFile), c => c.kind === kind && c); + export function findChildOfKind(n: Node, kind: T["kind"], sourceFile: SourceFileLike): T | undefined { + return find(n.getChildren(sourceFile), (c): c is T => c.kind === kind); } export function findContainingList(node: Node): SyntaxList | undefined { @@ -1113,10 +1111,6 @@ namespace ts { export function singleElementArray(t: T | undefined): T[] { return t === undefined ? undefined : [t]; } - - export function getFirstChildOfKind(node: Node, sourceFile: SourceFile, kind: SyntaxKind): Node | undefined { - return find(node.getChildren(sourceFile), c => c.kind === kind); - } } // Display-part writer helpers