Skip to content

Commit ad9ad8f

Browse files
committed
Clean up getJSDocTypeForVariableLikeDeclarationFromJSDocComment
Yeah, that name is way too long.
1 parent eaf2f6c commit ad9ad8f

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,30 +3156,11 @@ namespace ts {
31563156

31573157
function getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration: VariableLikeDeclaration): JSDocType {
31583158
// First, see if this node has an @type annotation on it directly.
3159-
const typeTag = getJSDocTypeTag(declaration);
3159+
const typeTag = getJSDocTypeTag(declaration, true);
31603160
if (typeTag && typeTag.typeExpression) {
31613161
return typeTag.typeExpression.type;
31623162
}
31633163

3164-
if (declaration.kind === SyntaxKind.VariableDeclaration &&
3165-
declaration.parent.kind === SyntaxKind.VariableDeclarationList &&
3166-
declaration.parent.parent.kind === SyntaxKind.VariableStatement) {
3167-
3168-
// @type annotation might have been on the variable statement, try that instead.
3169-
const annotation = getJSDocTypeTag(declaration.parent.parent);
3170-
if (annotation && annotation.typeExpression) {
3171-
return annotation.typeExpression.type;
3172-
}
3173-
}
3174-
else if (declaration.kind === SyntaxKind.Parameter) {
3175-
// If it's a parameter, see if the parent has a jsdoc comment with an @param
3176-
// annotation.
3177-
const paramTag = getCorrespondingJSDocParameterTag(<ParameterDeclaration>declaration);
3178-
if (paramTag && paramTag.typeExpression) {
3179-
return paramTag.typeExpression.type;
3180-
}
3181-
}
3182-
31833164
return undefined;
31843165
}
31853166

