Parsing of nullable types

This commit is contained in:
Anders Hejlsberg
2016-02-14 18:59:58 -08:00
parent 26cc99b92d
commit 8e926035b7
2 changed files with 24 additions and 7 deletions

View File

@@ -127,7 +127,8 @@ namespace ts {
case SyntaxKind.IntersectionType:
return visitNodes(cbNodes, (<UnionOrIntersectionTypeNode>node).types);
case SyntaxKind.ParenthesizedType:
return visitNode(cbNode, (<ParenthesizedTypeNode>node).type);
case SyntaxKind.NullableType:
return visitNode(cbNode, (<ParenthesizedTypeNode | NullableTypeNode>node).type);
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.ArrayBindingPattern:
return visitNodes(cbNodes, (<BindingPattern>node).elements);
@@ -2413,11 +2414,21 @@ namespace ts {
function parseArrayTypeOrHigher(): TypeNode {
let type = parseNonArrayType();
while (!scanner.hasPrecedingLineBreak() && parseOptional(SyntaxKind.OpenBracketToken)) {
parseExpected(SyntaxKind.CloseBracketToken);
const node = <ArrayTypeNode>createNode(SyntaxKind.ArrayType, type.pos);
node.elementType = type;
type = finishNode(node);
while (!scanner.hasPrecedingLineBreak()) {
if (parseOptional(SyntaxKind.OpenBracketToken)) {
parseExpected(SyntaxKind.CloseBracketToken);
const node = <ArrayTypeNode>createNode(SyntaxKind.ArrayType, type.pos);
node.elementType = type;
type = finishNode(node);
}
else if (parseOptional(SyntaxKind.QuestionToken)) {
const node = <NullableTypeNode>createNode(SyntaxKind.NullableType, type.pos);
node.type = type;
type = finishNode(node);
}
else {
break;
}
}
return type;
}

View File

@@ -208,6 +208,7 @@ namespace ts {
ParenthesizedType,
ThisType,
StringLiteralType,
NullableType,
// Binding patterns
ObjectBindingPattern,
ArrayBindingPattern,
@@ -353,7 +354,7 @@ namespace ts {
FirstFutureReservedWord = ImplementsKeyword,
LastFutureReservedWord = YieldKeyword,
FirstTypeNode = TypePredicate,
LastTypeNode = StringLiteralType,
LastTypeNode = NullableType,
FirstPunctuation = OpenBraceToken,
LastPunctuation = CaretEqualsToken,
FirstToken = Unknown,
@@ -777,6 +778,11 @@ namespace ts {
_stringLiteralTypeBrand: any;
}
// @kind(SyntaxKind.NullableType)
export interface NullableTypeNode extends TypeNode {
type: TypeNode;
}
// @kind(SyntaxKind.StringLiteral)
export interface StringLiteral extends LiteralExpression {
_stringLiteralBrand: any;