Fixes CR feedback

This commit is contained in:
Tingan Ho
2015-06-06 11:27:55 +08:00
parent fa9a914648
commit 487dff564f
10 changed files with 352 additions and 58 deletions

View File

@@ -5642,13 +5642,13 @@ module ts {
}
if (targetType) {
return getOptionalNarrowedType(type, targetType);
return getNarrowedType(type, targetType);
}
return type;
}
function getOptionalNarrowedType(originalType: Type, narrowedTypeCandidate: Type) {
function getNarrowedType(originalType: Type, narrowedTypeCandidate: Type) {
// Narrow to the target type if it's a subtype of the current type
if (isTypeSubtypeOf(narrowedTypeCandidate, originalType)) {
return narrowedTypeCandidate;
@@ -5675,7 +5675,7 @@ module ts {
}
return type;
}
return getOptionalNarrowedType(type, signature.typePredicate.type);
return getNarrowedType(type, signature.typePredicate.type);
}
return type;
}
@@ -8601,6 +8601,19 @@ module ts {
}
}
function isInATypePredicateCompatiblePosition(node: Node): boolean {
switch (node.parent.kind) {
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionType:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
return true;
}
return false;
}
function checkSignatureDeclaration(node: SignatureDeclaration) {
// Grammar checking
if (node.kind === SyntaxKind.IndexSignature) {
@@ -8621,15 +8634,17 @@ module ts {
if (node.type.kind === SyntaxKind.TypePredicate) {
let typePredicate = getSignatureFromDeclaration(node).typePredicate;
let typePredicateNode = <TypePredicateNode>node.type;
if (typePredicateNode.type.kind === SyntaxKind.TypePredicate) {
error(typePredicateNode.type,
Diagnostics.Type_predicates_are_only_allowed_in_return_type_position);
}
else {
if (isInATypePredicateCompatiblePosition(typePredicateNode)) {
if (typePredicate.parameterIndex >= 0) {
checkTypeAssignableTo(typePredicate.type,
getTypeAtLocation(node.parameters[typePredicate.parameterIndex]),
typePredicateNode.type);
if (node.parameters[typePredicate.parameterIndex].dotDotDotToken) {
error(typePredicateNode.parameterName,
Diagnostics.Type_predicate_cannot_reference_a_spread_parameter);
}
else {
checkTypeAssignableTo(typePredicate.type,
getTypeAtLocation(node.parameters[typePredicate.parameterIndex]),
typePredicateNode.type);
}
}
else if (typePredicateNode.parameterName) {
error(typePredicateNode.parameterName,
@@ -8637,6 +8652,10 @@ module ts {
typePredicate.parameterName);
}
}
else {
error(typePredicateNode,
Diagnostics.Type_predicates_are_only_allowed_in_return_type_position_for_arrow_functions_function_expressions_function_declarations_function_types_and_method_declarations);
}
}
else {
checkSourceElement(node.type);
@@ -11275,6 +11294,12 @@ module ts {
}
}
function checkTypePredicate(node: TypePredicateNode) {
if(!isInATypePredicateCompatiblePosition(node)) {
error(node, Diagnostics.Type_predicates_are_only_allowed_in_return_type_position_for_arrow_functions_function_expressions_function_declarations_function_types_and_method_declarations);
}
}
function checkSourceElement(node: Node): void {
if (!node) return;
switch (node.kind) {
@@ -11303,12 +11328,7 @@ module ts {
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;
return checkTypePredicate(<TypePredicateNode>node);
case SyntaxKind.TypeQuery:
return checkTypeQuery(<TypeQueryNode>node);
case SyntaxKind.TypeLiteral:

View File

@@ -183,7 +183,8 @@ 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}'." },
Type_predicates_are_only_allowed_in_return_type_position: { code: 1228, category: DiagnosticCategory.Error, key: "Type predicates are only allowed in return type position." },
Type_predicates_are_only_allowed_in_return_type_position_for_arrow_functions_function_expressions_function_declarations_function_types_and_method_declarations: { code: 1228, category: DiagnosticCategory.Error, key: "Type predicates are only allowed in return type position for arrow functions, function expressions, function declarations, function types and method declarations." },
Type_predicate_cannot_reference_a_spread_parameter: { code: 1229, category: DiagnosticCategory.Error, key: "Type predicate cannot reference a spread parameter." },
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,10 +719,14 @@
"category": "Error",
"code": 1227
},
"Type predicates are only allowed in return type position.": {
"Type predicates are only allowed in return type position for arrow functions, function expressions, function declarations, function types and method declarations.": {
"category": "Error",
"code": 1228
},
"Type predicate cannot reference a spread parameter.": {
"category": "Error",
"code": 1229
},
"Duplicate identifier '{0}'.": {