Skip to content

Commit 4092531

Browse files
authored
Merge pull request #12335 from Microsoft/getJSDoc-cleanup
getJSDocs cleanup
2 parents baa228b + 0c5429d commit 4092531

File tree

10 files changed

+124
-206
lines changed

10 files changed

+124
-206
lines changed

src/compiler/binder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,8 @@ namespace ts {
599599
// Binding of JsDocComment should be done before the current block scope container changes.
600600
// because the scope of JsDocComment should not be affected by whether the current node is a
601601
// container or not.
602-
if (isInJavaScriptFile(node) && node.jsDocComments) {
603-
forEach(node.jsDocComments, bind);
602+
if (isInJavaScriptFile(node) && node.jsDoc) {
603+
forEach(node.jsDoc, bind);
604604
}
605605
if (checkUnreachable(node)) {
606606
bindEachChild(node);

src/compiler/checker.ts

+19-46
Original file line numberDiff line numberDiff line change
@@ -3156,38 +3156,10 @@ namespace ts {
31563156
}
31573157

31583158
function getTypeForVariableLikeDeclarationFromJSDocComment(declaration: VariableLikeDeclaration) {
3159-
const jsDocType = getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration);
3160-
if (jsDocType) {
3161-
return getTypeFromTypeNode(jsDocType);
3159+
const jsdocType = getJSDocType(declaration);
3160+
if (jsdocType) {
3161+
return getTypeFromTypeNode(jsdocType);
31623162
}
3163-
}
3164-
3165-
function getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration: VariableLikeDeclaration): JSDocType {
3166-
// First, see if this node has an @type annotation on it directly.
3167-
const typeTag = getJSDocTypeTag(declaration);
3168-
if (typeTag && typeTag.typeExpression) {
3169-
return typeTag.typeExpression.type;
3170-
}
3171-
3172-
if (declaration.kind === SyntaxKind.VariableDeclaration &&
3173-
declaration.parent.kind === SyntaxKind.VariableDeclarationList &&
3174-
declaration.parent.parent.kind === SyntaxKind.VariableStatement) {
3175-
3176-
// @type annotation might have been on the variable statement, try that instead.
3177-
const annotation = getJSDocTypeTag(declaration.parent.parent);
3178-
if (annotation && annotation.typeExpression) {
3179-
return annotation.typeExpression.type;
3180-
}
3181-
}
3182-
else if (declaration.kind === SyntaxKind.Parameter) {
3183-
// If it's a parameter, see if the parent has a jsdoc comment with an @param
3184-
// annotation.
3185-
const paramTag = getCorrespondingJSDocParameterTag(<ParameterDeclaration>declaration);
3186-
if (paramTag && paramTag.typeExpression) {
3187-
return paramTag.typeExpression.type;
3188-
}
3189-
}
3190-
31913163
return undefined;
31923164
}
31933165

@@ -3455,9 +3427,9 @@ namespace ts {
34553427
declaration.kind === SyntaxKind.PropertyAccessExpression && declaration.parent.kind === SyntaxKind.BinaryExpression) {
34563428
// Use JS Doc type if present on parent expression statement
34573429
if (declaration.flags & NodeFlags.JavaScriptFile) {
3458-
const typeTag = getJSDocTypeTag(declaration.parent);
3459-
if (typeTag && typeTag.typeExpression) {
3460-
return links.type = getTypeFromTypeNode(typeTag.typeExpression.type);
3430+
const jsdocType = getJSDocType(declaration.parent);
3431+
if (jsdocType) {
3432+
return links.type = getTypeFromTypeNode(jsdocType);
34613433
}
34623434
}
34633435
const declaredTypes = map(symbol.declarations,
@@ -4901,15 +4873,16 @@ namespace ts {
49014873
if (node.type && node.type.kind === SyntaxKind.JSDocOptionalType) {
49024874
return true;
49034875
}
4876+
const paramTags = getJSDocParameterTags(node);
4877+
if (paramTags) {
4878+
for (const paramTag of paramTags) {
4879+
if (paramTag.isBracketed) {
4880+
return true;
4881+
}
49044882

4905-
const paramTag = getCorrespondingJSDocParameterTag(node);
4906-
if (paramTag) {
4907-
if (paramTag.isBracketed) {
4908-
return true;
4909-
}
4910-
4911-
if (paramTag.typeExpression) {
4912-
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
4883+
if (paramTag.typeExpression) {
4884+
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType;
4885+
}
49134886
}
49144887
}
49154888
}
@@ -10396,9 +10369,9 @@ namespace ts {
1039610369
}
1039710370

1039810371
function getTypeForThisExpressionFromJSDoc(node: Node) {
10399-
const typeTag = getJSDocTypeTag(node);
10400-
if (typeTag && typeTag.typeExpression && typeTag.typeExpression.type && typeTag.typeExpression.type.kind === SyntaxKind.JSDocFunctionType) {
10401-
const jsDocFunctionType = <JSDocFunctionType>typeTag.typeExpression.type;
10372+
const jsdocType = getJSDocType(node);
10373+
if (jsdocType && jsdocType.kind === SyntaxKind.JSDocFunctionType) {
10374+
const jsDocFunctionType = <JSDocFunctionType>jsdocType;
1040210375
if (jsDocFunctionType.parameters.length > 0 && jsDocFunctionType.parameters[0].type.kind === SyntaxKind.JSDocThisType) {
1040310376
return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type);
1040410377
}
@@ -13632,7 +13605,7 @@ namespace ts {
1363213605
// the destructured type into the contained binding elements.
1363313606
function assignBindingElementTypes(node: VariableLikeDeclaration) {
1363413607
if (isBindingPattern(node.name)) {
13635-
for (const element of (<BindingPattern>node.name).elements) {
13608+
for (const element of node.name.elements) {
1363613609
if (!isOmittedExpression(element)) {
1363713610
if (element.name.kind === SyntaxKind.Identifier) {
1363813611
getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element);

src/compiler/declarationEmitter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ namespace ts {
371371

372372
function writeJsDocComments(declaration: Node) {
373373
if (declaration) {
374-
const jsDocComments = getJsDocCommentsFromText(declaration, currentText);
374+
const jsDocComments = getJSDocCommentRanges(declaration, currentText);
375375
emitNewLineBeforeLeadingComments(currentLineMap, writer, declaration, jsDocComments);
376376
// jsDoc comments are emitted at /*leading comment1 */space/*leading comment*/space
377377
emitComments(currentText, currentLineMap, writer, jsDocComments, /*leadingSeparator*/ false, /*trailingSeparator*/ true, newLine, writeCommentRange);

src/compiler/parser.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -681,18 +681,18 @@ namespace ts {
681681

682682

683683
function addJSDocComment<T extends Node>(node: T): T {
684-
const comments = getJsDocCommentsFromText(node, sourceFile.text);
684+
const comments = getJSDocCommentRanges(node, sourceFile.text);
685685
if (comments) {
686686
for (const comment of comments) {
687687
const jsDoc = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos);
688688
if (!jsDoc) {
689689
continue;
690690
}
691691

692-
if (!node.jsDocComments) {
693-
node.jsDocComments = [];
692+
if (!node.jsDoc) {
693+
node.jsDoc = [];
694694
}
695-
node.jsDocComments.push(jsDoc);
695+
node.jsDoc.push(jsDoc);
696696
}
697697
}
698698

@@ -719,11 +719,11 @@ namespace ts {
719719
const saveParent = parent;
720720
parent = n;
721721
forEachChild(n, visitNode);
722-
if (n.jsDocComments) {
723-
for (const jsDocComment of n.jsDocComments) {
724-
jsDocComment.parent = n;
725-
parent = jsDocComment;
726-
forEachChild(jsDocComment, visitNode);
722+
if (n.jsDoc) {
723+
for (const jsDoc of n.jsDoc) {
724+
jsDoc.parent = n;
725+
parent = jsDoc;
726+
forEachChild(jsDoc, visitNode);
727727
}
728728
}
729729
parent = saveParent;
@@ -6954,8 +6954,8 @@ namespace ts {
69546954
}
69556955

69566956
forEachChild(node, visitNode, visitArray);
6957-
if (node.jsDocComments) {
6958-
for (const jsDocComment of node.jsDocComments) {
6957+
if (node.jsDoc) {
6958+
for (const jsDocComment of node.jsDoc) {
69596959
forEachChild(jsDocComment, visitNode, visitArray);
69606960
}
69616961
}

src/compiler/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ namespace ts {
497497
parent?: Node; // Parent node (initialized by binding)
498498
/* @internal */ original?: Node; // The original node if this is an updated node.
499499
/* @internal */ startsOnNewLine?: boolean; // Whether a synthesized node should start on a new line (used by transforms).
500-
/* @internal */ jsDocComments?: JSDoc[]; // JSDoc for the node, if it has any.
500+
/* @internal */ jsDoc?: JSDoc[]; // JSDoc that directly precedes this node
501+
/* @internal */ jsDocCache?: (JSDoc | JSDocTag)[]; // All JSDoc that applies to the node, including parent docs and @param tags
501502
/* @internal */ symbol?: Symbol; // Symbol declared by node (initialized by binding)
502503
/* @internal */ locals?: SymbolTable; // Locals associated with node (initialized by binding)
503504
/* @internal */ nextContainer?: Node; // Next container in declaration order (initialized by binding)

0 commit comments

Comments
 (0)