mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-26 21:23:53 -06:00
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:
parent
c578367c26
commit
a6642d68c9
@ -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);
|
||||
|
||||
@ -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 ?
|
||||
|
||||
@ -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)
|
||||
|
||||
16
tests/baselines/reference/jsdocStringLiteral.js
Normal file
16
tests/baselines/reference/jsdocStringLiteral.js
Normal 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 + '.';
|
||||
}
|
||||
12
tests/baselines/reference/jsdocStringLiteral.symbols
Normal file
12
tests/baselines/reference/jsdocStringLiteral.symbols
Normal 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))
|
||||
}
|
||||
|
||||
14
tests/baselines/reference/jsdocStringLiteral.types
Normal file
14
tests/baselines/reference/jsdocStringLiteral.types
Normal 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
|
||||
}
|
||||
|
||||
12
tests/cases/conformance/jsdoc/jsdocStringLiteral.ts
Normal file
12
tests/cases/conformance/jsdoc/jsdocStringLiteral.ts
Normal 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 + '.';
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user