Skip to content

Commit a9e5961

Browse files
authored
fix: use-isnan false positive on shadowed NaN/Number (#20958)
1 parent 557fde8 commit a9e5961

2 files changed

Lines changed: 87 additions & 12 deletions

File tree

‎lib/rules/use-isnan.js‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@ const astUtils = require("./utils/ast-utils");
1818
/**
1919
* Determines if the given node is a NaN `Identifier` node.
2020
* @param {ASTNode|null} node The node to check.
21-
* @returns {boolean} `true` if the node is 'NaN' identifier.
21+
* @param {SourceCode} sourceCode The source code object.
22+
* @returns {boolean} `true` if the node is a global 'NaN' identifier.
2223
*/
23-
function isNaNIdentifier(node) {
24+
function isNaNIdentifier(node, sourceCode) {
2425
if (!node) {
2526
return false;
2627
}
2728

2829
const nodeToCheck =
2930
node.type === "SequenceExpression" ? node.expressions.at(-1) : node;
3031

31-
return (
32-
astUtils.isSpecificId(nodeToCheck, "NaN") ||
33-
astUtils.isSpecificMemberAccess(nodeToCheck, "Number", "NaN")
34-
);
32+
if (astUtils.isSpecificId(nodeToCheck, "NaN")) {
33+
return sourceCode.isGlobalReference(nodeToCheck);
34+
}
35+
36+
if (astUtils.isSpecificMemberAccess(nodeToCheck, "Number", "NaN")) {
37+
const objectNode = astUtils.skipChainExpression(nodeToCheck).object;
38+
return sourceCode.isGlobalReference(objectNode);
39+
}
40+
41+
return false;
3542
}
3643

3744
//------------------------------------------------------------------------------
@@ -103,7 +110,7 @@ module.exports = {
103110
*/
104111
function getBinaryExpressionFixer(node, wrapValue) {
105112
return fixer => {
106-
const comparedValue = isNaNIdentifier(node.left)
113+
const comparedValue = isNaNIdentifier(node.left, sourceCode)
107114
? node.right
108115
: node.left;
109116
const shouldWrap = comparedValue.type === "SequenceExpression";
@@ -130,10 +137,11 @@ module.exports = {
130137
function checkBinaryExpression(node) {
131138
if (
132139
/^(?:[<>]|[!=]=)=?$/u.test(node.operator) &&
133-
(isNaNIdentifier(node.left) || isNaNIdentifier(node.right))
140+
(isNaNIdentifier(node.left, sourceCode) ||
141+
isNaNIdentifier(node.right, sourceCode))
134142
) {
135143
const suggestedFixes = [];
136-
const NaNNode = isNaNIdentifier(node.left)
144+
const NaNNode = isNaNIdentifier(node.left, sourceCode)
137145
? node.left
138146
: node.right;
139147

@@ -178,12 +186,12 @@ module.exports = {
178186
* @returns {void}
179187
*/
180188
function checkSwitchStatement(node) {
181-
if (isNaNIdentifier(node.discriminant)) {
189+
if (isNaNIdentifier(node.discriminant, sourceCode)) {
182190
context.report({ node, messageId: "switchNaN" });
183191
}
184192

185193
for (const switchCase of node.cases) {
186-
if (isNaNIdentifier(switchCase.test)) {
194+
if (isNaNIdentifier(switchCase.test, sourceCode)) {
187195
context.report({ node: switchCase, messageId: "caseNaN" });
188196
}
189197
}
@@ -204,7 +212,7 @@ module.exports = {
204212
(methodName === "indexOf" ||
205213
methodName === "lastIndexOf") &&
206214
node.arguments.length <= 2 &&
207-
isNaNIdentifier(node.arguments[0])
215+
isNaNIdentifier(node.arguments[0], sourceCode)
208216
) {
209217
/*
210218
* To retain side effects, it's essential to address `NaN` beforehand, which

‎tests/lib/rules/use-isnan.js‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,73 @@ ruleTester.run("use-isnan", rule, {
374374
code: "foo.lastIndexOf((Number.NaN, 1))",
375375
options: [{ enforceForIndexOf: true }],
376376
},
377+
378+
//------------------------------------------------------------------------------
379+
// Shadowed NaN / Number.NaN
380+
//------------------------------------------------------------------------------
381+
382+
"let NaN; if (x === NaN) {}",
383+
"let Number; if (x === Number.NaN) {}",
384+
"function f(NaN) { return x === NaN; }",
385+
"function f(Number) { return x === Number.NaN; }",
386+
{
387+
code: "if (x === NaN) {}",
388+
languageOptions: { globals: { NaN: "off" } },
389+
},
390+
{
391+
code: "if (x === Number.NaN) {}",
392+
languageOptions: { globals: { Number: "off" } },
393+
},
394+
{
395+
code: "let NaN; switch (foo) { case NaN: break; }",
396+
options: [{ enforceForSwitchCase: true }],
397+
},
398+
{
399+
code: "let Number; switch (foo) { case Number.NaN: break; }",
400+
options: [{ enforceForSwitchCase: true }],
401+
},
402+
{
403+
code: "let NaN; switch (NaN) { case a: break; }",
404+
options: [{ enforceForSwitchCase: true }],
405+
},
406+
{
407+
code: "let Number; switch (Number.NaN) { case a: break; }",
408+
options: [{ enforceForSwitchCase: true }],
409+
},
410+
{
411+
code: "function foo(NaN) { return arr.indexOf(NaN); }",
412+
options: [{ enforceForIndexOf: true }],
413+
},
414+
{
415+
code: "function foo(NaN) { return arr.lastIndexOf(NaN); }",
416+
options: [{ enforceForIndexOf: true }],
417+
},
418+
{
419+
code: "function foo(Number) { return arr.indexOf(Number.NaN); }",
420+
options: [{ enforceForIndexOf: true }],
421+
},
422+
{
423+
code: "function foo(Number) { return arr.lastIndexOf(Number.NaN); }",
424+
options: [{ enforceForIndexOf: true }],
425+
},
426+
"let Number; if (x === Number?.NaN) {}",
427+
"function f(Number) { return x === Number?.NaN; }",
428+
{
429+
code: "let Number; switch (foo) { case Number?.NaN: break; }",
430+
options: [{ enforceForSwitchCase: true }],
431+
},
432+
{
433+
code: "let Number; switch (Number?.NaN) { case a: break; }",
434+
options: [{ enforceForSwitchCase: true }],
435+
},
436+
{
437+
code: "function foo(Number) { return arr.indexOf(Number?.NaN); }",
438+
options: [{ enforceForIndexOf: true }],
439+
},
440+
{
441+
code: "function foo(Number) { return arr.lastIndexOf(Number?.NaN); }",
442+
options: [{ enforceForIndexOf: true }],
443+
},
377444
],
378445
invalid: [
379446
{

0 commit comments

Comments
 (0)