mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 01:49:57 -05:00
Use type predicate for getFirstJSDocTag (#22573)
* Use type predicate for getFirstJSDocTag * Restore API
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<JSDocTag> | undefined {
|
||||
export function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> {
|
||||
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<T extends JSDocTag>(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<JSDocTag> | 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user