Skip to content

Commit fdb4465

Browse files
authored
Merge pull request #17352 from Microsoft/jsdoc-param-type-literals
Parse jsdoc `@param` type literals
2 parents 6f90b31 + 9fd90e7 commit fdb4465

24 files changed

+691
-265
lines changed

src/compiler/binder.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,9 +1504,9 @@ namespace ts {
15041504
return declareSymbol(container.symbol.exports, container.symbol, node, symbolFlags, symbolExcludes);
15051505

15061506
case SyntaxKind.TypeLiteral:
1507+
case SyntaxKind.JSDocTypeLiteral:
15071508
case SyntaxKind.ObjectLiteralExpression:
15081509
case SyntaxKind.InterfaceDeclaration:
1509-
case SyntaxKind.JSDocTypeLiteral:
15101510
case SyntaxKind.JsxAttributes:
15111511
// Interface/Object-types always have their children added to the 'members' of
15121512
// their container. They are only accessible through an instance of their
@@ -2103,8 +2103,9 @@ namespace ts {
21032103
case SyntaxKind.ConstructorType:
21042104
return bindFunctionOrConstructorType(<SignatureDeclaration>node);
21052105
case SyntaxKind.TypeLiteral:
2106+
case SyntaxKind.JSDocTypeLiteral:
21062107
case SyntaxKind.MappedType:
2107-
return bindAnonymousTypeWorker(node as TypeLiteralNode | MappedTypeNode);
2108+
return bindAnonymousTypeWorker(node as TypeLiteralNode | MappedTypeNode | JSDocTypeLiteral);
21082109
case SyntaxKind.ObjectLiteralExpression:
21092110
return bindObjectLiteralExpression(<ObjectLiteralExpression>node);
21102111
case SyntaxKind.FunctionExpression:
@@ -2162,13 +2163,17 @@ namespace ts {
21622163
case SyntaxKind.ModuleBlock:
21632164
return updateStrictModeStatementList((<Block | ModuleBlock>node).statements);
21642165

2166+
case SyntaxKind.JSDocParameterTag:
2167+
if (node.parent.kind !== SyntaxKind.JSDocTypeLiteral) {
2168+
break;
2169+
}
2170+
// falls through
21652171
case SyntaxKind.JSDocPropertyTag:
2166-
return declareSymbolAndAddToSymbolTable(node as JSDocPropertyTag,
2167-
(node as JSDocPropertyTag).isBracketed || ((node as JSDocPropertyTag).typeExpression && (node as JSDocPropertyTag).typeExpression.type.kind === SyntaxKind.JSDocOptionalType) ?
2168-
SymbolFlags.Property | SymbolFlags.Optional : SymbolFlags.Property,
2169-
SymbolFlags.PropertyExcludes);
2170-
case SyntaxKind.JSDocTypeLiteral:
2171-
return bindAnonymousTypeWorker(node as JSDocTypeLiteral);
2172+
const propTag = node as JSDocPropertyLikeTag;
2173+
const flags = propTag.isBracketed || propTag.typeExpression.type.kind === SyntaxKind.JSDocOptionalType ?
2174+
SymbolFlags.Property | SymbolFlags.Optional :
2175+
SymbolFlags.Property;
2176+
return declareSymbolAndAddToSymbolTable(propTag, flags, SymbolFlags.PropertyExcludes);
21722177
case SyntaxKind.JSDocTypedefTag: {
21732178
const { fullName } = node as JSDocTypedefTag;
21742179
if (!fullName || fullName.kind === SyntaxKind.Identifier) {

src/compiler/checker.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,8 +4493,8 @@ namespace ts {
44934493
if (declaration.kind === SyntaxKind.ExportAssignment) {
44944494
return links.type = checkExpression((<ExportAssignment>declaration).expression);
44954495
}
4496-
if (isInJavaScriptFile(declaration) && declaration.kind === SyntaxKind.JSDocPropertyTag && (<JSDocPropertyTag>declaration).typeExpression) {
4497-
return links.type = getTypeFromTypeNode((<JSDocPropertyTag>declaration).typeExpression.type);
4496+
if (isInJavaScriptFile(declaration) && isJSDocPropertyLikeTag(declaration) && declaration.typeExpression) {
4497+
return links.type = getTypeFromTypeNode(declaration.typeExpression.type);
44984498
}
44994499
// Handle variable, parameter or property
45004500
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
@@ -5074,20 +5074,9 @@ namespace ts {
50745074
return unknownType;
50755075
}
50765076

5077-
let declaration: JSDocTypedefTag | TypeAliasDeclaration = getDeclarationOfKind<JSDocTypedefTag>(symbol, SyntaxKind.JSDocTypedefTag);
5078-
let type: Type;
5079-
if (declaration) {
5080-
if (declaration.jsDocTypeLiteral) {
5081-
type = getTypeFromTypeNode(declaration.jsDocTypeLiteral);
5082-
}
5083-
else {
5084-
type = getTypeFromTypeNode(declaration.typeExpression.type);
5085-
}
5086-
}
5087-
else {
5088-
declaration = getDeclarationOfKind<TypeAliasDeclaration>(symbol, SyntaxKind.TypeAliasDeclaration);
5089-
type = getTypeFromTypeNode(declaration.type);
5090-
}
5077+
const declaration = <JSDocTypedefTag | TypeAliasDeclaration>findDeclaration(
5078+
symbol, d => d.kind === SyntaxKind.JSDocTypedefTag || d.kind === SyntaxKind.TypeAliasDeclaration);
5079+
let type = getTypeFromTypeNode(declaration.kind === SyntaxKind.JSDocTypedefTag ? declaration.typeExpression : declaration.type);
50915080

50925081
if (popTypeResolution()) {
50935082
const typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol);
@@ -7662,9 +7651,12 @@ namespace ts {
76627651
links.resolvedType = emptyTypeLiteralType;
76637652
}
76647653
else {
7665-
const type = createObjectType(ObjectFlags.Anonymous, node.symbol);
7654+
let type = createObjectType(ObjectFlags.Anonymous, node.symbol);
76667655
type.aliasSymbol = aliasSymbol;
76677656
type.aliasTypeArguments = getAliasTypeArgumentsForTypeNode(node);
7657+
if (isJSDocTypeLiteral(node) && node.isArrayType) {
7658+
type = createArrayType(type);
7659+
}
76687660
links.resolvedType = type;
76697661
}
76707662
}
@@ -7898,7 +7890,8 @@ namespace ts {
78987890
case SyntaxKind.ParenthesizedType:
78997891
case SyntaxKind.JSDocNonNullableType:
79007892
case SyntaxKind.JSDocOptionalType:
7901-
return getTypeFromTypeNode((<ParenthesizedTypeNode | JSDocTypeReferencingNode>node).type);
7893+
case SyntaxKind.JSDocTypeExpression:
7894+
return getTypeFromTypeNode((<ParenthesizedTypeNode | JSDocTypeReferencingNode | JSDocTypeExpression>node).type);
79027895
case SyntaxKind.FunctionType:
79037896
case SyntaxKind.ConstructorType:
79047897
case SyntaxKind.TypeLiteral:
@@ -22646,8 +22639,7 @@ namespace ts {
2264622639
}
2264722640

2264822641
if (entityName.parent!.kind === SyntaxKind.JSDocParameterTag) {
22649-
const parameter = getParameterFromJSDoc(entityName.parent as JSDocParameterTag);
22650-
return parameter && parameter.symbol;
22642+
return getParameterSymbolFromJSDoc(entityName.parent as JSDocParameterTag);
2265122643
}
2265222644

2265322645
if (entityName.parent.kind === SyntaxKind.TypeParameter && entityName.parent.parent.kind === SyntaxKind.JSDocTemplateTag) {

0 commit comments

Comments
 (0)