Skip to content

Commit

Permalink
change diagnostic message
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Sep 14, 2022
1 parent ec857a9 commit fb44a1d
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/compiler/checker.ts
Expand Up @@ -34621,15 +34621,17 @@ namespace ts {
}

function checkNaNEquality(errorNode: Node | undefined, operator: SyntaxKind, left: Expression, right: Expression, leftType: Type, rightType: Type) {
const isLeftNaN = leftType.flags & TypeFlags.Number && isNaN(skipParentheses(left));
const isRightNaN = rightType.flags & TypeFlags.Number && isNaN(skipParentheses(right));
const isLeftNaN = (leftType.flags & TypeFlags.Number) && isNaN(skipParentheses(left));
const isRightNaN = (rightType.flags & TypeFlags.Number) && isNaN(skipParentheses(right));
if (isLeftNaN || isRightNaN) {
const err = error(errorNode, Diagnostics.This_condition_will_always_return_0,
tokenToString(operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.EqualsEqualsToken ? SyntaxKind.FalseKeyword : SyntaxKind.TrueKeyword));
if (isLeftNaN && isRightNaN) return;
const operatorString = operator === SyntaxKind.ExclamationEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken ? tokenToString(SyntaxKind.ExclamationToken) : "";
const location = isLeftNaN ? right : left;
const expression = skipParentheses(location);
addRelatedInfo(err, createDiagnosticForNode(location, Diagnostics.Did_you_mean_0,
`${operator === SyntaxKind.ExclamationEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken ? tokenToString(SyntaxKind.ExclamationToken) : ""}Number.isNaN(${getTextOfNode(location)})`));
`${operatorString}Number.isNaN(${isEntityNameExpression(expression) ? entityNameToString(expression) : "..."})`));
}
}

Expand Down
10 changes: 9 additions & 1 deletion tests/baselines/reference/nanEquality.errors.txt
Expand Up @@ -14,9 +14,10 @@ tests/cases/compiler/nanEquality.ts(21,5): error TS2845: This condition will alw
tests/cases/compiler/nanEquality.ts(22,5): error TS2845: This condition will always return 'true'.
tests/cases/compiler/nanEquality.ts(24,5): error TS2845: This condition will always return 'false'.
tests/cases/compiler/nanEquality.ts(25,5): error TS2845: This condition will always return 'true'.
tests/cases/compiler/nanEquality.ts(29,5): error TS2845: This condition will always return 'false'.


==== tests/cases/compiler/nanEquality.ts (16 errors) ====
==== tests/cases/compiler/nanEquality.ts (17 errors) ====
declare const x: number;

if (x === NaN) {}
Expand Down Expand Up @@ -86,4 +87,11 @@ tests/cases/compiler/nanEquality.ts(25,5): error TS2845: This condition will alw
if (NaN != NaN) {}
~~~~~~~~~~
!!! error TS2845: This condition will always return 'true'.

// ...
declare let y: any;
if (NaN === y[0][1]) {}
~~~~~~~~~~~~~~~
!!! error TS2845: This condition will always return 'false'.
!!! related TS1369 tests/cases/compiler/nanEquality.ts:29:13: Did you mean 'Number.isNaN(...)'?

5 changes: 5 additions & 0 deletions tests/baselines/reference/nanEquality.js
Expand Up @@ -24,6 +24,10 @@ if (NaN !== NaN) {}

if (NaN == NaN) {}
if (NaN != NaN) {}

// ...
declare let y: any;
if (NaN === y[0][1]) {}


//// [nanEquality.js]
Expand All @@ -43,3 +47,4 @@ if (NaN === NaN) { }
if (NaN !== NaN) { }
if (NaN == NaN) { }
if (NaN != NaN) { }
if (NaN === y[0][1]) { }
8 changes: 8 additions & 0 deletions tests/baselines/reference/nanEquality.symbols
Expand Up @@ -66,3 +66,11 @@ if (NaN != NaN) {}
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))

// ...
declare let y: any;
>y : Symbol(y, Decl(nanEquality.ts, 27, 11))

if (NaN === y[0][1]) {}
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(nanEquality.ts, 27, 11))

