Skip to content

Commit 540e8b9

Browse files
authored
Collect jsdoc tags for type parameters (#26824)
Before the template tag, there was no reason to do this, but now you can add JSDoc for type parameters in Typescript.
1 parent 4510149 commit 540e8b9

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

Diff for: src/compiler/utilities.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,10 @@ namespace ts {
21162116
result = addRange(result, getJSDocParameterTags(node as ParameterDeclaration));
21172117
break;
21182118
}
2119+
if (node.kind === SyntaxKind.TypeParameter) {
2120+
result = addRange(result, getJSDocTypeParameterTags(node as TypeParameterDeclaration));
2121+
break;
2122+
}
21192123
node = getNextJSDocCommentLocation(node);
21202124
}
21212125
return result || emptyArray;
@@ -4994,15 +4998,14 @@ namespace ts {
49944998
/**
49954999
* Gets the JSDoc parameter tags for the node if present.
49965000
*
4997-
* @remarks Returns any JSDoc param tag that matches the provided
5001+
* @remarks Returns any JSDoc param tag whose name matches the provided
49985002
* parameter, whether a param tag on a containing function
49995003
* expression, or a param tag on a variable declaration whose
50005004
* initializer is the containing function. The tags closest to the
50015005
* node are returned first, so in the previous example, the param
50025006
* tag on the containing function expression would be first.
50035007
*
5004-
* Does not return tags for binding patterns, because JSDoc matches
5005-
* parameters by name and binding patterns do not have a name.
5008+
* For binding patterns, parameter tags are matched by position.
50065009
*/
50075010
export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> {
50085011
if (param.name) {
@@ -5023,6 +5026,22 @@ namespace ts {
50235026
return emptyArray;
50245027
}
50255028

5029+
/**
5030+
* Gets the JSDoc type parameter tags for the node if present.
5031+
*
5032+
* @remarks Returns any JSDoc template tag whose names match the provided
5033+
* parameter, whether a template tag on a containing function
5034+
* expression, or a template tag on a variable declaration whose
5035+
* initializer is the containing function. The tags closest to the
5036+
* node are returned first, so in the previous example, the template
5037+
* tag on the containing function expression would be first.
5038+
*/
5039+
export function getJSDocTypeParameterTags(param: TypeParameterDeclaration): ReadonlyArray<JSDocTemplateTag> {
5040+
const name = param.name.escapedText;
5041+
return getJSDocTags(param.parent).filter((tag): tag is JSDocTemplateTag =>
5042+
isJSDocTemplateTag(tag) && tag.typeParameters.some(tp => tp.name.escapedText === name));
5043+
}
5044+
50265045
/**
50275046
* Return true if the node has JSDoc parameter tags.
50285047
*

Diff for: src/services/signatureHelp.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,6 @@ namespace ts.SignatureHelp {
591591
const param = checker.typeParameterToDeclaration(typeParameter, enclosingDeclaration)!;
592592
printer.writeNode(EmitHint.Unspecified, param, sourceFile, writer);
593593
});
594-
return { name: typeParameter.symbol.name, documentation: emptyArray, displayParts, isOptional: false };
594+
return { name: typeParameter.symbol.name, documentation: typeParameter.symbol.getDocumentationComment(checker), displayParts, isOptional: false };
595595
}
596596
}

Diff for: tests/baselines/reference/api/tsserverlibrary.d.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -3208,17 +3208,27 @@ declare namespace ts {
32083208
/**
32093209
* Gets the JSDoc parameter tags for the node if present.
32103210
*
3211-
* @remarks Returns any JSDoc param tag that matches the provided
3211+
* @remarks Returns any JSDoc param tag whose name matches the provided
32123212
* parameter, whether a param tag on a containing function
32133213
* expression, or a param tag on a variable declaration whose
32143214
* initializer is the containing function. The tags closest to the
32153215
* node are returned first, so in the previous example, the param
32163216
* tag on the containing function expression would be first.
32173217
*
3218-
* Does not return tags for binding patterns, because JSDoc matches
3219-
* parameters by name and binding patterns do not have a name.
3218+
* For binding patterns, parameter tags are matched by position.
32203219
*/
32213220
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
3221+
/**
3222+
* Gets the JSDoc type parameter tags for the node if present.
3223+
*
3224+
* @remarks Returns any JSDoc template tag whose names match the provided
3225+
* parameter, whether a template tag on a containing function
3226+
* expression, or a template tag on a variable declaration whose
3227+
* initializer is the containing function. The tags closest to the
3228+
* node are returned first, so in the previous example, the template
3229+
* tag on the containing function expression would be first.
3230+
*/
3231+
function getJSDocTypeParameterTags(param: TypeParameterDeclaration): ReadonlyArray<JSDocTemplateTag>;
32223232
/**
32233233
* Return true if the node has JSDoc parameter tags.
32243234
*

Diff for: tests/baselines/reference/api/typescript.d.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -3208,17 +3208,27 @@ declare namespace ts {
32083208
/**
32093209
* Gets the JSDoc parameter tags for the node if present.
32103210
*
3211-
* @remarks Returns any JSDoc param tag that matches the provided
3211+
* @remarks Returns any JSDoc param tag whose name matches the provided
32123212
* parameter, whether a param tag on a containing function
32133213
* expression, or a param tag on a variable declaration whose
32143214
* initializer is the containing function. The tags closest to the
32153215
* node are returned first, so in the previous example, the param
32163216
* tag on the containing function expression would be first.
32173217
*
3218-
* Does not return tags for binding patterns, because JSDoc matches
3219-
* parameters by name and binding patterns do not have a name.
3218+
* For binding patterns, parameter tags are matched by position.
32203219
*/
32213220
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
3221+
/**
3222+
* Gets the JSDoc type parameter tags for the node if present.
3223+
*
3224+
* @remarks Returns any JSDoc template tag whose names match the provided
3225+
* parameter, whether a template tag on a containing function
3226+
* expression, or a template tag on a variable declaration whose
3227+
* initializer is the containing function. The tags closest to the
3228+
* node are returned first, so in the previous example, the template
3229+
* tag on the containing function expression would be first.
3230+
*/
3231+
function getJSDocTypeParameterTags(param: TypeParameterDeclaration): ReadonlyArray<JSDocTemplateTag>;
32223232
/**
32233233
* Return true if the node has JSDoc parameter tags.
32243234
*

Diff for: tests/cases/fourslash/signatureHelpTypeArguments2.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
/////** some documentation
4+
//// * @template T some documentation 2
5+
//// * @template W
6+
//// * @template U,V others
7+
//// * @param a ok
8+
//// * @param b not ok
9+
//// */
10+
////function f<T, U, V, W>(a: number, b: string, c: boolean): void { }
11+
////f</*f0*/;
12+
////f<number, /*f1*/;
13+
////f<number, string, /*f2*/;
14+
////f<number, string, boolean, /*f3*/;
15+
16+
function build(marker: string, parameterName: string, parameterDocComment: string) {
17+
return {
18+
marker,
19+
text: "f<T, U, V, W>(a: number, b: string, c: boolean): void",
20+
parameterName,
21+
parameterSpan: parameterName,
22+
docComment: "some documentation",
23+
parameterDocComment,
24+
tags: [{ name: "template", text: "T some documentation 2" },
25+
{ name: "template", text: "W" },
26+
{ name: "template", text: "U, V others" },
27+
{ name: "param", text: "a ok" },
28+
{ name: "param", text: "b not ok" }]
29+
}
30+
}
31+
32+
verify.signatureHelp(
33+
build("f0", "T", "some documentation 2"),
34+
build("f1", "U", "others"),
35+
build("f2", "V", "others"),
36+
build("f3", "W", ""),
37+
);

0 commit comments

Comments
 (0)