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

feat: use-isnan report NaN in indexOf and lastIndexOf with fromIndex #18225

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions docs/src/rules/use-isnan.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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: []
}]
}
]
});