Skip to content

Commit

Permalink
feat: use-isnan report NaN in indexOf and lastIndexOf with fromIn…
Browse files Browse the repository at this point in the history
…dex (#18225)

* fix: report NaN with fromIndex

* revert known limitation title change

* add tests with third argument
  • Loading branch information
Tanujkanti4441 committed Mar 23, 2024
1 parent b185eb9 commit d85c436
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/src/rules/use-isnan.md
Expand Up @@ -226,6 +226,10 @@ var firstIndex = myArray.indexOf(NaN);
var lastIndex = myArray.lastIndexOf(NaN);

var indexWithSequenceExpression = myArray.indexOf((doStuff(), NaN));

var firstIndexFromSecondElement = myArray.indexOf(NaN, 1);

var lastIndexFromSecondElement = myArray.lastIndexOf(NaN, 1);
```

:::
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/use-isnan.js
Expand Up @@ -182,15 +182,15 @@ module.exports = {

if (
(methodName === "indexOf" || methodName === "lastIndexOf") &&
node.arguments.length === 1 &&
node.arguments.length <= 2 &&
isNaNIdentifier(node.arguments[0])
) {

/*
* To retain side effects, it's essential to address `NaN` beforehand, which
* is not possible with fixes like `arr.findIndex(Number.isNaN)`.
*/
const isSuggestable = node.arguments[0].type !== "SequenceExpression";
const isSuggestable = node.arguments[0].type !== "SequenceExpression" && !node.arguments[1];
const suggestedFixes = [];

if (isSuggestable) {
Expand Down
88 changes: 84 additions & 4 deletions tests/lib/rules/use-isnan.js
Expand Up @@ -263,15 +263,15 @@ ruleTester.run("use-isnan", rule, {
options: [{ enforceForIndexOf: true }]
},
{
code: "foo.lastIndexOf(NaN, b)",
code: "foo.lastIndexOf(NaN, b, c)",
options: [{ enforceForIndexOf: true }]
},
{
code: "foo.indexOf(a, b)",
options: [{ enforceForIndexOf: true }]
},
{
code: "foo.lastIndexOf(NaN, NaN)",
code: "foo.lastIndexOf(NaN, NaN, b)",
options: [{ enforceForIndexOf: true }]
},
{
Expand Down Expand Up @@ -340,11 +340,11 @@ ruleTester.run("use-isnan", rule, {
options: [{ enforceForIndexOf: true }]
},
{
code: "foo.lastIndexOf(Number.NaN, b)",
code: "foo.lastIndexOf(Number.NaN, b, c)",
options: [{ enforceForIndexOf: true }]
},
{
code: "foo.lastIndexOf(Number.NaN, NaN)",
code: "foo.lastIndexOf(Number.NaN, NaN, b)",
options: [{ enforceForIndexOf: true }]
},
{
Expand Down Expand Up @@ -1289,6 +1289,86 @@ ruleTester.run("use-isnan", rule, {
data: { methodName: "lastIndexOf" },
suggestions: []
}]
},
{
code: "foo.indexOf(NaN, 1)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "indexOf" },
suggestions: []
}]
},
{
code: "foo.lastIndexOf(NaN, 1)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "lastIndexOf" },
suggestions: []
}]
},
{
code: "foo.indexOf(NaN, b)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "indexOf" },
suggestions: []
}]
},
{
code: "foo.lastIndexOf(NaN, b)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "lastIndexOf" },
suggestions: []
}]
},
{
code: "foo.indexOf(Number.NaN, b)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "indexOf" },
suggestions: []
}]
},
{
code: "foo.lastIndexOf(Number.NaN, b)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "lastIndexOf" },
suggestions: []
}]
},
{
code: "foo.lastIndexOf(NaN, NaN)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "lastIndexOf" },
suggestions: []
}]
},
{
code: "foo.indexOf((1, NaN), 1)",
options: [{ enforceForIndexOf: true }],
languageOptions: { ecmaVersion: 2020 },
errors: [{
messageId: "indexOfNaN",
data: { methodName: "indexOf" },
suggestions: []
}]
}
]
});

0 comments on commit d85c436

Please sign in to comment.