Skip to content

Commit 104c0b5

Browse files
Update: improve use-isnan rule to detect Number.NaN (fixes #14715) (#14718)
* Update: improve `isNaNIdentifier` to detect `Number.isNaN` (fixes #14715) * Chore: add test cases for `Number.NaN` * Docs: add more examples for `use-isnan` * Chore: improve logic and add more test cases * Docs: Update docs/rules/use-isnan.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
1 parent b08170b commit 104c0b5

File tree

3 files changed

+396
-1
lines changed

3 files changed

+396
-1
lines changed

docs/rules/use-isnan.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ if (foo == NaN) {
2525
if (foo != NaN) {
2626
// ...
2727
}
28+
29+
if (foo == Number.NaN) {
30+
// ...
31+
}
32+
33+
if (foo != Number.NaN) {
34+
// ...
35+
}
2836
```
2937

3038
Examples of **correct** code for this rule:
@@ -77,6 +85,26 @@ switch (NaN) {
7785
break;
7886
// ...
7987
}
88+
89+
switch (foo) {
90+
case Number.NaN:
91+
bar();
92+
break;
93+
case 1:
94+
baz();
95+
break;
96+
// ...
97+
}
98+
99+
switch (Number.NaN) {
100+
case a:
101+
bar();
102+
break;
103+
case b:
104+
baz();
105+
break;
106+
// ...
107+
}
80108
```
81109

82110
Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `true` (default):
@@ -126,6 +154,26 @@ switch (NaN) {
126154
break;
127155
// ...
128156
}
157+
158+
switch (foo) {
159+
case Number.NaN:
160+
bar();
161+
break;
162+
case 1:
163+
baz();
164+
break;
165+
// ...
166+
}
167+
168+
switch (Number.NaN) {
169+
case a:
170+
bar();
171+
break;
172+
case b:
173+
baz();
174+
break;
175+
// ...
176+
}
129177
```
130178

131179
### enforceForIndexOf

lib/rules/use-isnan.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const astUtils = require("./utils/ast-utils");
2121
* @returns {boolean} `true` if the node is 'NaN' identifier.
2222
*/
2323
function isNaNIdentifier(node) {
24-
return Boolean(node) && node.type === "Identifier" && node.name === "NaN";
24+
return Boolean(node) && (
25+
astUtils.isSpecificId(node, "NaN") ||
26+
astUtils.isSpecificMemberAccess(node, "Number", "NaN")
27+
);
2528
}
2629

2730
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy