Skip to content

Commit ddffe20

Browse files
committed
Clean up getJSDocParameterTag
And delete its near-duplicate which was much less correct. The callers that had to switch are slightly more complex and more correct now.
1 parent 5a05b94 commit ddffe20

File tree

2 files changed

+22
-51
lines changed

2 files changed

+22
-51
lines changed

src/compiler/checker.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -4854,15 +4854,16 @@ namespace ts {
48544854
if (node.type && node.type.kind === SyntaxKind.JSDocOptionalType) {
48554855
return true;
48564856
}
4857+
const paramTags = getJSDocParameterTag(node);
4858+
if (paramTags) {
4859+
for (const paramTag of paramTags) {
4860+
if (paramTag.isBracketed) {
4861+
return true;
4862+
}
48574863

4858-
const paramTag = getCorrespondingJSDocParameterTag(node);
4859-
if (paramTag) {
4860-
if (paramTag.isBracketed) {
4861-
return true;
4862-
}
4863-
4864-
if (paramTag.typeExpression) {
4865-
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
4864+
if (paramTag.typeExpression) {
4865+
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
4866+
}
48664867
}
48674868
}
48684869
}

src/compiler/utilities.ts

+13-43
Original file line numberDiff line numberDiff line change
@@ -1498,16 +1498,14 @@ namespace ts {
14981498

14991499
// Pull parameter comments from declaring function as well
15001500
if (node.kind === SyntaxKind.Parameter) {
1501-
result = concatenate(result, getContentFromParam(getJSDocParameterTag(node as ParameterDeclaration)));
1501+
result = concatenate(result, getContentFromParam(getJSDocParameterTag(node)));
15021502
}
15031503

15041504
if (isVariableLike(node) && node.initializer) {
15051505
result = concatenate(result, getOwnJSDocs(node.initializer));
15061506
}
15071507

1508-
result = concatenate(result, getOwnJSDocs(node));
1509-
1510-
return result;
1508+
return concatenate(result, getOwnJSDocs(node));
15111509
}
15121510

15131511
function getOwnJSDocs(node: Node) {
@@ -1517,24 +1515,23 @@ namespace ts {
15171515
}
15181516
}
15191517

1520-
function getJSDocParameterTag(param: ParameterDeclaration): JSDocTag[] {
1521-
// TODO: getCorrespondingJSDocParameterTag is basically the same as this, except worse. (and typed, singleton return)
1518+
export function getJSDocParameterTag(param: Node): JSDocParameterTag[] {
1519+
if (!isParameter(param)) {
1520+
return undefined;
1521+
}
15221522
const func = param.parent as FunctionLikeDeclaration;
15231523
const tags = getJSDocTags(func);
15241524
if (!param.name) {
15251525
// this is an anonymous jsdoc param from a `function(type1, type2): type3` specification
15261526
const i = func.parameters.indexOf(param);
1527-
const paramTags = filter(tags, tag => tag.kind === SyntaxKind.JSDocParameterTag);
1527+
const paramTags = filter(tags, tag => tag.kind === SyntaxKind.JSDocParameterTag) as JSDocParameterTag[];
15281528
if (paramTags && 0 <= i && i < paramTags.length) {
15291529
return [paramTags[i]];
15301530
}
15311531
}
15321532
else if (param.name.kind === SyntaxKind.Identifier) {
15331533
const name = (param.name as Identifier).text;
1534-
const paramTags = filter(tags, tag => tag.kind === SyntaxKind.JSDocParameterTag && (tag as JSDocParameterTag).parameterName.text === name);
1535-
if (paramTags) {
1536-
return paramTags;
1537-
}
1534+
return filter(tags as JSDocParameterTag[], tag => tag.kind === SyntaxKind.JSDocParameterTag && tag.parameterName.text === name);
15381535
}
15391536
else {
15401537
// TODO: it's a destructured parameter, so it should look up an "object type" series of multiple lines
@@ -1544,12 +1541,11 @@ namespace ts {
15441541
}
15451542

15461543
export function getJSDocType(node: Node): JSDocType {
1547-
// TODO: If you have to call getparamtag, you really want the first with a typeExpression, not the first one.
15481544
let tag: JSDocTypeTag | JSDocParameterTag = getJSDocTag(node, SyntaxKind.JSDocTypeTag) as JSDocTypeTag;
15491545
if (!tag && node.kind === SyntaxKind.Parameter) {
1550-
const paramTags = getJSDocParameterTag(node as ParameterDeclaration);
1546+
const paramTags = getJSDocParameterTag(node);
15511547
if (paramTags) {
1552-
tag = find(paramTags, tag => !!(tag as JSDocParameterTag).typeExpression) as JSDocParameterTag;
1548+
tag = find(paramTags, tag => !!tag.typeExpression);
15531549
}
15541550
}
15551551

@@ -1564,29 +1560,6 @@ namespace ts {
15641560
return getJSDocTag(node, SyntaxKind.JSDocTemplateTag) as JSDocTemplateTag;
15651561
}
15661562

1567-
export function getCorrespondingJSDocParameterTag(parameter: ParameterDeclaration): JSDocParameterTag {
1568-
if (parameter.name && parameter.name.kind === SyntaxKind.Identifier) {
1569-
// If it's a parameter, see if the parent has a jsdoc comment with an @param
1570-
// annotation.
1571-
const parameterName = (<Identifier>parameter.name).text;
1572-
1573-
const jsDocTags = getJSDocTags(parameter.parent);
1574-
if (!jsDocTags) {
1575-
return undefined;
1576-
}
1577-
for (const tag of jsDocTags) {
1578-
if (tag.kind === SyntaxKind.JSDocParameterTag) {
1579-
const parameterTag = <JSDocParameterTag>tag;
1580-
if (parameterTag.parameterName.text === parameterName) {
1581-
return parameterTag;
1582-
}
1583-
}
1584-
}
1585-
}
1586-
1587-
return undefined;
1588-
}
1589-
15901563
export function hasRestParameter(s: SignatureDeclaration): boolean {
15911564
return isRestParameter(lastOrUndefined(s.parameters));
15921565
}
@@ -1597,14 +1570,11 @@ namespace ts {
15971570

15981571
export function isRestParameter(node: ParameterDeclaration) {
15991572
if (node && (node.flags & NodeFlags.JavaScriptFile)) {
1600-
if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType) {
1573+
if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType ||
1574+
forEach(getJSDocParameterTag(node),
1575+
t => t.typeExpression && t.typeExpression.type.kind === SyntaxKind.JSDocVariadicType)) {
16011576
return true;
16021577
}
1603-
1604-
const paramTag = getCorrespondingJSDocParameterTag(node);
1605-
if (paramTag && paramTag.typeExpression) {
1606-
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocVariadicType;
1607-
}
16081578
}
16091579
return isDeclaredRestParam(node);
16101580
}

0 commit comments

Comments
 (0)