13 changes: 13 additions & 0 deletions tests/baselines/reference/nanEquality.types
Expand Up @@ -90,3 +90,16 @@ if (NaN != NaN) {}
>NaN : number
>NaN : number

// ...
declare let y: any;
>y : any

if (NaN === y[0][1]) {}
>NaN === y[0][1] : boolean
>NaN : number
>y[0][1] : any
>y[0] : any
>y : any
>0 : 0
>1 : 1

4 changes: 4 additions & 0 deletions tests/cases/compiler/nanEquality.ts
Expand Up @@ -23,3 +23,7 @@ if (NaN !== NaN) {}

if (NaN == NaN) {}
if (NaN != NaN) {}

// ...
declare let y: any;
if (NaN === y[0][1]) {}
5 changes: 3 additions & 2 deletions tests/cases/fourslash/fixNaNEquality1.ts
@@ -1,9 +1,10 @@
/// <reference path="fourslash.ts" />

////if (x === NaN) {}
////declare const x: number;
////[|if (x === NaN) {}|]

verify.codeFix({
index: 0,
description: "Use `Number.isNaN(x)`.",
newFileContent: "if (Number.isNaN(x)) {}",
newRangeContent: "if (Number.isNaN(x)) {}",
});
5 changes: 3 additions & 2 deletions tests/cases/fourslash/fixNaNEquality2.ts
@@ -1,9 +1,10 @@
/// <reference path="fourslash.ts" />

////if (NaN === x) {}
////declare const x: number;
////[|if (NaN === x) {}|]

verify.codeFix({
index: 0,
description: "Use `Number.isNaN(x)`.",
newFileContent: "if (Number.isNaN(x)) {}",
newRangeContent: "if (Number.isNaN(x)) {}",
});
5 changes: 3 additions & 2 deletions tests/cases/fourslash/fixNaNEquality3.ts
@@ -1,9 +1,10 @@
/// <reference path="fourslash.ts" />

////if (x !== NaN) {}
////declare const x: number;
////[|if (x !== NaN) {}|]

verify.codeFix({
index: 0,
description: "Use `!Number.isNaN(x)`.",
newFileContent: "if (!Number.isNaN(x)) {}",
newRangeContent: "if (!Number.isNaN(x)) {}",
});
5 changes: 3 additions & 2 deletions tests/cases/fourslash/fixNaNEquality4.ts
@@ -1,9 +1,10 @@
/// <reference path="fourslash.ts" />

////if (NaN !== x) {}
////declare const x: number;
////[|if (NaN !== x) {}|]

verify.codeFix({
index: 0,
description: "Use `!Number.isNaN(x)`.",
newFileContent: "if (!Number.isNaN(x)) {}",
newRangeContent: "if (!Number.isNaN(x)) {}",
});
10 changes: 10 additions & 0 deletions tests/cases/fourslash/fixNaNEquality5.ts
@@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />

////declare const x: any;
////[|if (NaN !== x[0][1]) {}|]

verify.codeFix({
index: 0,
description: "Use `!Number.isNaN(...)`.",
newRangeContent: "if (!Number.isNaN(x[0][1])) {}",
});
10 changes: 8 additions & 2 deletions tests/cases/fourslash/fixNaNEquality_all.ts
@@ -1,16 +1,22 @@
/// <reference path="fourslash.ts" />

////declare const x: number;
////declare const y: any;
////if (x === NaN) {}
////if (NaN === x) {}
////if (x !== NaN) {}
////if (NaN !== x) {}
////if (NaN === y[0][1]) {}

verify.codeFixAll({
fixId: "fixNaNEquality",
fixAllDescription: ts.Diagnostics.Use_Number_isNaN_in_all_conditions.message,
newFileContent:
`if (Number.isNaN(x)) {}
`declare const x: number;
declare const y: any;
if (Number.isNaN(x)) {}
if (Number.isNaN(x)) {}
if (!Number.isNaN(x)) {}
if (!Number.isNaN(x)) {}
if (!Number.isNaN(x)) {}`
if (Number.isNaN(y[0][1])) {}`
});

0 comments on commit fb44a1d

Please sign in to comment.