JSDoc understands string literal types

Unfortunately, I didn't find a way to reuse the normal string literal
type, so I had to extend the existing JSDoc type hierarchy. Otherwise,
this feature is very simple.
This commit is contained in:
Nathan Shively-Sanders 2016-07-27 13:21:42 -07:00
parent c578367c26
commit a6642d68c9
7 changed files with 69 additions and 1 deletions

View File

@ -5326,6 +5326,8 @@ namespace ts {
return getTypeFromThisTypeNode(node);
case SyntaxKind.StringLiteralType:
return getTypeFromStringLiteralTypeNode(<StringLiteralTypeNode>node);
case SyntaxKind.JSDocStringLiteralType:
return getTypeFromStringLiteralTypeNode((<JSDocStringLiteralType>node).stringLiteral);
case SyntaxKind.TypeReference:
case SyntaxKind.JSDocTypeReference:
return getTypeFromTypeReference(<TypeReferenceNode>node);

View File

@ -5860,9 +5860,10 @@ namespace ts {
case SyntaxKind.SymbolKeyword:
case SyntaxKind.VoidKeyword:
return parseTokenNode<JSDocType>();
case SyntaxKind.StringLiteral:
return parseJSDocStringLiteralType();
}
// TODO (drosen): Parse string literal types in JSDoc as well.
return parseJSDocTypeReference();
}
@ -6041,6 +6042,12 @@ namespace ts {
return finishNode(result);
}
function parseJSDocStringLiteralType(): JSDocStringLiteralType {
const result = <JSDocStringLiteralType>createNode(SyntaxKind.JSDocStringLiteralType);
result.stringLiteral = parseStringLiteralTypeNode();
return finishNode(result);
}
function parseJSDocUnknownOrNullableType(): JSDocUnknownType | JSDocNullableType {
const pos = scanner.getStartPos();
// skip the ?

View File

@ -346,6 +346,7 @@ namespace ts {
JSDocTypedefTag,
JSDocPropertyTag,
JSDocTypeLiteral,
JSDocStringLiteralType,
// Synthesized list
SyntaxList,
@ -1491,6 +1492,10 @@ namespace ts {
type: JSDocType;
}
export interface JSDocStringLiteralType extends JSDocType {
stringLiteral: StringLiteralTypeNode;
}
export type JSDocTypeReferencingNode = JSDocThisType | JSDocConstructorType | JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
// @kind(SyntaxKind.JSDocRecordMember)

View File

@ -0,0 +1,16 @@
//// [in.js]
/**
* @param {'literal'} input
*/
function f(input) {
return input + '.';
}
//// [out.js]
/**
* @param {'literal'} input
*/
function f(input) {
return input + '.';
}

View File

@ -0,0 +1,12 @@
=== tests/cases/compiler/in.js ===
/**
* @param {'literal'} input
*/
function f(input) {
>f : Symbol(f, Decl(in.js, 0, 0))
>input : Symbol(input, Decl(in.js, 3, 11))
return input + '.';
>input : Symbol(input, Decl(in.js, 3, 11))
}

View File

@ -0,0 +1,14 @@
=== tests/cases/compiler/in.js ===
/**
* @param {'literal'} input
*/
function f(input) {
>f : (input: "literal") => string
>input : "literal"
return input + '.';
>input + '.' : string
>input : "literal"
>'.' : string
}

View File

@ -0,0 +1,12 @@
// @allowJs: true
// @filename: in.js
// @out: out.js
/**
* @param {'literal'} p1
* @param {"literal"} p2
* @param {'literal' | 'other'} p3
* @param {'literal' | number} p4
*/
function f(p1, p2, p3, p4) {
return p1 + p2 + p3 + p4 + '.';
}