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.
This commit is contained in:
Nathan Shively-Sanders
2018-09-04 09:29:19 -07:00
committed by GitHub
parent 45101491c0
commit 540e8b9eb0
5 changed files with 86 additions and 10 deletions
+22 -3
View File
@@ -2116,6 +2116,10 @@ namespace ts {
result = addRange(result, getJSDocParameterTags(node as ParameterDeclaration));
break;
}
if (node.kind === SyntaxKind.TypeParameter) {
result = addRange(result, getJSDocTypeParameterTags(node as TypeParameterDeclaration));
break;
}
node = getNextJSDocCommentLocation(node);
}
return result || emptyArray;
@@ -4994,15 +4998,14 @@ namespace ts {
/**
* Gets the JSDoc parameter tags for the node if present.
*
* @remarks Returns any JSDoc param tag that matches the provided
* @remarks Returns any JSDoc param tag whose name matches the provided
* parameter, whether a param tag on a containing function
* expression, or a param tag on a variable declaration whose
* initializer is the containing function. The tags closest to the
* node are returned first, so in the previous example, the param
* tag on the containing function expression would be first.
*
* Does not return tags for binding patterns, because JSDoc matches
* parameters by name and binding patterns do not have a name.
* For binding patterns, parameter tags are matched by position.
*/
export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> {
if (param.name) {
@@ -5023,6 +5026,22 @@ namespace ts {
return emptyArray;
}
/**
* Gets the JSDoc type parameter tags for the node if present.
*
* @remarks Returns any JSDoc template tag whose names match the provided
* parameter, whether a template tag on a containing function
* expression, or a template tag on a variable declaration whose
* initializer is the containing function. The tags closest to the
* node are returned first, so in the previous example, the template
* tag on the containing function expression would be first.
*/
export function getJSDocTypeParameterTags(param: TypeParameterDeclaration): ReadonlyArray<JSDocTemplateTag> {
const name = param.name.escapedText;
return getJSDocTags(param.parent).filter((tag): tag is JSDocTemplateTag =>
isJSDocTemplateTag(tag) && tag.typeParameters.some(tp => tp.name.escapedText === name));
}
/**
* Return true if the node has JSDoc parameter tags.
*
+1 -1
View File
@@ -591,6 +591,6 @@ namespace ts.SignatureHelp {
const param = checker.typeParameterToDeclaration(typeParameter, enclosingDeclaration)!;
printer.writeNode(EmitHint.Unspecified, param, sourceFile, writer);
});
return { name: typeParameter.symbol.name, documentation: emptyArray, displayParts, isOptional: false };
return { name: typeParameter.symbol.name, documentation: typeParameter.symbol.getDocumentationComment(checker), displayParts, isOptional: false };
}
}
+13 -3
View File
@@ -3208,17 +3208,27 @@ declare namespace ts {
/**
* Gets the JSDoc parameter tags for the node if present.
*
* @remarks Returns any JSDoc param tag that matches the provided
* @remarks Returns any JSDoc param tag whose name matches the provided
* parameter, whether a param tag on a containing function
* expression, or a param tag on a variable declaration whose
* initializer is the containing function. The tags closest to the
* node are returned first, so in the previous example, the param
* tag on the containing function expression would be first.
*
* Does not return tags for binding patterns, because JSDoc matches
* parameters by name and binding patterns do not have a name.
* For binding patterns, parameter tags are matched by position.
*/
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
/**
* Gets the JSDoc type parameter tags for the node if present.
*
* @remarks Returns any JSDoc template tag whose names match the provided
* parameter, whether a template tag on a containing function
* expression, or a template tag on a variable declaration whose
* initializer is the containing function. The tags closest to the
* node are returned first, so in the previous example, the template
* tag on the containing function expression would be first.
*/
function getJSDocTypeParameterTags(param: TypeParameterDeclaration): ReadonlyArray<JSDocTemplateTag>;
/**
* Return true if the node has JSDoc parameter tags.
*
+13 -3
View File
@@ -3208,17 +3208,27 @@ declare namespace ts {
/**
* Gets the JSDoc parameter tags for the node if present.
*
* @remarks Returns any JSDoc param tag that matches the provided
* @remarks Returns any JSDoc param tag whose name matches the provided
* parameter, whether a param tag on a containing function
* expression, or a param tag on a variable declaration whose
* initializer is the containing function. The tags closest to the
* node are returned first, so in the previous example, the param
* tag on the containing function expression would be first.
*
* Does not return tags for binding patterns, because JSDoc matches
* parameters by name and binding patterns do not have a name.
* For binding patterns, parameter tags are matched by position.
*/
function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag>;
/**
* Gets the JSDoc type parameter tags for the node if present.
*
* @remarks Returns any JSDoc template tag whose names match the provided
* parameter, whether a template tag on a containing function
* expression, or a template tag on a variable declaration whose
* initializer is the containing function. The tags closest to the
* node are returned first, so in the previous example, the template
* tag on the containing function expression would be first.
*/
function getJSDocTypeParameterTags(param: TypeParameterDeclaration): ReadonlyArray<JSDocTemplateTag>;
/**
* Return true if the node has JSDoc parameter tags.
*
@@ -0,0 +1,37 @@
/// <reference path="fourslash.ts"/>
/////** some documentation
//// * @template T some documentation 2
//// * @template W
//// * @template U,V others
//// * @param a ok
//// * @param b not ok
//// */
////function f<T, U, V, W>(a: number, b: string, c: boolean): void { }
////f</*f0*/;
////f<number, /*f1*/;
////f<number, string, /*f2*/;
////f<number, string, boolean, /*f3*/;
function build(marker: string, parameterName: string, parameterDocComment: string) {
return {
marker,
text: "f<T, U, V, W>(a: number, b: string, c: boolean): void",
parameterName,
parameterSpan: parameterName,
docComment: "some documentation",
parameterDocComment,
tags: [{ name: "template", text: "T some documentation 2" },
{ name: "template", text: "W" },
{ name: "template", text: "U, V others" },
{ name: "param", text: "a ok" },
{ name: "param", text: "b not ok" }]
}
}
verify.signatureHelp(
build("f0", "T", "some documentation 2"),
build("f1", "U", "others"),
build("f2", "V", "others"),
build("f3", "W", ""),
);