Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f1668e

Browse files
author
Andy
authored
Use type predicate for getFirstJSDocTag (microsoft#22573)
* Use type predicate for getFirstJSDocTag * Restore API
1 parent 3728ec3 commit 5f1668e

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21471,7 +21471,7 @@ namespace ts {
2147121471
return;
2147221472
}
2147321473

21474-
const augmentsTags = getAllJSDocTagsOfKind(classLike, SyntaxKind.JSDocAugmentsTag);
21474+
const augmentsTags = getJSDocTags(classLike).filter(isJSDocAugmentsTag);
2147521475
Debug.assert(augmentsTags.length > 0);
2147621476
if (augmentsTags.length > 1) {
2147721477
error(augmentsTags[1], Diagnostics.Class_declarations_cannot_have_more_than_one_augments_or_extends_tag);

src/compiler/utilities.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4435,33 +4435,33 @@ namespace ts {
44354435
* for example on a variable declaration whose initializer is a function expression.
44364436
*/
44374437
export function hasJSDocParameterTags(node: FunctionLikeDeclaration | SignatureDeclaration): boolean {
4438-
return !!getFirstJSDocTag(node, SyntaxKind.JSDocParameterTag);
4438+
return !!getFirstJSDocTag(node, isJSDocParameterTag);
44394439
}
44404440

44414441
/** Gets the JSDoc augments tag for the node if present */
44424442
export function getJSDocAugmentsTag(node: Node): JSDocAugmentsTag | undefined {
4443-
return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsTag) as JSDocAugmentsTag;
4443+
return getFirstJSDocTag(node, isJSDocAugmentsTag);
44444444
}
44454445

44464446
/** Gets the JSDoc class tag for the node if present */
44474447
export function getJSDocClassTag(node: Node): JSDocClassTag | undefined {
4448-
return getFirstJSDocTag(node, SyntaxKind.JSDocClassTag) as JSDocClassTag;
4448+
return getFirstJSDocTag(node, isJSDocClassTag);
44494449
}
44504450

44514451
/** Gets the JSDoc return tag for the node if present */
44524452
export function getJSDocReturnTag(node: Node): JSDocReturnTag | undefined {
4453-
return getFirstJSDocTag(node, SyntaxKind.JSDocReturnTag) as JSDocReturnTag;
4453+
return getFirstJSDocTag(node, isJSDocReturnTag);
44544454
}
44554455

44564456
/** Gets the JSDoc template tag for the node if present */
44574457
export function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined {
4458-
return getFirstJSDocTag(node, SyntaxKind.JSDocTemplateTag) as JSDocTemplateTag;
4458+
return getFirstJSDocTag(node, isJSDocTemplateTag);
44594459
}
44604460

44614461
/** Gets the JSDoc type tag for the node if present and valid */
44624462
export function getJSDocTypeTag(node: Node): JSDocTypeTag | undefined {
44634463
// We should have already issued an error if there were multiple type jsdocs, so just use the first one.
4464-
const tag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag;
4464+
const tag = getFirstJSDocTag(node, isJSDocTypeTag);
44654465
if (tag && tag.typeExpression && tag.typeExpression.type) {
44664466
return tag;
44674467
}
@@ -4480,7 +4480,7 @@ namespace ts {
44804480
* tag directly on the node would be returned.
44814481
*/
44824482
export function getJSDocType(node: Node): TypeNode | undefined {
4483-
let tag: JSDocTypeTag | JSDocParameterTag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag;
4483+
let tag: JSDocTypeTag | JSDocParameterTag | undefined = getFirstJSDocTag(node, isJSDocTypeTag);
44844484
if (!tag && isParameter(node)) {
44854485
tag = find(getJSDocParameterTags(node), tag => !!tag.typeExpression);
44864486
}
@@ -4500,7 +4500,7 @@ namespace ts {
45004500
}
45014501

45024502
/** Get all JSDoc tags related to a node, including those on parent nodes. */
4503-
export function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> | undefined {
4503+
export function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> {
45044504
let tags = (node as JSDocContainer).jsDocCache;
45054505
// If cache is 'null', that means we did the work of searching for JSDoc tags and came up with nothing.
45064506
if (tags === undefined) {
@@ -4510,17 +4510,14 @@ namespace ts {
45104510
}
45114511

45124512
/** Get the first JSDoc tag of a specified kind, or undefined if not present. */
4513-
function getFirstJSDocTag(node: Node, kind: SyntaxKind): JSDocTag | undefined {
4514-
const tags = getJSDocTags(node);
4515-
return find(tags, doc => doc.kind === kind);
4513+
function getFirstJSDocTag<T extends JSDocTag>(node: Node, predicate: (tag: JSDocTag) => tag is T): T | undefined {
4514+
return find(getJSDocTags(node), predicate);
45164515
}
45174516

45184517
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
45194518
export function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag> | undefined {
4520-
const tags = getJSDocTags(node);
4521-
return filter(tags, doc => doc.kind === kind);
4519+
return getJSDocTags(node).filter(doc => doc.kind === kind);
45224520
}
4523-
45244521
}
45254522

45264523
// Simple node tests of the form `node.kind === SyntaxKind.Foo`.
@@ -5163,6 +5160,10 @@ namespace ts {
51635160
return node.kind === SyntaxKind.JSDocAugmentsTag;
51645161
}
51655162

5163+
export function isJSDocClassTag(node: Node): node is JSDocClassTag {
5164+
return node.kind === SyntaxKind.JSDocClassTag;
5165+
}
5166+
51665167
export function isJSDocParameterTag(node: Node): node is JSDocParameterTag {
51675168
return node.kind === SyntaxKind.JSDocParameterTag;
51685169
}

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3034,7 +3034,7 @@ declare namespace ts {
30343034
*/
30353035
function getJSDocReturnType(node: Node): TypeNode | undefined;
30363036
/** Get all JSDoc tags related to a node, including those on parent nodes. */
3037-
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> | undefined;
3037+
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag>;
30383038
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
30393039
function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag> | undefined;
30403040
}
@@ -3190,6 +3190,7 @@ declare namespace ts {
31903190
function isJSDocVariadicType(node: Node): node is JSDocVariadicType;
31913191
function isJSDoc(node: Node): node is JSDoc;
31923192
function isJSDocAugmentsTag(node: Node): node is JSDocAugmentsTag;
3193+
function isJSDocClassTag(node: Node): node is JSDocClassTag;
31933194
function isJSDocParameterTag(node: Node): node is JSDocParameterTag;
31943195
function isJSDocReturnTag(node: Node): node is JSDocReturnTag;
31953196
function isJSDocTypeTag(node: Node): node is JSDocTypeTag;

tests/baselines/reference/api/typescript.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,7 @@ declare namespace ts {
30893089
*/
30903090
function getJSDocReturnType(node: Node): TypeNode | undefined;
30913091
/** Get all JSDoc tags related to a node, including those on parent nodes. */
3092-
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag> | undefined;
3092+
function getJSDocTags(node: Node): ReadonlyArray<JSDocTag>;
30933093
/** Gets all JSDoc tags of a specified kind, or undefined if not present. */
30943094
function getAllJSDocTagsOfKind(node: Node, kind: SyntaxKind): ReadonlyArray<JSDocTag> | undefined;
30953095
}
@@ -3245,6 +3245,7 @@ declare namespace ts {
32453245
function isJSDocVariadicType(node: Node): node is JSDocVariadicType;
32463246
function isJSDoc(node: Node): node is JSDoc;
32473247
function isJSDocAugmentsTag(node: Node): node is JSDocAugmentsTag;
3248+
function isJSDocClassTag(node: Node): node is JSDocClassTag;
32483249
function isJSDocParameterTag(node: Node): node is JSDocParameterTag;
32493250
function isJSDocReturnTag(node: Node): node is JSDocReturnTag;
32503251
function isJSDocTypeTag(node: Node): node is JSDocTypeTag;

0 commit comments

Comments
 (0)