Skip to content

Commit fb44a1d

Browse files
committed
change diagnostic message
1 parent ec857a9 commit fb44a1d

12 files changed

+74
-14
lines changed

Diff for: src/compiler/checker.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -34621,15 +34621,17 @@ namespace ts {
3462134621
}
3462234622

3462334623
function checkNaNEquality(errorNode: Node | undefined, operator: SyntaxKind, left: Expression, right: Expression, leftType: Type, rightType: Type) {
34624-
const isLeftNaN = leftType.flags & TypeFlags.Number && isNaN(skipParentheses(left));
34625-
const isRightNaN = rightType.flags & TypeFlags.Number && isNaN(skipParentheses(right));
34624+
const isLeftNaN = (leftType.flags & TypeFlags.Number) && isNaN(skipParentheses(left));
34625+
const isRightNaN = (rightType.flags & TypeFlags.Number) && isNaN(skipParentheses(right));
3462634626
if (isLeftNaN || isRightNaN) {
3462734627
const err = error(errorNode, Diagnostics.This_condition_will_always_return_0,
3462834628
tokenToString(operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.EqualsEqualsToken ? SyntaxKind.FalseKeyword : SyntaxKind.TrueKeyword));
3462934629
if (isLeftNaN && isRightNaN) return;
34630+
const operatorString = operator === SyntaxKind.ExclamationEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken ? tokenToString(SyntaxKind.ExclamationToken) : "";
3463034631
const location = isLeftNaN ? right : left;
34632+
const expression = skipParentheses(location);
3463134633
addRelatedInfo(err, createDiagnosticForNode(location, Diagnostics.Did_you_mean_0,
34632-
`${operator === SyntaxKind.ExclamationEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken ? tokenToString(SyntaxKind.ExclamationToken) : ""}Number.isNaN(${getTextOfNode(location)})`));
34634+
`${operatorString}Number.isNaN(${isEntityNameExpression(expression) ? entityNameToString(expression) : "..."})`));
3463334635
}
3463434636
}
3463534637

Diff for: tests/baselines/reference/nanEquality.errors.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ tests/cases/compiler/nanEquality.ts(21,5): error TS2845: This condition will alw
1414
tests/cases/compiler/nanEquality.ts(22,5): error TS2845: This condition will always return 'true'.
1515
tests/cases/compiler/nanEquality.ts(24,5): error TS2845: This condition will always return 'false'.
1616
tests/cases/compiler/nanEquality.ts(25,5): error TS2845: This condition will always return 'true'.
17+
tests/cases/compiler/nanEquality.ts(29,5): error TS2845: This condition will always return 'false'.
1718

1819

19-
==== tests/cases/compiler/nanEquality.ts (16 errors) ====
20+
==== tests/cases/compiler/nanEquality.ts (17 errors) ====
2021
declare const x: number;
2122

2223
if (x === NaN) {}
@@ -86,4 +87,11 @@ tests/cases/compiler/nanEquality.ts(25,5): error TS2845: This condition will alw
8687
if (NaN != NaN) {}
8788
~~~~~~~~~~
8889
!!! error TS2845: This condition will always return 'true'.
90+
91+
// ...
92+
declare let y: any;
93+
if (NaN === y[0][1]) {}
94+
~~~~~~~~~~~~~~~
95+
!!! error TS2845: This condition will always return 'false'.
96+
!!! related TS1369 tests/cases/compiler/nanEquality.ts:29:13: Did you mean 'Number.isNaN(...)'?
8997

Diff for: tests/baselines/reference/nanEquality.js

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ if (NaN !== NaN) {}
2424

2525
if (NaN == NaN) {}
2626
if (NaN != NaN) {}
27+
28+
// ...
29+
declare let y: any;
30+
if (NaN === y[0][1]) {}
2731

2832

2933
//// [nanEquality.js]
@@ -43,3 +47,4 @@ if (NaN === NaN) { }
4347
if (NaN !== NaN) { }
4448
if (NaN == NaN) { }
4549
if (NaN != NaN) { }
50+
if (NaN === y[0][1]) { }

Diff for: tests/baselines/reference/nanEquality.symbols

+8
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ if (NaN != NaN) {}
6666
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))
6767
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))
6868

