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
+8 -2
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:
@@ -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." },
+1 -1
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
},
+14 -22
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 {
-1
View File
@@ -464,7 +464,6 @@ module ts {
typeParameters?: NodeArray<TypeParameterDeclaration>;
parameters: NodeArray<ParameterDeclaration>;
type?: TypeNode;
typePredicate?: TypePredicateNode;
}
// SyntaxKind.VariableDeclaration
@@ -1,5 +1,5 @@
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(15,12): error TS2322: Type 'string' is not assignable to type 'boolean'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(18,55): error TS1228: Cannot define type predicate 'x is A' as a type to a type predicate.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(18,55): error TS1228: Type predicates are only allowed in return type position.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(22,33): error TS2304: Cannot find name 'x'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(26,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(27,5): error TS1131: Property or signature expected.
@@ -22,9 +22,12 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(81,1):
Parameter 'p2' is not in the same position as parameter 'p1'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(87,1): error TS2322: Type '(p1: any, p2: any, p3: any) => boolean' is not assignable to type '(p1: any, p2: any) => boolean'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(92,56): error TS1225: Cannot find parameter 'p1'.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(95,9): error TS1228: Type predicates are only allowed in return type position.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(96,16): error TS1228: Type predicates are only allowed in return type position.
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(97,20): error TS1228: Type predicates are only allowed in return type position.
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (18 errors) ====
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (21 errors) ====
class A {
propA: number;
@@ -46,7 +49,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(92,56)
function hasTypeGuardTypeInsideTypeGuardType(x): x is x is A {
~~~~~~
!!! error TS1228: Cannot define type predicate 'x is A' as a type to a type predicate.
!!! error TS1228: Type predicates are only allowed in return type position.
return true;
}
@@ -159,4 +162,16 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(92,56)
declare function destructureParameter({ p1, p2, p3 }): p1 is A;
~~
!!! error TS1225: Cannot find parameter 'p1'.
// Type predicates in non-return type positions
var b1: b is A;
~~~~~~
!!! error TS1228: Type predicates are only allowed in return type position.
function b2(a: b is A) {};
~~~~~~
!!! error TS1228: Type predicates are only allowed in return type position.
function b3(): A | b is A {
~~~~~~
!!! error TS1228: Type predicates are only allowed in return type position.
return true;
};
@@ -91,7 +91,13 @@ assign3 = function(p1, p2, p3): p1 is A {
// Type guard paramater referring to a binding pattern
declare function destructureParameter({ p1, p2, p3 }): p1 is A;
// Type predicates in non-return type positions
var b1: b is A;
function b2(a: b is A) {};
function b3(): A | b is A {
return true;
};
//// [typeGuardFunctionErrors.js]
var __extends = (this && this.__extends) || function (d, b) {
@@ -169,3 +175,11 @@ var assign3;
assign3 = function (p1, p2, p3) {
return true;
};
// Type predicates in non-return type positions
var b1;
function b2(a) { }
;
function b3() {
return true;
}
;
@@ -90,3 +90,10 @@ assign3 = function(p1, p2, p3): p1 is A {
// Type guard paramater referring to a binding pattern
declare function destructureParameter({ p1, p2, p3 }): p1 is A;
// Type predicates in non-return type positions
var b1: b is A;
function b2(a: b is A) {};
function b3(): A | b is A {
return true;
};