From 4714c2654372229931204e85f948150f87501bf8 Mon Sep 17 00:00:00 2001 From: Chris Bubernak Date: Fri, 3 Oct 2014 08:09:57 -0700 Subject: [PATCH] rewrote changes to the checker --- src/compiler/checker.ts | 44 +++++++------------ .../diagnosticInformationMap.generated.ts | 3 +- src/compiler/diagnosticMessages.json | 6 +-- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index bcd0adfb40d..48433984c4d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5022,9 +5022,11 @@ module ts { if (leftType.flags & (TypeFlags.Undefined | TypeFlags.Null)) leftType = rightType; if (rightType.flags & (TypeFlags.Undefined | TypeFlags.Null)) rightType = leftType; - 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)); + if (leftType.flags & TypeFlags.Boolean && rightType.flags & TypeFlags.Boolean) { + var suggestedOperator = suggestedBooleanOperator(node.operator); + if (suggestedOperator !== undefined) { + error(node, Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, tokenToString(node.operator), tokenToString(suggestedOperator)); + } } 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); @@ -5094,36 +5096,22 @@ module ts { case SyntaxKind.CommaToken: return rightType; } - - function preferredBooleanOperator(operator: any): string { - var message = ""; - var suggestion:SyntaxKind; + + // if a user tries to apply an innappropriate operator to 2 boolean operands try and return them a helpful suggestion + function suggestedBooleanOperator(operator: any): 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; + return SyntaxKind.BarBarToken; + case SyntaxKind.CaretToken: + case SyntaxKind.CaretEqualsToken: + return SyntaxKind.ExclamationEqualsEqualsToken; + case SyntaxKind.AmpersandToken: case SyntaxKind.AmpersandEqualsToken: - suggestion = SyntaxKind.AmpersandEqualsToken; - break; + return SyntaxKind.AmpersandAmpersandToken; + default: + return undefined; } - 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 { diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index e4889cd5741..a430799ba50 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -260,7 +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}." }, + The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead: { code: 2447, category: DiagnosticCategory.Error, key: "The '{0}' operator is not allowed for boolean types. Consider using {1} instead." }, 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}'." }, @@ -354,7 +354,6 @@ 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 7cce4be878b..d715c898510 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1032,7 +1032,7 @@ "category": "Error", "code": 2446 }, - "The '{0}' operator is not allowed for boolean types {1}.": { + "The '{0}' operator is not allowed for boolean types. Consider using {1} instead.": { "category": "Error", "code": 2447 }, @@ -1411,10 +1411,6 @@ "category": "Message", "code": 6006 }, - "Consider using '{0}' instead.": { - "category": "Message", - "code": 6007 - }, "Do not emit comments to output.": { "category": "Message", "code": 6009