Skip to content

Commit

Permalink
Merge pull request #25251 from Microsoft/betterAlwaysTrueFalseErrors
Browse files Browse the repository at this point in the history
Better "always true/false" errors
  • Loading branch information
DanielRosenwasser authored Jun 28, 2018
2 parents 16f045b + 704cd7b commit 950593b
Show file tree
Hide file tree
Showing 40 changed files with 1,500 additions and 739 deletions.
26 changes: 25 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20884,7 +20884,31 @@ namespace ts {
}

function reportOperatorError() {
error(errorNode || operatorToken, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType));
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
));
}

function giveBetterPrimaryError(elaboration: DiagnosticMessageChain) {
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");
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 elaboration;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,10 @@
"category": "Error",
"code": 2366
},
"The types of these values indicate that this condition will always be '{0}'.": {
"category": "Error",
"code": 2367
},
"Type parameter name cannot be '{0}'.": {
"category": "Error",
"code": 2368
Expand Down
12 changes: 6 additions & 6 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4997,7 +4997,7 @@ namespace ts.projectSystem {
assert.isTrue(error2Result.length === 0);
});

it("should report semanitc errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
it("should report semantic errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
const jsFile = {
path: "/a/jsFile.js",
content: `
Expand All @@ -5016,10 +5016,10 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
});

it("should report semanitc errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
const jsconfigFile = {
path: "/a/jsconfig.json",
content: "{}"
Expand All @@ -5043,10 +5043,10 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
});

it("should report semanitc errors for configured js project with checkJs=true and skipLibCheck=true", () => {
it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
const jsconfigFile = {
path: "/a/jsconfig.json",
content: JSON.stringify({
Expand All @@ -5072,7 +5072,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code);
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
});
});

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5151,6 +5151,7 @@ declare namespace ts {
The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access: DiagnosticMessage;
Operator_0_cannot_be_applied_to_types_1_and_2: DiagnosticMessage;
Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined: DiagnosticMessage;
The_types_of_these_values_indicate_that_this_condition_will_always_be_0: DiagnosticMessage;
Type_parameter_name_cannot_be_0: DiagnosticMessage;
A_parameter_property_is_only_allowed_in_a_constructor_implementation: DiagnosticMessage;
A_rest_parameter_must_be_of_an_array_type: DiagnosticMessage;
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/capturedLetConstInLoop5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.


==== tests/cases/compiler/capturedLetConstInLoop5.ts (2 errors) ====
Expand Down Expand Up @@ -178,7 +180,8 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator
(() => x + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down Expand Up @@ -235,7 +238,8 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator
(() => x + y + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/capturedLetConstInLoop5_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.


==== tests/cases/compiler/capturedLetConstInLoop5_ES6.ts (2 errors) ====
Expand Down Expand Up @@ -178,7 +180,8 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Opera
(() => x + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down Expand Up @@ -235,7 +238,8 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Opera
(() => x + y + v);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
return;
}
}
Expand Down
24 changes: 16 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.


==== tests/cases/compiler/capturedLetConstInLoop6.ts (4 errors) ====
Expand Down Expand Up @@ -153,12 +157,14 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '
(() => x);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down Expand Up @@ -204,12 +210,14 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '
(() => x + y);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down
24 changes: 16 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop6_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '1'.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
Operator '==' cannot be applied to types '0' and '2'.


==== tests/cases/compiler/capturedLetConstInLoop6_ES6.ts (4 errors) ====
Expand Down Expand Up @@ -153,12 +157,14 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operat
(() => x);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down Expand Up @@ -204,12 +210,14 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operat
(() => x + y);
if (x == 1) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
continue;
}
}
Expand Down
Loading

0 comments on commit 950593b

Please sign in to comment.