Skip to content
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

Update: improve use-isnan rule to detect Number.NaN (fixes #14715) #14718

Merged
merged 5 commits into from Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/rules/use-isnan.md
Expand Up @@ -25,6 +25,14 @@ if (foo == NaN) {
if (foo != NaN) {
// ...
}

if (foo == Number.NaN) {
// ...
}

if (foo != Number.NaN) {
// ...
}
```

Examples of **correct** code for this rule:
Expand Down Expand Up @@ -77,6 +85,26 @@ switch (NaN) {
break;
// ...
}

switch (foo) {
case Number.NaN:
bar();
break;
case 1:
baz();
break;
// ...
}

switch (Number.NaN) {
case a:
bar();
break;
case b:
baz();
break;
// ...
}
```

Examples of **correct** code for this rule with `"enforceForSwitchCase"` option set to `true` (default):
Expand Down Expand Up @@ -126,6 +154,25 @@ switch (NaN) {
break;
// ...
}

switch (foo) {
case Number.NaN:
bar();
break;
case 1:
baz();
break;
// ...
}

switch (Number.NaN) {
case a:
bar();
break;
case b:
baz();
break;
/
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
```

### enforceForIndexOf
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/use-isnan.js
Expand Up @@ -21,7 +21,10 @@ const astUtils = require("./utils/ast-utils");
* @returns {boolean} `true` if the node is 'NaN' identifier.
*/
function isNaNIdentifier(node) {
return Boolean(node) && node.type === "Identifier" && node.name === "NaN";
return Boolean(node) && (node.type === "Identifier" && node.name === "NaN" ||
(node.type === "MemberExpression" &&
node.object.type === "Identifier" && node.object.name === "Number" &&
node.property.type === "Identifier" && node.property.name === "NaN"));
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
}

//------------------------------------------------------------------------------
Expand Down