@@ -13551,7 +13532,7 @@ namespace ts {
1355113532
// the destructured type into the contained binding elements.
1355213533
function assignBindingElementTypes(node: VariableLikeDeclaration) {
1355313534
if (isBindingPattern(node.name)) {
13554-
for (const element of (<BindingPattern>node.name).elements) {
13535+
for (const element of node.name.elements) {
1355513536
if (!isOmittedExpression(element)) {
1355613537
if (element.name.kind === SyntaxKind.Identifier) {
1355713538
getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);

src/compiler/utilities.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,10 +1449,16 @@ namespace ts {
14491449
}, tags => tags);
14501450
}
14511451

1452-
function getJSDocs<T>(node: Node, checkParentVariableStatement: boolean, getDocs: (docs: JSDoc[]) => T[], getTags: (tags: JSDocTag[]) => T[]): T[] {
1452+
function getJSDocs<T>(node: Node,
1453+
checkParentVariableStatement: boolean,
1454+
getDocContent: (docs: JSDoc[]) => T[],
1455+
getTagContent: (tags: JSDocTag[]) => T[]): T[] {
14531456
// TODO: A lot of this work should be cached, maybe. I guess it's only used in services right now...
1457+
// This will be hard because it may need to cache parentvariable versions and nonparent versions
1458+
// maybe I should eliminate checkParent first ...
14541459
let result: T[] = undefined;
14551460
// prepend documentation from parent sources
1461+
// TODO: Probably always want checkParent=true, right?
14561462
if (checkParentVariableStatement) {
14571463
// Try to recognize this pattern when node is initializer of variable declaration and JSDoc comments are on containing variable statement.
14581464
// /**
@@ -1472,11 +1478,11 @@ namespace ts {
14721478
isVariableOfVariableDeclarationStatement ? node.parent.parent :
14731479
undefined;
14741480
if (variableStatementNode) {
1475-
result = concatenate(result, getJSDocs(variableStatementNode, checkParentVariableStatement, getDocs, getTags));
1481+
result = concatenate(result, getJSDocs(variableStatementNode, checkParentVariableStatement, getDocContent, getTagContent));
14761482
}
14771483
if (node.kind === SyntaxKind.ModuleDeclaration &&
14781484
node.parent && node.parent.kind === SyntaxKind.ModuleDeclaration) {
1479-
result = concatenate(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags));
1485+
result = concatenate(result, getJSDocs(node.parent, checkParentVariableStatement, getDocContent, getTagContent));
14801486
}
14811487

14821488
// Also recognize when the node is the RHS of an assignment expression
@@ -1487,40 +1493,42 @@ namespace ts {
14871493
(parent as BinaryExpression).operatorToken.kind === SyntaxKind.EqualsToken &&
14881494
parent.parent.kind === SyntaxKind.ExpressionStatement;
14891495
if (isSourceOfAssignmentExpressionStatement) {
1490-
result = concatenate(result, getJSDocs(parent.parent, checkParentVariableStatement, getDocs, getTags));
1496+
result = concatenate(result, getJSDocs(parent.parent, checkParentVariableStatement, getDocContent, getTagContent));
14911497
}
14921498

14931499
const isPropertyAssignmentExpression = parent && parent.kind === SyntaxKind.PropertyAssignment;
14941500
if (isPropertyAssignmentExpression) {
1495-
result = concatenate(result, getJSDocs(parent, checkParentVariableStatement, getDocs, getTags));
1501+
result = concatenate(result, getJSDocs(parent, checkParentVariableStatement, getDocContent, getTagContent));
14961502
}
14971503

14981504
// Pull parameter comments from declaring function as well
14991505
if (node.kind === SyntaxKind.Parameter) {
15001506
const paramTags = getJSDocParameterTag(node as ParameterDeclaration, checkParentVariableStatement);
15011507
if (paramTags) {
1502-
result = concatenate(result, getTags(paramTags));
1508+
result = concatenate(result, getTagContent(paramTags));
15031509
}
15041510
}
15051511
}
15061512

15071513
if (isVariableLike(node) && node.initializer) {
1508-
result = concatenate(result, getJSDocs(node.initializer, /*checkParentVariableStatement*/ false, getDocs, getTags));
1514+
// TODO: Does this really need to be called for everything?
1515+
result = concatenate(result, getJSDocs(node.initializer, /*checkParentVariableStatement*/ false, getDocContent, getTagContent));
15091516
}
15101517

15111518
if (node.jsDocComments) {
15121519
if (result) {
1513-
result = concatenate(result, getDocs(node.jsDocComments));
1520+
result = concatenate(result, getDocContent(node.jsDocComments));
15141521
}
15151522
else {
1516-
return getDocs(node.jsDocComments);
1523+
return getDocContent(node.jsDocComments);
15171524
}
15181525
}
15191526

15201527
return result;
15211528
}
15221529

15231530
function getJSDocParameterTag(param: ParameterDeclaration, checkParentVariableStatement: boolean): JSDocTag[] {
1531+
// TODO: getCorrespondingJSDocParameterTag is basically the same as this, except worse. (and typed, singleton return)
15241532
const func = param.parent as FunctionLikeDeclaration;
15251533
const tags = getJSDocTags(func, checkParentVariableStatement);
15261534
if (!param.name) {
@@ -1545,16 +1553,19 @@ namespace ts {
15451553
}
15461554
}
15471555

1548-
export function getJSDocTypeTag(node: Node): JSDocTypeTag {
1549-
return <JSDocTypeTag>getJSDocTag(node, SyntaxKind.JSDocTypeTag, /*checkParentVariableStatement*/ false);
1556+
export function getJSDocTypeTag(node: Node, checkParentVariableStatement?: boolean): JSDocTypeTag | JSDocParameterTag {
1557+
// TODO: Get rid of the second parameter again
1558+
// TODO: Don't call getJSDocTag twice. Call a version that allows you to retrieve either kind.
1559+
return getJSDocTag(node, SyntaxKind.JSDocTypeTag, checkParentVariableStatement) as JSDocTypeTag ||
1560+
node.kind === SyntaxKind.Parameter && firstOrUndefined(getJSDocParameterTag(node as ParameterDeclaration, checkParentVariableStatement)) as JSDocParameterTag;
15501561
}
15511562

15521563
export function getJSDocReturnTag(node: Node): JSDocReturnTag {
1553-
return <JSDocReturnTag>getJSDocTag(node, SyntaxKind.JSDocReturnTag, /*checkParentVariableStatement*/ true);
1564+
return getJSDocTag(node, SyntaxKind.JSDocReturnTag, /*checkParentVariableStatement*/ true) as JSDocReturnTag;
15541565
}
15551566

15561567
export function getJSDocTemplateTag(node: Node): JSDocTemplateTag {
1557-
return <JSDocTemplateTag>getJSDocTag(node, SyntaxKind.JSDocTemplateTag, /*checkParentVariableStatement*/ false);
1568+
return getJSDocTag(node, SyntaxKind.JSDocTemplateTag, /*checkParentVariableStatement*/ false) as JSDocTemplateTag;
15581569
}
15591570

15601571
export function getCorrespondingJSDocParameterTag(parameter: ParameterDeclaration): JSDocParameterTag {

0 commit comments

Comments
 (0)