From 6f06fd0d466f24129088463c2226733749c06816 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 28 Jun 2018 16:06:27 -0700 Subject: [PATCH] Use a more direct error. --- src/compiler/checker.ts | 33 +++++++++---------- src/compiler/diagnosticMessages.json | 2 +- .../unittests/tsserverProjectSystem.ts | 6 ++-- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index dfb10780fcb..1dc3c2cefba 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20904,31 +20904,30 @@ namespace ts { } function reportOperatorError() { - let err = chainDiagnosticMessages( - /*elaboration*/ undefined, - Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, - tokenToString(operatorToken.kind), - typeToString(leftType), - typeToString(rightType) - ); - err = giveBetterPrimaryError(err); - - diagnostics.add(createDiagnosticForNodeFromMessageChain( - errorNode || operatorToken, - err - )); + const leftStr = typeToString(leftType); + const rightStr = typeToString(rightType); + const errNode = errorNode || operatorToken; + if (!tryGiveBetterPrimaryError(errNode, leftStr, rightStr)) { + error( + errNode, + Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, + tokenToString(operatorToken.kind), + leftStr, + rightStr, + ); + } } - function giveBetterPrimaryError(elaboration: DiagnosticMessageChain) { + function tryGiveBetterPrimaryError(errNode: Node, leftStr: string, rightStr: string) { switch (operatorToken.kind) { case SyntaxKind.EqualsEqualsEqualsToken: case SyntaxKind.EqualsEqualsToken: - return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0, "false"); + return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "false", leftStr, rightStr); case SyntaxKind.ExclamationEqualsEqualsToken: case SyntaxKind.ExclamationEqualsToken: - return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0, "true"); + return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "true", leftStr, rightStr); } - return elaboration; + return undefined; } } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 5550cad86b7..d70db4d992b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1252,7 +1252,7 @@ "category": "Error", "code": 2366 }, - "The types of these values indicate that this condition will always be '{0}'.": { + "This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap.": { "category": "Error", "code": 2367 }, diff --git a/src/testRunner/unittests/tsserverProjectSystem.ts b/src/testRunner/unittests/tsserverProjectSystem.ts index 860c7f8ef55..a917fd08d6d 100644 --- a/src/testRunner/unittests/tsserverProjectSystem.ts +++ b/src/testRunner/unittests/tsserverProjectSystem.ts @@ -5016,7 +5016,7 @@ namespace ts.projectSystem { ); const errorResult = session.executeCommand(getErrRequest).response; assert.isTrue(errorResult.length === 1); - assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code); + assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code); }); it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => { @@ -5043,7 +5043,7 @@ namespace ts.projectSystem { ); const errorResult = session.executeCommand(getErrRequest).response; assert.isTrue(errorResult.length === 1); - assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code); + assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code); }); it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => { @@ -5072,7 +5072,7 @@ namespace ts.projectSystem { ); const errorResult = session.executeCommand(getErrRequest).response; assert.isTrue(errorResult.length === 1); - assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code); + assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code); }); });