Skip to content

Commit 22769d9

Browse files
authored
Merge pull request #18747 from Microsoft/refactor-jsdoc-types-to-typescript
Refactor jsdoc types to typescript
2 parents 027528e + 4cf06bb commit 22769d9

26 files changed

+629
-14
lines changed

Diff for: src/compiler/diagnosticMessages.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -3758,12 +3758,20 @@
37583758
"category": "Message",
37593759
"code": 95008
37603760
},
3761-
"Infer type of '{0}' from usage.": {
3761+
"Annotate with type from JSDoc": {
37623762
"category": "Message",
37633763
"code": 95009
37643764
},
3765-
"Infer parameter types from usage.": {
3765+
"Annotate with types from JSDoc": {
37663766
"category": "Message",
37673767
"code": 95010
3768+
},
3769+
"Infer type of '{0}' from usage.": {
3770+
"category": "Message",
3771+
"code": 95011
3772+
},
3773+
"Infer parameter types from usage.": {
3774+
"category": "Message",
3775+
"code": 95012
37683776
}
37693777
}

Diff for: src/compiler/emitter.ts

+53-2
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ namespace ts {
546546
return emitTypeReference(<TypeReferenceNode>node);
547547
case SyntaxKind.FunctionType:
548548
return emitFunctionType(<FunctionTypeNode>node);
549+
case SyntaxKind.JSDocFunctionType:
550+
return emitJSDocFunctionType(node as JSDocFunctionType);
549551
case SyntaxKind.ConstructorType:
550552
return emitConstructorType(<ConstructorTypeNode>node);
551553
case SyntaxKind.TypeQuery:
@@ -574,6 +576,20 @@ namespace ts {
574576
return emitMappedType(<MappedTypeNode>node);
575577
case SyntaxKind.LiteralType:
576578
return emitLiteralType(<LiteralTypeNode>node);
579+
case SyntaxKind.JSDocAllType:
580+
write("*");
581+
return;
582+
case SyntaxKind.JSDocUnknownType:
583+
write("?");
584+
return;
585+
case SyntaxKind.JSDocNullableType:
586+
return emitJSDocNullableType(node as JSDocNullableType);
587+
case SyntaxKind.JSDocNonNullableType:
588+
return emitJSDocNonNullableType(node as JSDocNonNullableType);
589+
case SyntaxKind.JSDocOptionalType:
590+
return emitJSDocOptionalType(node as JSDocOptionalType);
591+
case SyntaxKind.JSDocVariadicType:
592+
return emitJSDocVariadicType(node as JSDocVariadicType);
577593

578594
// Binding patterns
579595
case SyntaxKind.ObjectBindingPattern:
@@ -914,9 +930,16 @@ namespace ts {
914930
emitDecorators(node, node.decorators);
915931
emitModifiers(node, node.modifiers);
916932
emitIfPresent(node.dotDotDotToken);
917-
emit(node.name);
933+
if (node.name) {
934+
emit(node.name);
935+
}
918936
emitIfPresent(node.questionToken);
919-
emitWithPrefix(": ", node.type);
937+
if (node.parent && node.parent.kind === SyntaxKind.JSDocFunctionType && !node.name) {
938+
emit(node.type);
939+
}
940+
else {
941+
emitWithPrefix(": ", node.type);
942+
}
920943
emitExpressionWithPrefix(" = ", node.initializer);
921944
}
922945

@@ -1035,6 +1058,29 @@ namespace ts {
10351058
emit(node.type);
10361059
}
10371060

1061+
function emitJSDocFunctionType(node: JSDocFunctionType) {
1062+
write("function");
1063+
emitParameters(node, node.parameters);
1064+
write(":");
1065+
emit(node.type);
1066+
}
1067+
1068+
1069+
function emitJSDocNullableType(node: JSDocNullableType) {
1070+
write("?");
1071+
emit(node.type);
1072+
}
1073+
1074+
function emitJSDocNonNullableType(node: JSDocNonNullableType) {
1075+
write("!");
1076+
emit(node.type);
1077+
}
1078+
1079+
function emitJSDocOptionalType(node: JSDocOptionalType) {
1080+
emit(node.type);
1081+
write("=");
1082+
}
1083+
10381084
function emitConstructorType(node: ConstructorTypeNode) {
10391085
write("new ");
10401086
emitTypeParameters(node, node.typeParameters);
@@ -1060,6 +1106,11 @@ namespace ts {
10601106
write("[]");
10611107
}
10621108

1109+
function emitJSDocVariadicType(node: JSDocVariadicType) {
1110+
write("...");
1111+
emit(node.type);
1112+
}
1113+
10631114
function emitTupleType(node: TupleTypeNode) {
10641115
write("[");
10651116
emitList(node, node.elementTypes, ListFormat.TupleTypeElements);

Diff for: src/compiler/utilities.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -2700,11 +2700,11 @@ namespace ts {
27002700
* Gets the effective type annotation of a variable, parameter, or property. If the node was
27012701
* parsed in a JavaScript file, gets the type annotation from JSDoc.
27022702
*/
2703-
export function getEffectiveTypeAnnotationNode(node: VariableLikeDeclaration): TypeNode | undefined {
2703+
export function getEffectiveTypeAnnotationNode(node: VariableLikeDeclaration, checkJSDoc?: boolean): TypeNode | undefined {
27042704
if (node.type) {
27052705
return node.type;
27062706
}
2707-
if (isInJavaScriptFile(node)) {
2707+
if (checkJSDoc || isInJavaScriptFile(node)) {
27082708
return getJSDocType(node);
27092709
}
27102710
}
@@ -2713,11 +2713,11 @@ namespace ts {
27132713
* Gets the effective return type annotation of a signature. If the node was parsed in a
27142714
* JavaScript file, gets the return type annotation from JSDoc.
27152715
*/
2716-
export function getEffectiveReturnTypeNode(node: SignatureDeclaration): TypeNode | undefined {
2716+
export function getEffectiveReturnTypeNode(node: SignatureDeclaration, checkJSDoc?: boolean): TypeNode | undefined {
27172717
if (node.type) {
27182718
return node.type;
27192719
}
2720-
if (isInJavaScriptFile(node)) {
2720+
if (checkJSDoc || isInJavaScriptFile(node)) {
27212721
return getJSDocReturnType(node);
27222722
}
27232723
}
@@ -2726,11 +2726,11 @@ namespace ts {
27262726
* Gets the effective type parameters. If the node was parsed in a
27272727
* JavaScript file, gets the type parameters from the `@template` tag from JSDoc.
27282728
*/
2729-
export function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters): ReadonlyArray<TypeParameterDeclaration> {
2729+
export function getEffectiveTypeParameterDeclarations(node: DeclarationWithTypeParameters, checkJSDoc?: boolean): ReadonlyArray<TypeParameterDeclaration> {
27302730
if (node.typeParameters) {
27312731
return node.typeParameters;
27322732
}
2733-
if (isInJavaScriptFile(node)) {
2733+
if (checkJSDoc || isInJavaScriptFile(node)) {
27342734
const templateTag = getJSDocTemplateTag(node);
27352735
return templateTag && templateTag.typeParameters;
27362736
}
@@ -2740,9 +2740,9 @@ namespace ts {
27402740
* Gets the effective type annotation of the value parameter of a set accessor. If the node
27412741
* was parsed in a JavaScript file, gets the type annotation from JSDoc.
27422742
*/
2743-
export function getEffectiveSetAccessorTypeAnnotationNode(node: SetAccessorDeclaration): TypeNode {
2743+
export function getEffectiveSetAccessorTypeAnnotationNode(node: SetAccessorDeclaration, checkJSDoc?: boolean): TypeNode {
27442744
const parameter = getSetAccessorValueParameter(node);
2745-
return parameter && getEffectiveTypeAnnotationNode(parameter);
2745+
return parameter && getEffectiveTypeAnnotationNode(parameter, checkJSDoc);
27462746
}
27472747

27482748
export function emitNewLineBeforeLeadingComments(lineMap: ReadonlyArray<number>, writer: EmitTextWriter, node: TextRange, leadingComments: ReadonlyArray<CommentRange>) {
@@ -5130,7 +5130,14 @@ namespace ts {
51305130
|| kind === SyntaxKind.UndefinedKeyword
51315131
|| kind === SyntaxKind.NullKeyword
51325132
|| kind === SyntaxKind.NeverKeyword
5133-
|| kind === SyntaxKind.ExpressionWithTypeArguments;
5133+
|| kind === SyntaxKind.ExpressionWithTypeArguments
5134+
|| kind === SyntaxKind.JSDocAllType
5135+
|| kind === SyntaxKind.JSDocUnknownType
5136+
|| kind === SyntaxKind.JSDocNullableType
5137+
|| kind === SyntaxKind.JSDocNonNullableType
5138+
|| kind === SyntaxKind.JSDocOptionalType
5139+
|| kind === SyntaxKind.JSDocFunctionType
5140+
|| kind === SyntaxKind.JSDocVariadicType;
51345141
}
51355142

51365143
/**

0 commit comments

Comments
 (0)