Adds error for non-return positioned type predicates and changed parse type predicate logic

This commit is contained in:
Tingan Ho
2015-06-05 16:36:02 +08:00
parent a73bf31c86
commit fa9a914648
8 changed files with 65 additions and 32 deletions

View File

@@ -8623,8 +8623,7 @@ module ts {
let typePredicateNode = <TypePredicateNode>node.type;
if (typePredicateNode.type.kind === SyntaxKind.TypePredicate) {
error(typePredicateNode.type,
Diagnostics.Cannot_define_type_predicate_0_as_a_type_to_a_type_predicate,
getTextOfNode(typePredicateNode.type));
Diagnostics.Type_predicates_are_only_allowed_in_return_type_position);
}
else {
if (typePredicate.parameterIndex >= 0) {
@@ -11303,6 +11302,13 @@ module ts {
return checkAccessorDeclaration(<AccessorDeclaration>node);
case SyntaxKind.TypeReference:
return checkTypeReferenceNode(<TypeReferenceNode>node);
case SyntaxKind.TypePredicate:
// Issue an error every time we encounter a type predicate. They are only allowed
// in return type positions in signature declarations. checkSignatureDeclaration(..)
// already have a specific check for type predicates, so every time we encounter a type
// predicate in checkSourceElement it must be in a non return type position.
error(node, Diagnostics.Type_predicates_are_only_allowed_in_return_type_position);
return;
case SyntaxKind.TypeQuery:
return checkTypeQuery(<TypeQueryNode>node);
case SyntaxKind.TypeLiteral:

View File

@@ -183,7 +183,7 @@ module ts {
Cannot_find_parameter_0: { code: 1225, category: DiagnosticCategory.Error, key: "Cannot find parameter '{0}'." },
Type_predicate_0_is_not_assignable_to_1: { code: 1226, category: DiagnosticCategory.Error, key: "Type predicate '{0}' is not assignable to '{1}'." },
Parameter_0_is_not_in_the_same_position_as_parameter_1: { code: 1227, category: DiagnosticCategory.Error, key: "Parameter '{0}' is not in the same position as parameter '{1}'." },
Cannot_define_type_predicate_0_as_a_type_to_a_type_predicate: { code: 1228, category: DiagnosticCategory.Error, key: "Cannot define type predicate '{0}' as a type to a type predicate." },
Type_predicates_are_only_allowed_in_return_type_position: { code: 1228, category: DiagnosticCategory.Error, key: "Type predicates are only allowed in return type position." },
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

View File

@@ -719,7 +719,7 @@
"category": "Error",
"code": 1227
},
"Cannot define type predicate '{0}' as a type to a type predicate.": {
"Type predicates are only allowed in return type position.": {
"category": "Error",
"code": 1228
},

View File

@@ -1900,9 +1900,17 @@ module ts {
// TYPES
function parseTypeReference(): TypeReferenceNode {
let node = <TypeReferenceNode>createNode(SyntaxKind.TypeReference);
node.typeName = parseEntityName(/*allowReservedWords*/ false, Diagnostics.Type_expected);
function parseTypeReferenceOrTypePredicate(): TypeReferenceNode | TypePredicateNode {
let typeName = parseEntityName(/*allowReservedWords*/ false, Diagnostics.Type_expected);
if (typeName.kind === SyntaxKind.Identifier && token === SyntaxKind.IsKeyword) {
nextToken();
let node = <TypePredicateNode>createNode(SyntaxKind.TypePredicate, typeName.pos);
node.parameterName = <Identifier>typeName;
node.type = parseType();
return finishNode(node);
}
let node = <TypeReferenceNode>createNode(SyntaxKind.TypeReference, typeName.pos);
node.typeName = typeName;
if (!scanner.hasPrecedingLineBreak() && token === SyntaxKind.LessThanToken) {
node.typeArguments = parseBracketedList(ParsingContext.TypeArguments, parseType, SyntaxKind.LessThanToken, SyntaxKind.GreaterThanToken);
}
@@ -2339,7 +2347,7 @@ module ts {
case SyntaxKind.SymbolKeyword:
// If these are followed by a dot, then parse these out as a dotted type reference instead.
let node = tryParse(parseKeywordAndNoDot);
return node || parseTypeReference();
return node || parseTypeReferenceOrTypePredicate();
case SyntaxKind.VoidKeyword:
return parseTokenNode<TypeNode>();
case SyntaxKind.TypeOfKeyword:
@@ -2351,7 +2359,7 @@ module ts {
case SyntaxKind.OpenParenToken:
return parseParenthesizedType();
default:
return parseTypeReference();
return parseTypeReferenceOrTypePredicate();
}
}
@@ -2464,22 +2472,6 @@ module ts {
return result;
}
function parseTypePredicateOrHigher(): TypeNode {
let type = parseUnionTypeOrHigher();
if (token === SyntaxKind.IsKeyword &&
type.kind === SyntaxKind.TypeReference &&
(<TypeReferenceNode>type).typeName.kind === SyntaxKind.Identifier) {
nextToken();
let typePredicate = <TypePredicateNode>createNode(SyntaxKind.TypePredicate, type.pos);
typePredicate.parameterName = <Identifier>(<TypeReferenceNode>type).typeName;
typePredicate.type = parseType();
return finishNode(typePredicate);
}
return type;
}
function parseTypeWorker(): TypeNode {
if (isStartOfFunctionType()) {
@@ -2488,7 +2480,7 @@ module ts {
if (token === SyntaxKind.NewKeyword) {
return parseFunctionOrConstructorType(SyntaxKind.ConstructorType);
}
return parseTypePredicateOrHigher();
return parseUnionTypeOrHigher();
}
function parseTypeAnnotation(): TypeNode {

View File

@@ -464,7 +464,6 @@ module ts {
typeParameters?: NodeArray<TypeParameterDeclaration>;
parameters: NodeArray<ParameterDeclaration>;
type?: TypeNode;
typePredicate?: TypePredicateNode;
}
// SyntaxKind.VariableDeclaration