-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat(49962): Disallow comparison against NaN #50626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec857a9
feat(49962): disallow comparison against NaN
a-tarasyuk fb44a1d
change diagnostic message
a-tarasyuk 5ca49bb
use global NaN symbol for NaN equality comparisons
a-tarasyuk 89c00e6
Merge branch 'main' of https://github.com/microsoft/TypeScript into f…
a-tarasyuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "fixNaNEquality"; | ||
const errorCodes = [ | ||
Diagnostics.This_condition_will_always_return_0.code, | ||
]; | ||
|
||
registerCodeFix({ | ||
errorCodes, | ||
getCodeActions(context) { | ||
const { sourceFile, span, program } = context; | ||
const info = getInfo(program, sourceFile, span); | ||
if (info === undefined) return; | ||
|
||
const { suggestion, expression, arg } = info; | ||
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, arg, expression)); | ||
return [createCodeFixAction(fixId, changes, [Diagnostics.Use_0, suggestion], fixId, Diagnostics.Use_Number_isNaN_in_all_conditions)]; | ||
}, | ||
fixIds: [fixId], | ||
getAllCodeActions: context => { | ||
return codeFixAll(context, errorCodes, (changes, diag) => { | ||
const info = getInfo(context.program, diag.file, createTextSpan(diag.start, diag.length)); | ||
if (info) { | ||
doChange(changes, diag.file, info.arg, info.expression); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
interface Info { | ||
suggestion: string; | ||
expression: BinaryExpression; | ||
arg: Expression; | ||
} | ||
|
||
function getInfo(program: Program, sourceFile: SourceFile, span: TextSpan): Info | undefined { | ||
const diag = find(program.getSemanticDiagnostics(sourceFile), diag => diag.start === span.start && diag.length === span.length); | ||
if (diag === undefined || diag.relatedInformation === undefined) return; | ||
|
||
const related = find(diag.relatedInformation, related => related.code === Diagnostics.Did_you_mean_0.code); | ||
if (related === undefined || related.file === undefined || related.start === undefined || related.length === undefined) return; | ||
|
||
const token = findAncestorMatchingSpan(related.file, createTextSpan(related.start, related.length)); | ||
if (token === undefined) return; | ||
|
||
if (isExpression(token) && isBinaryExpression(token.parent)) { | ||
return { suggestion: getSuggestion(related.messageText), expression: token.parent, arg: token }; | ||
} | ||
return undefined; | ||
} | ||
|
||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, arg: Expression, expression: BinaryExpression) { | ||
const callExpression = factory.createCallExpression( | ||
factory.createPropertyAccessExpression(factory.createIdentifier("Number"), factory.createIdentifier("isNaN")), /*typeArguments*/ undefined, [arg]); | ||
const operator = expression.operatorToken.kind ; | ||
changes.replaceNode(sourceFile, expression, | ||
operator === SyntaxKind.ExclamationEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken | ||
? factory.createPrefixUnaryExpression(SyntaxKind.ExclamationToken, callExpression) : callExpression); | ||
} | ||
|
||
function getSuggestion(messageText: string | DiagnosticMessageChain) { | ||
const [_, suggestion] = flattenDiagnosticMessageText(messageText, "\n", 0).match(/\'(.*)\'/) || []; | ||
return suggestion; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
tests/cases/compiler/nanEquality.ts(3,5): error TS2845: This condition will always return 'false'. | ||
tests/cases/compiler/nanEquality.ts(4,5): error TS2845: This condition will always return 'false'. | ||
tests/cases/compiler/nanEquality.ts(6,5): error TS2845: This condition will always return 'false'. | ||
tests/cases/compiler/nanEquality.ts(7,5): error TS2845: This condition will always return 'false'. | ||
tests/cases/compiler/nanEquality.ts(9,5): error TS2845: This condition will always return 'true'. | ||
tests/cases/compiler/nanEquality.ts(10,5): error TS2845: This condition will always return 'true'. | ||
tests/cases/compiler/nanEquality.ts(12,5): error TS2845: This condition will always return 'true'. | ||
tests/cases/compiler/nanEquality.ts(13,5): error TS2845: This condition will always return 'true'. | ||
tests/cases/compiler/nanEquality.ts(15,5): error TS2845: This condition will always return 'false'. | ||
tests/cases/compiler/nanEquality.ts(16,5): error TS2845: This condition will always return 'false'. | ||
tests/cases/compiler/nanEquality.ts(18,5): error TS2845: This condition will always return 'true'. | ||
tests/cases/compiler/nanEquality.ts(19,5): error TS2845: This condition will always return 'true'. | ||
tests/cases/compiler/nanEquality.ts(21,5): error TS2845: This condition will always return 'false'. | ||
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 (17 errors) ==== | ||
declare const x: number; | ||
|
||
if (x === NaN) {} | ||
~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:3:5: Did you mean 'Number.isNaN(x)'? | ||
if (NaN === x) {} | ||
~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:4:13: Did you mean 'Number.isNaN(x)'? | ||
|
||
if (x == NaN) {} | ||
~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:6:5: Did you mean 'Number.isNaN(x)'? | ||
if (NaN == x) {} | ||
~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:7:12: Did you mean 'Number.isNaN(x)'? | ||
|
||
if (x !== NaN) {} | ||
~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:9:5: Did you mean '!Number.isNaN(x)'? | ||
if (NaN !== x) {} | ||
~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:10:13: Did you mean '!Number.isNaN(x)'? | ||
|
||
if (x != NaN) {} | ||
~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:12:5: Did you mean '!Number.isNaN(x)'? | ||
if (NaN != x) {} | ||
~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:13:12: Did you mean '!Number.isNaN(x)'? | ||
|
||
if (x === ((NaN))) {} | ||
~~~~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:15:5: Did you mean 'Number.isNaN(x)'? | ||
if (((NaN)) === x) {} | ||
~~~~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:16:17: Did you mean 'Number.isNaN(x)'? | ||
|
||
if (x !== ((NaN))) {} | ||
~~~~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:18:5: Did you mean '!Number.isNaN(x)'? | ||
if (((NaN)) !== x) {} | ||
~~~~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
!!! related TS1369 tests/cases/compiler/nanEquality.ts:19:17: Did you mean '!Number.isNaN(x)'? | ||
|
||
if (NaN === NaN) {} | ||
~~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
if (NaN !== NaN) {} | ||
~~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'true'. | ||
|
||
if (NaN == NaN) {} | ||
~~~~~~~~~~ | ||
!!! error TS2845: This condition will always return 'false'. | ||
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(...)'? | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//// [nanEquality.ts] | ||
declare const x: number; | ||
|
||
if (x === NaN) {} | ||
if (NaN === x) {} | ||
|
||
if (x == NaN) {} | ||
if (NaN == x) {} | ||
|
||
if (x !== NaN) {} | ||
if (NaN !== x) {} | ||
|
||
if (x != NaN) {} | ||
if (NaN != x) {} | ||
|
||
if (x === ((NaN))) {} | ||
if (((NaN)) === x) {} | ||
|
||
if (x !== ((NaN))) {} | ||
if (((NaN)) !== x) {} | ||
|
||
if (NaN === NaN) {} | ||
if (NaN !== NaN) {} | ||
|
||
if (NaN == NaN) {} | ||
if (NaN != NaN) {} | ||
|
||
// ... | ||
declare let y: any; | ||
if (NaN === y[0][1]) {} | ||
|
||
|
||
//// [nanEquality.js] | ||
if (x === NaN) { } | ||
if (NaN === x) { } | ||
if (x == NaN) { } | ||
if (NaN == x) { } | ||
if (x !== NaN) { } | ||
if (NaN !== x) { } | ||
if (x != NaN) { } | ||
if (NaN != x) { } | ||
if (x === ((NaN))) { } | ||
if (((NaN)) === x) { } | ||
if (x !== ((NaN))) { } | ||
if (((NaN)) !== x) { } | ||
if (NaN === NaN) { } | ||
if (NaN !== NaN) { } | ||
if (NaN == NaN) { } | ||
if (NaN != NaN) { } | ||
if (NaN === y[0][1]) { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
=== tests/cases/compiler/nanEquality.ts === | ||
declare const x: number; | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (x === NaN) {} | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (NaN === x) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (x == NaN) {} | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (NaN == x) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (x !== NaN) {} | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (NaN !== x) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (x != NaN) {} | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (NaN != x) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (x === ((NaN))) {} | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (((NaN)) === x) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (x !== ((NaN))) {} | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (((NaN)) !== x) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>x : Symbol(x, Decl(nanEquality.ts, 0, 13)) | ||
|
||
if (NaN === NaN) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (NaN !== NaN) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
if (NaN == NaN) {} | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
>NaN : Symbol(NaN, Decl(lib.es5.d.ts, --, --)) | ||
|
||
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)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.