69+
// ...
70+
declare let y: any;
71+
>y : Symbol(y, Decl(nanEquality.ts, 27, 11))
72+
73+
if (NaN === y[0][1]) {}
74+
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --))
75+
>y : Symbol(y, Decl(nanEquality.ts, 27, 11))
76+

Diff for: tests/baselines/reference/nanEquality.types

+13
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,16 @@ if (NaN != NaN) {}
9090
>NaN : number
9191
>NaN : number
9292

93+
// ...
94+
declare let y: any;
95+
>y : any
96+
97+
if (NaN === y[0][1]) {}
98+
>NaN === y[0][1] : boolean
99+
>NaN : number
100+
>y[0][1] : any
101+
>y[0] : any
102+
>y : any
103+
>0 : 0
104+
>1 : 1
105+

Diff for: tests/cases/compiler/nanEquality.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ if (NaN !== NaN) {}
2323

2424
if (NaN == NaN) {}
2525
if (NaN != NaN) {}
26+
27+
// ...
28+
declare let y: any;
29+
if (NaN === y[0][1]) {}

Diff for: tests/cases/fourslash/fixNaNEquality1.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/// <reference path="fourslash.ts" />
22

3-
////if (x === NaN) {}
3+
////declare const x: number;
4+
////[|if (x === NaN) {}|]
45

56
verify.codeFix({
67
index: 0,
78
description: "Use `Number.isNaN(x)`.",
8-
newFileContent: "if (Number.isNaN(x)) {}",
9+
newRangeContent: "if (Number.isNaN(x)) {}",
910
});

Diff for: tests/cases/fourslash/fixNaNEquality2.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/// <reference path="fourslash.ts" />
22

3-
////if (NaN === x) {}
3+
////declare const x: number;
4+
////[|if (NaN === x) {}|]
45

56
verify.codeFix({
67
index: 0,
78
description: "Use `Number.isNaN(x)`.",
8-
newFileContent: "if (Number.isNaN(x)) {}",
9+
newRangeContent: "if (Number.isNaN(x)) {}",
910
});

Diff for: tests/cases/fourslash/fixNaNEquality3.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/// <reference path="fourslash.ts" />
22

3-
////if (x !== NaN) {}
3+
////declare const x: number;
4+
////[|if (x !== NaN) {}|]
45

56
verify.codeFix({
67
index: 0,
78
description: "Use `!Number.isNaN(x)`.",
8-
newFileContent: "if (!Number.isNaN(x)) {}",
9+
newRangeContent: "if (!Number.isNaN(x)) {}",
910
});

Diff for: tests/cases/fourslash/fixNaNEquality4.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/// <reference path="fourslash.ts" />
22

3-
////if (NaN !== x) {}
3+
////declare const x: number;
4+
////[|if (NaN !== x) {}|]
45

56
verify.codeFix({
67
index: 0,
78
description: "Use `!Number.isNaN(x)`.",
8-
newFileContent: "if (!Number.isNaN(x)) {}",
9+
newRangeContent: "if (!Number.isNaN(x)) {}",
910
});

Diff for: tests/cases/fourslash/fixNaNEquality5.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////declare const x: any;
4+
////[|if (NaN !== x[0][1]) {}|]
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: "Use `!Number.isNaN(...)`.",
9+
newRangeContent: "if (!Number.isNaN(x[0][1])) {}",
10+
});

Diff for: tests/cases/fourslash/fixNaNEquality_all.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
/// <reference path="fourslash.ts" />
22

3+
////declare const x: number;
4+
////declare const y: any;
35
////if (x === NaN) {}
46
////if (NaN === x) {}
57
////if (x !== NaN) {}
68
////if (NaN !== x) {}
9+
////if (NaN === y[0][1]) {}
710

811
verify.codeFixAll({
912
fixId: "fixNaNEquality",
1013
fixAllDescription: ts.Diagnostics.Use_Number_isNaN_in_all_conditions.message,
1114
newFileContent:
12-
`if (Number.isNaN(x)) {}
15+
`declare const x: number;
16+
declare const y: any;
1317
if (Number.isNaN(x)) {}
18+
if (Number.isNaN(x)) {}
19+
if (!Number.isNaN(x)) {}
1420
if (!Number.isNaN(x)) {}
15-
if (!Number.isNaN(x)) {}`
21+
if (Number.isNaN(y[0][1])) {}`
1622
});

0 commit comments

Comments
 (0)