diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cf35c1c02f5..bcd0adfb40d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5022,12 +5022,16 @@ module ts { if (leftType.flags & (TypeFlags.Undefined | TypeFlags.Null)) leftType = rightType; if (rightType.flags & (TypeFlags.Undefined | TypeFlags.Null)) rightType = leftType; - var leftOk = checkArithmeticOperandType(node.left, leftType, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); - var rightOk = checkArithmeticOperandType(node.right, rightType, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); - if (leftOk && rightOk) { - checkAssignmentOperator(numberType); - } - + var boolean = typeToString(booleanType); + if (typeToString(leftType) === boolean && typeToString(rightType) === boolean) { + error(node, Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_1, tokenToString(node.operator), preferredBooleanOperator(node.operator)); + } else { + var leftOk = checkArithmeticOperandType(node.left, leftType, Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); + var rightOk = checkArithmeticOperandType(node.right, rightType, Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); + if (leftOk && rightOk) { + checkAssignmentOperator(numberType); + } + } return numberType; case SyntaxKind.PlusToken: case SyntaxKind.PlusEqualsToken: @@ -5091,6 +5095,37 @@ module ts { return rightType; } + function preferredBooleanOperator(operator: any): string { + var message = ""; + var suggestion:SyntaxKind; + switch (operator) { + case SyntaxKind.BarToken: + suggestion = SyntaxKind.BarBarToken; + break; + case SyntaxKind.CaretToken: + suggestion = SyntaxKind.ExclamationEqualsEqualsToken; + break; + case SyntaxKind.AmpersandToken: + suggestion = SyntaxKind.AmpersandAmpersandToken; + break; + case SyntaxKind.CaretEqualsToken: + suggestion = SyntaxKind.CaretEqualsToken; + break; + case SyntaxKind.BarEqualsToken: + suggestion = SyntaxKind.BarEqualsToken; + break; + case SyntaxKind.AmpersandEqualsToken: + suggestion = SyntaxKind.AmpersandEqualsToken; + break; + } + if (suggestion !== undefined) { + return "Consider using " + tokenToString(suggestion) + "."; + //message = createCompilerDiagnostic(Diagnostics.Consider_using_0_instead, tokenToString(suggestion)); + //message = message(node, Diagnostics.Consider_using_0_instead, tokenToString(suggestion)); + } + return message; + } + function checkAssignmentOperator(valueType: Type): void { if (fullTypeCheck && operator >= SyntaxKind.FirstAssignment && operator <= SyntaxKind.LastAssignment) { // TypeScript 1.0 spec (April 2014): 4.17 diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index ba6f742c4c1..e4889cd5741 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -260,6 +260,7 @@ module ts { Property_0_is_protected_in_type_1_but_public_in_type_2: { code: 2444, category: DiagnosticCategory.Error, key: "Property '{0}' is protected in type '{1}' but public in type '{2}'." }, Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses: { code: 2445, category: DiagnosticCategory.Error, key: "Property '{0}' is protected and only accessible within class '{1}' and its subclasses." }, Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1: { code: 2446, category: DiagnosticCategory.Error, key: "Property '{0}' is protected and only accessible through an instance of class '{1}'." }, + The_0_operator_is_not_allowed_for_boolean_types_1: { code: 2447, category: DiagnosticCategory.Error, key: "The '{0}' operator is not allowed for boolean types {1}." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_name_1_from_private_module_2: { code: 4001, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using name '{1}' from private module '{2}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, @@ -353,6 +354,7 @@ module ts { Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations: { code: 6004, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate TypeScript files instead of source locations." }, Watch_input_files: { code: 6005, category: DiagnosticCategory.Message, key: "Watch input files." }, Redirect_output_structure_to_the_directory: { code: 6006, category: DiagnosticCategory.Message, key: "Redirect output structure to the directory." }, + Consider_using_0_instead: { code: 6007, category: DiagnosticCategory.Message, key: "Consider using '{0}' instead." }, Do_not_emit_comments_to_output: { code: 6009, category: DiagnosticCategory.Message, key: "Do not emit comments to output." }, Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: 'ES3' (default), or 'ES5'" }, Specify_module_code_generation_Colon_commonjs_or_amd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs' or 'amd'" }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 0480cd3000c..7cce4be878b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1032,6 +1032,10 @@ "category": "Error", "code": 2446 }, + "The '{0}' operator is not allowed for boolean types {1}.": { + "category": "Error", + "code": 2447 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", @@ -1407,6 +1411,10 @@ "category": "Message", "code": 6006 }, + "Consider using '{0}' instead.": { + "category": "Message", + "code": 6007 + }, "Do not emit comments to output.": { "category": "Message", "code": 6009