diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0e023fc2ac5b9..5375ce495641f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21471,7 +21471,7 @@ namespace ts { return; } - const augmentsTags = getAllJSDocTagsOfKind(classLike, SyntaxKind.JSDocAugmentsTag); + const augmentsTags = getJSDocTags(classLike).filter(isJSDocAugmentsTag); Debug.assert(augmentsTags.length > 0); if (augmentsTags.length > 1) { error(augmentsTags[1], Diagnostics.Class_declarations_cannot_have_more_than_one_augments_or_extends_tag); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 8f076d9016b04..631e95fbde406 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4435,33 +4435,33 @@ namespace ts { * for example on a variable declaration whose initializer is a function expression. */ export function hasJSDocParameterTags(node: FunctionLikeDeclaration | SignatureDeclaration): boolean { - return !!getFirstJSDocTag(node, SyntaxKind.JSDocParameterTag); + return !!getFirstJSDocTag(node, isJSDocParameterTag); } /** Gets the JSDoc augments tag for the node if present */ export function getJSDocAugmentsTag(node: Node): JSDocAugmentsTag | undefined { - return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsTag) as JSDocAugmentsTag; + return getFirstJSDocTag(node, isJSDocAugmentsTag); } /** Gets the JSDoc class tag for the node if present */ export function getJSDocClassTag(node: Node): JSDocClassTag | undefined { - return getFirstJSDocTag(node, SyntaxKind.JSDocClassTag) as JSDocClassTag; + return getFirstJSDocTag(node, isJSDocClassTag); } /** Gets the JSDoc return tag for the node if present */ export function getJSDocReturnTag(node: Node): JSDocReturnTag | undefined { - return getFirstJSDocTag(node, SyntaxKind.JSDocReturnTag) as JSDocReturnTag; + return getFirstJSDocTag(node, isJSDocReturnTag); } /** Gets the JSDoc template tag for the node if present */ export function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined { - return getFirstJSDocTag(node, SyntaxKind.JSDocTemplateTag) as JSDocTemplateTag; + return getFirstJSDocTag(node, isJSDocTemplateTag); } /** Gets the JSDoc type tag for the node if present and valid */ export function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined { // We should have already issued an error if there were multiple type jsdocs, so just use the first one. - const tag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag; + const tag = getFirstJSDocTag(node, isJSDocTypeTag); if (tag && tag.typeExpression && tag.typeExpression.type) { return tag; } @@ -4480,7 +4480,7 @@ namespace ts { * tag directly on the node would be returned. */ export function getJSDocType(node: Node): TypeNode | undefined { - let tag: JSDocTypeTag | JSDocParameterTag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag; + let tag: JSDocTypeTag | JSDocParameterTag | undefined = getFirstJSDocTag(node, isJSDocTypeTag); if (!tag && isParameter(node)) { tag = find(getJSDocParameterTags(node), tag => !!tag.typeExpression); } @@ -4500,7 +4500,7 @@ namespace ts { } /** Get all JSDoc tags related to a node, including those on parent nodes. */ - export function getJSDocTags(node: Node): ReadonlyArray | undefined { + export function getJSDocTags(node: Node): ReadonlyArray { let tags = (node as JSDocContainer).jsDocCache; // If cache is 'null', that means we did the work of searching for JSDoc tags and came up with nothing. if (tags === undefined) { @@ -4510,17 +4510,14 @@ namespace ts { } /** Get the first JSDoc tag of a specified kind, or undefined if not present. */ - function getFirstJSDocTag(node: Node, kind: SyntaxKind): JSDocTag | undefined { - const tags = getJSDocTags(node); - return find(tags, doc => doc.kind === kind); + function getFirstJSDocTag(node: Node, predicate: (tag: JSDocTag) => tag is T): T | undefined { + return find(getJSDocTags(node), predicate); } /** Gets all JSDoc tags of a specified kind, or undefined if not present. */ export function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray | undefined { - const tags = getJSDocTags(node); - return filter(tags, doc => doc.kind === kind); + return getJSDocTags(node).filter(doc => doc.kind === kind); } - } // Simple node tests of the form `node.kind === SyntaxKind.Foo`. @@ -5163,6 +5160,10 @@ namespace ts { return node.kind === SyntaxKind.JSDocAugmentsTag; } + export function isJSDocClassTag(node: Node): node is JSDocClassTag { + return node.kind === SyntaxKind.JSDocClassTag; + } + export function isJSDocParameterTag(node: Node): node is JSDocParameterTag { return node.kind === SyntaxKind.JSDocParameterTag; } diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 4e3e01692f20e..2b8bf0d2a407a 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -3034,7 +3034,7 @@ declare namespace ts { */ function getJSDocReturnType(node: Node): TypeNode | undefined; /** Get all JSDoc tags related to a node, including those on parent nodes. */ - function getJSDocTags(node: Node): ReadonlyArray | undefined; + function getJSDocTags(node: Node): ReadonlyArray; /** Gets all JSDoc tags of a specified kind, or undefined if not present. */ function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray | undefined; } @@ -3190,6 +3190,7 @@ declare namespace ts { function isJSDocVariadicType(node: Node): node is JSDocVariadicType; function isJSDoc(node: Node): node is JSDoc; function isJSDocAugmentsTag(node: Node): node is JSDocAugmentsTag; + function isJSDocClassTag(node: Node): node is JSDocClassTag; function isJSDocParameterTag(node: Node): node is JSDocParameterTag; function isJSDocReturnTag(node: Node): node is JSDocReturnTag; function isJSDocTypeTag(node: Node): node is JSDocTypeTag; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 1031837234398..d77850c0c5203 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -3089,7 +3089,7 @@ declare namespace ts { */ function getJSDocReturnType(node: Node): TypeNode | undefined; /** Get all JSDoc tags related to a node, including those on parent nodes. */ - function getJSDocTags(node: Node): ReadonlyArray | undefined; + function getJSDocTags(node: Node): ReadonlyArray; /** Gets all JSDoc tags of a specified kind, or undefined if not present. */ function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray | undefined; } @@ -3245,6 +3245,7 @@ declare namespace ts { function isJSDocVariadicType(node: Node): node is JSDocVariadicType; function isJSDoc(node: Node): node is JSDoc; function isJSDocAugmentsTag(node: Node): node is JSDocAugmentsTag; + function isJSDocClassTag(node: Node): node is JSDocClassTag; function isJSDocParameterTag(node: Node): node is JSDocParameterTag; function isJSDocReturnTag(node: Node): node is JSDocReturnTag; function isJSDocTypeTag(node: Node): node is JSDocTypeTag;