Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support other (new) literal types in jsdoc
  • Loading branch information
sandersn committed Aug 4, 2016
commit 3c32478b8fd433b5bdf708b27aef04309a107a3d
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5529,8 +5529,8 @@ namespace ts {
return getTypeFromThisTypeNode(node);
case SyntaxKind.LiteralType:
return getTypeFromLiteralTypeNode(<LiteralTypeNode>node);
case SyntaxKind.JSDocStringLiteralType:
return getTypeFromStringLiteralTypeNode((<JSDocStringLiteralType>node).stringLiteral);
case SyntaxKind.JSDocLiteralType:
return getTypeFromLiteralTypeNode((<JSDocLiteralType>node).literal);
case SyntaxKind.TypeReference:
case SyntaxKind.JSDocTypeReference:
return getTypeFromTypeReference(<TypeReferenceNode>node);
Expand Down
13 changes: 9 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ namespace ts {
case SyntaxKind.JSDocPropertyTag:
return visitNode(cbNode, (<JSDocPropertyTag>node).typeExpression) ||
visitNode(cbNode, (<JSDocPropertyTag>node).name);
case SyntaxKind.JSDocLiteralType:
return visitNode(cbNode, (<JSDocLiteralType>node).literal);
}
}

Expand Down Expand Up @@ -5890,7 +5892,10 @@ namespace ts {
case SyntaxKind.VoidKeyword:
return parseTokenNode<JSDocType>();
case SyntaxKind.StringLiteral:
return parseJSDocStringLiteralType();
case SyntaxKind.NumericLiteral:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
Copy link
Member Author

Choose a reason for hiding this comment

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

need NeverKeyword and UndefinedKeyword here too

Copy link
Contributor

Choose a reason for hiding this comment

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

and NullKeyword as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, those are not literal types, it turns out. In the interests of getting this PR in, I'll do those separately.

Copy link
Member Author

Choose a reason for hiding this comment

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

They probably need their own special handling (see the lines preceding the diff in checker)

return parseJSDocLiteralType();
}

return parseJSDocTypeReference();
Expand Down Expand Up @@ -6071,9 +6076,9 @@ namespace ts {
return finishNode(result);
}

