Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse jsdoc with normal TS type parser #17176

Merged
merged 14 commits into from
Jul 17, 2017
Merged
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6866,6 +6866,10 @@ namespace ts {
}
}

function isJSDocTypeReference(node: TypeReferenceType): node is TypeReferenceNode {
return node.flags & NodeFlags.JSDoc && node.kind === SyntaxKind.TypeReference;
}

function getPrimitiveTypeFromJSDocTypeReference(node: TypeReferenceNode): Type {
if (isIdentifier(node.typeName)) {
switch (node.typeName.text) {
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2199,7 +2199,11 @@ namespace ts {
}

function isStartOfParameter(): boolean {
return token() === SyntaxKind.DotDotDotToken || isIdentifierOrPattern() || isModifierKind(token()) || token() === SyntaxKind.AtToken || token() === SyntaxKind.ThisKeyword || token() === SyntaxKind.NewKeyword;
return token() === SyntaxKind.DotDotDotToken ||
isIdentifierOrPattern() ||
isModifierKind(token()) ||
token() === SyntaxKind.AtToken || token() === SyntaxKind.ThisKeyword || token() === SyntaxKind.NewKeyword ||
token() === SyntaxKind.StringLiteral || token() === SyntaxKind.NumericLiteral;
}

function parseParameter(): ParameterDeclaration {
Expand Down
4 changes: 0 additions & 4 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3302,10 +3302,6 @@ namespace ts {
return false;
}

export function isJSDocTypeReference(node: TypeReferenceType): node is TypeReferenceNode {
return node.flags & NodeFlags.JSDoc && node.kind === SyntaxKind.TypeReference;
}

/**
* Formats an enum value as a string for debugging and debug assertions.
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/jsdocFunctionType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ z.length;
>z : Symbol(z, Decl(functions.js, 26, 3))
>length : Symbol(length, Decl(functions.js, 12, 27))

/** @type {function ("a" | "b"): 1 | 2} */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually test the ability to parse a number literal as a parameter type, that would be function("a" | "b", 1 | 2): whatever.

var f = function (s) { return s === "a" ? 1 : 2; }
>f : Symbol(f, Decl(functions.js, 30, 3))
>s : Symbol(s, Decl(functions.js, 30, 18))
>s : Symbol(s, Decl(functions.js, 30, 18))

12 changes: 12 additions & 0 deletions tests/baselines/reference/jsdocFunctionType.types
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,15 @@ z.length;
>z : { length: number; }
>length : number

/** @type {function ("a" | "b"): 1 | 2} */
var f = function (s) { return s === "a" ? 1 : 2; }
>f : (arg0: "a" | "b") => 1 | 2
>function (s) { return s === "a" ? 1 : 2; } : (s: "a" | "b") => 1 | 2
>s : "a" | "b"
>s === "a" ? 1 : 2 : 1 | 2
>s === "a" : boolean
>s : "a" | "b"
>"a" : "a"
>1 : 1
>2 : 2

29 changes: 0 additions & 29 deletions tests/baselines/reference/jsdocTypesInTypeAnnotations.errors.txt

This file was deleted.

14 changes: 0 additions & 14 deletions tests/baselines/reference/jsweird.symbols

This file was deleted.

16 changes: 0 additions & 16 deletions tests/baselines/reference/jsweird.types

This file was deleted.

3 changes: 3 additions & 0 deletions tests/cases/conformance/jsdoc/jsdocFunctionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ class C {
var y = id2(C);
var z = new y(12);
z.length;

/** @type {function ("a" | "b"): 1 | 2} */
var f = function (s) { return s === "a" ? 1 : 2; }
22 changes: 0 additions & 22 deletions tests/cases/conformance/jsdoc/jsdocTypesInTypeAnnotations.ts

This file was deleted.