Skip to content

Commit 56f646e

Browse files
committed
Make top-level getJSDoc* functions public
* getJSDocParameterTags * getJSDocAugmentsTag * getJSDocClassTag * getJSDocClassTag * getJSDocTemplateTag * getJSDocReturnTag * getJSDocType * getJSDocReturnType
1 parent 6c8bc18 commit 56f646e

1 file changed

Lines changed: 91 additions & 51 deletions

File tree

src/compiler/utilities.ts

Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,15 +1495,6 @@ namespace ts {
14951495
((node as JSDocFunctionType).parameters[0].name as Identifier).escapedText === "new";
14961496
}
14971497

1498-
export function hasJSDocParameterTags(node: FunctionLikeDeclaration | SignatureDeclaration): boolean {
1499-
return !!getFirstJSDocTag(node, SyntaxKind.JSDocParameterTag);
1500-
}
1501-
1502-
function getFirstJSDocTag(node: Node, kind: SyntaxKind): JSDocTag | undefined {
1503-
const tags = getJSDocTags(node);
1504-
return find(tags, doc => doc.kind === kind);
1505-
}
1506-
15071498
export function getAllJSDocs(node: Node): (JSDoc | JSDocTag)[] {
15081499
if (isJSDocTypedefTag(node)) {
15091500
return [node.parent];
@@ -1577,15 +1568,6 @@ namespace ts {
15771568
}
15781569
}
15791570

1580-
export function getJSDocParameterTags(param: ParameterDeclaration): JSDocParameterTag[] | undefined {
1581-
if (param.name && isIdentifier(param.name)) {
1582-
const name = param.name.escapedText;
1583-
return getJSDocTags(param.parent).filter((tag): tag is JSDocParameterTag => isJSDocParameterTag(tag) && isIdentifier(tag.name) && tag.name.escapedText === name) as JSDocParameterTag[];
1584-
}
1585-
// a binding pattern doesn't have a name, so it's not possible to match it a jsdoc parameter, which is identified by name
1586-
return undefined;
1587-
}
1588-
15891571
/** Does the opposite of `getJSDocParameterTags`: given a JSDoc parameter, finds the parameter corresponding to it. */
15901572
export function getParameterSymbolFromJSDoc(node: JSDocParameterTag): Symbol | undefined {
15911573
if (node.symbol) {
@@ -1611,39 +1593,6 @@ namespace ts {
16111593
return find(typeParameters, p => p.name.escapedText === name);
16121594
}
16131595

1614-
export function getJSDocType(node: Node): TypeNode {
1615-
let tag: JSDocTypeTag | JSDocParameterTag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag;
1616-
if (!tag && node.kind === SyntaxKind.Parameter) {
1617-
const paramTags = getJSDocParameterTags(node as ParameterDeclaration);
1618-
if (paramTags) {
1619-
tag = find(paramTags, tag => !!tag.typeExpression);
1620-
}
1621-
}
1622-
1623-
return tag && tag.typeExpression && tag.typeExpression.type;
1624-
}
1625-
1626-
export function getJSDocAugmentsTag(node: Node): JSDocAugmentsTag {
1627-
return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsTag) as JSDocAugmentsTag;
1628-
}
1629-
1630-
export function getJSDocClassTag(node: Node): JSDocClassTag {
1631-
return getFirstJSDocTag(node, SyntaxKind.JSDocClassTag) as JSDocClassTag;
1632-
}
1633-
1634-
export function getJSDocReturnTag(node: Node): JSDocReturnTag {
1635-
return getFirstJSDocTag(node, SyntaxKind.JSDocReturnTag) as JSDocReturnTag;
1636-
}
1637-
1638-
export function getJSDocReturnType(node: Node): TypeNode {
1639-
const returnTag = getJSDocReturnTag(node);
1640-
return returnTag && returnTag.typeExpression && returnTag.typeExpression.type;
1641-
}
1642-
1643-
export function getJSDocTemplateTag(node: Node): JSDocTemplateTag {
1644-
return getFirstJSDocTag(node, SyntaxKind.JSDocTemplateTag) as JSDocTemplateTag;
1645-
}
1646-
16471596
export function hasRestParameter(s: SignatureDeclaration): boolean {
16481597
return isRestParameter(lastOrUndefined(s.parameters));
16491598
}
@@ -3983,6 +3932,97 @@ namespace ts {
39833932
return (declaration as NamedDeclaration).name;
39843933
}
39853934
}
3935+
3936+
/**
3937+
* Gets the JSDoc parameter tags for the node if present.
3938+
*
3939+
* @remarks Returns any JSDoc param tag that matches the provided
3940+
* parameter, whether a param tag on a containing function
3941+
* expression, or a param tag on a variable declaration whose
3942+
* initializer is the containing function. The tags closest to the
3943+
* node are returned first, so in the previous example, the param
3944+
* tag on the containing function expression would be first.
3945+
*
3946+
* Does not return tags for binding patterns, because JSDoc matches
3947+
* parameters by name and binding patterns do not have a name.
3948+
*/
3949+
export function getJSDocParameterTags(param: ParameterDeclaration): ReadonlyArray<JSDocParameterTag> | undefined {
3950+
if (param.name && isIdentifier(param.name)) {
3951+
const name = param.name.escapedText;
3952+
return getJSDocTags(param.parent).filter((tag): tag is JSDocParameterTag => isJSDocParameterTag(tag) && isIdentifier(tag.name) && tag.name.escapedText === name) as JSDocParameterTag[];
3953+
}
3954+
// a binding pattern doesn't have a name, so it's not possible to match it a JSDoc parameter, which is identified by name
3955+
return undefined;
3956+
}
3957+
3958+
/**
3959+
* Return true if the node has JSDoc parameter tags.
3960+
*
3961+
* @remarks Includes parameter tags that are not directly on the node,
3962+
* for example on a variable declaration whose initializer is a function expression.
3963+
*/
3964+
export function hasJSDocParameterTags(node: FunctionLikeDeclaration | SignatureDeclaration): boolean {
3965+
return !!getFirstJSDocTag(node, SyntaxKind.JSDocParameterTag);
3966+
}
3967+
3968+
/** Gets the JSDoc augments tag for the node if present */
3969+
export function getJSDocAugmentsTag(node: Node): JSDocAugmentsTag | undefined {
3970+
return getFirstJSDocTag(node, SyntaxKind.JSDocAugmentsTag) as JSDocAugmentsTag;
3971+
}
3972+
3973+
/** Gets the JSDoc class tag for the node if present */
3974+
export function getJSDocClassTag(node: Node): JSDocClassTag | undefined {
3975+
return getFirstJSDocTag(node, SyntaxKind.JSDocClassTag) as JSDocClassTag;
3976+
}
3977+
3978+
/** Gets the JSDoc template tag for the node if present */
3979+
export function getJSDocTemplateTag(node: Node): JSDocTemplateTag | undefined {
3980+
return getFirstJSDocTag(node, SyntaxKind.JSDocTemplateTag) as JSDocTemplateTag;
3981+
}
3982+
3983+
/** Gets the JSDoc return tag for the node if present */
3984+
export function getJSDocReturnTag(node: Node): JSDocReturnTag | undefined {
3985+
return getFirstJSDocTag(node, SyntaxKind.JSDocReturnTag) as JSDocReturnTag;
3986+
}
3987+
3988+
/**
3989+
* Gets the type node for the node if provided via JSDoc.
3990+
*
3991+
* @remarks The search includes any JSDoc param tag that relates
3992+
* to the provided parameter, for example a type tag on the
3993+
* parameter itself, or a param tag on a containing function
3994+
* expression, or a param tag on a variable declaration whose
3995+
* initializer is the containing function. The tags closest to the
3996+
* node are examined first, so in the previous example, the type
3997+
* tag directly on the node would be returned.
3998+
*/
3999+
export function getJSDocType(node: Node): TypeNode | undefined {
4000+
let tag: JSDocTypeTag | JSDocParameterTag = getFirstJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag;
4001+
if (!tag && node.kind === SyntaxKind.Parameter) {
4002+
const paramTags = getJSDocParameterTags(node as ParameterDeclaration);
4003+
if (paramTags) {
4004+
tag = find(paramTags, tag => !!tag.typeExpression);
4005+
}
4006+
}
4007+
4008+
return tag && tag.typeExpression && tag.typeExpression.type;
4009+
}
4010+
4011+
/**
4012+
* Gets the return type node for the node if provided via JSDoc's return tag.
4013+
*
4014+
* @remarks `getJSDocReturnTag` just gets the whole JSDoc tag. This function
4015+
* gets the type from inside the braces. */
4016+
export function getJSDocReturnType(node: Node): TypeNode | undefined {
4017+
const returnTag = getJSDocReturnTag(node);
4018+
return returnTag && returnTag.typeExpression && returnTag.typeExpression.type;
4019+
}
4020+
4021+
/* Get the first JSDoc tag of a specified kind, or undefined if not present. */
4022+
function getFirstJSDocTag(node: Node, kind: SyntaxKind): JSDocTag | undefined {
4023+
const tags = getJSDocTags(node);
4024+
return find(tags, doc => doc.kind === kind);
4025+
}
39864026
}
39874027

39884028
// Simple node tests of the form `node.kind === SyntaxKind.Foo`.

0 commit comments

Comments
 (0)