function parseJSDocStringLiteralType(): JSDocStringLiteralType {
const result = <JSDocStringLiteralType>createNode(SyntaxKind.JSDocStringLiteralType);
result.stringLiteral = parseStringLiteralTypeNode();
function parseJSDocLiteralType(): JSDocLiteralType {
const result = <JSDocLiteralType>createNode(SyntaxKind.JSDocLiteralType);
result.literal = parseLiteralTypeNode();
return finishNode(result);
}

Expand Down
10 changes: 5 additions & 5 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ namespace ts {
JSDocTypedefTag,
JSDocPropertyTag,
JSDocTypeLiteral,
JSDocStringLiteralType,
JSDocLiteralType,

Copy link
Contributor

Choose a reason for hiding this comment

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

You may need to update the LastJSDocNode and LastJSDocTagNode pointers

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

// Synthesized list
SyntaxList,
Expand Down Expand Up @@ -377,9 +377,9 @@ namespace ts {
LastBinaryOperator = CaretEqualsToken,
FirstNode = QualifiedName,
FirstJSDocNode = JSDocTypeExpression,
LastJSDocNode = JSDocStringLiteralType,
LastJSDocNode = JSDocLiteralType,
FirstJSDocTagNode = JSDocComment,
LastJSDocTagNode = JSDocStringLiteralType
LastJSDocTagNode = JSDocLiteralType
}

export const enum NodeFlags {
Expand Down Expand Up @@ -1493,8 +1493,8 @@ namespace ts {
type: JSDocType;
}

export interface JSDocStringLiteralType extends JSDocType {
stringLiteral: StringLiteralTypeNode;
export interface JSDocLiteralType extends JSDocType {
literal: LiteralTypeNode;
}

export type JSDocTypeReferencingNode = JSDocThisType | JSDocConstructorType | JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* @param {"literal"} p2
* @param {'literal' | 'other'} p3
* @param {'literal' | number} p4
* @param {12 | true | 'str'} p5
*/
function f(p1, p2, p3, p4) {
return p1 + p2 + p3 + p4 + '.';
function f(p1, p2, p3, p4, p5) {
return p1 + p2 + p3 + p4 + p5 + '.';
}


Expand All @@ -16,7 +17,8 @@ function f(p1, p2, p3, p4) {
* @param {"literal"} p2
* @param {'literal' | 'other'} p3
* @param {'literal' | number} p4
* @param {12 | true | 'str'} p5
*/
function f(p1, p2, p3, p4) {
return p1 + p2 + p3 + p4 + '.';
function f(p1, p2, p3, p4, p5) {
return p1 + p2 + p3 + p4 + p5 + '.';
}
24 changes: 24 additions & 0 deletions tests/baselines/reference/jsdocLiteral.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/conformance/jsdoc/in.js ===
/**
* @param {'literal'} p1
* @param {"literal"} p2
* @param {'literal' | 'other'} p3
* @param {'literal' | number} p4
* @param {12 | true | 'str'} p5
*/
function f(p1, p2, p3, p4, p5) {
>f : Symbol(f, Decl(in.js, 0, 0))
>p1 : Symbol(p1, Decl(in.js, 7, 11))
>p2 : Symbol(p2, Decl(in.js, 7, 14))
>p3 : Symbol(p3, Decl(in.js, 7, 18))
>p4 : Symbol(p4, Decl(in.js, 7, 22))
>p5 : Symbol(p5, Decl(in.js, 7, 26))

return p1 + p2 + p3 + p4 + p5 + '.';
>p1 : Symbol(p1, Decl(in.js, 7, 11))
>p2 : Symbol(p2, Decl(in.js, 7, 14))
>p3 : Symbol(p3, Decl(in.js, 7, 18))
>p4 : Symbol(p4, Decl(in.js, 7, 22))
>p5 : Symbol(p5, Decl(in.js, 7, 26))
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
* @param {"literal"} p2
* @param {'literal' | 'other'} p3
* @param {'literal' | number} p4
* @param {12 | true | 'str'} p5
*/
function f(p1, p2, p3, p4) {
>f : (p1: "literal", p2: "literal", p3: "literal" | "other", p4: "literal" | number) => string
function f(p1, p2, p3, p4, p5) {
>f : (p1: "literal", p2: "literal", p3: "literal" | "other", p4: number | "literal", p5: true | 12 | "str") => string
>p1 : "literal"
>p2 : "literal"
>p3 : "literal" | "other"
>p4 : "literal" | number
>p4 : number | "literal"
>p5 : true | 12 | "str"

return p1 + p2 + p3 + p4 + '.';
>p1 + p2 + p3 + p4 + '.' : string
return p1 + p2 + p3 + p4 + p5 + '.';
>p1 + p2 + p3 + p4 + p5 + '.' : string
>p1 + p2 + p3 + p4 + p5 : string
>p1 + p2 + p3 + p4 : string
>p1 + p2 + p3 : string
>p1 + p2 : string
>p1 : "literal"
>p2 : "literal"
>p3 : "literal" | "other"
>p4 : "literal" | number
>p4 : number | "literal"
>p5 : true | 12 | "str"
>'.' : string
}

21 changes: 0 additions & 21 deletions tests/baselines/reference/jsdocStringLiteral.symbols

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* @param {"literal"} p2
* @param {'literal' | 'other'} p3
* @param {'literal' | number} p4
* @param {12 | true | 'str'} p5
*/
function f(p1, p2, p3, p4) {
return p1 + p2 + p3 + p4 + '.';
function f(p1, p2, p3, p4, p5) {
return p1 + p2 + p3 + p4 + p5 + '.';
}
7 changes: 4 additions & 3 deletions tests/cases/fourslash/completionForStringLiteral4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
//// * @param {"literal"} p2
//// * @param {'other1' | 'other2'} p3
//// * @param {'literal' | number} p4
//// * @param {12 | true} p5
//// */
////function f(p1, p2, p3, p4) {
//// return p1 + p2 + p3 + p4 + '.';
////function f(p1, p2, p3, p4, p5) {
//// return p1 + p2 + p3 + p4 + p5 + '.';
////}
////f/*1*/('literal', 'literal', "o/*2*/ther1", 12);

goTo.marker('1');
verify.quickInfoExists();
verify.quickInfoIs('function f(p1: "literal", p2: "literal", p3: "other1" | "other2", p4: "literal" | number): string', 'I am documentation');
verify.quickInfoIs('function f(p1: "literal", p2: "literal", p3: "other1" | "other2", p4: number | "literal", p5: true | 12): string', 'I am documentation');

goTo.marker('2');
verify.completionListContains("other1");
Expand Down