diff --git a/docs/src/rules/use-isnan.md b/docs/src/rules/use-isnan.md index f3de5919d184..6097afb7d1f0 100644 --- a/docs/src/rules/use-isnan.md +++ b/docs/src/rules/use-isnan.md @@ -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); ``` ::: diff --git a/lib/rules/use-isnan.js b/lib/rules/use-isnan.js index 06b2284ecd5a..5c0b65feefba 100644 --- a/lib/rules/use-isnan.js +++ b/lib/rules/use-isnan.js @@ -182,7 +182,7 @@ module.exports = { if ( (methodName === "indexOf" || methodName === "lastIndexOf") && - node.arguments.length === 1 && + node.arguments.length <= 2 && isNaNIdentifier(node.arguments[0]) ) { @@ -190,7 +190,7 @@ module.exports = { * 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) { diff --git a/tests/lib/rules/use-isnan.js b/tests/lib/rules/use-isnan.js index 6cb61821952d..3a12ffadb35d 100644 --- a/tests/lib/rules/use-isnan.js +++ b/tests/lib/rules/use-isnan.js @@ -263,7 +263,7 @@ ruleTester.run("use-isnan", rule, { options: [{ enforceForIndexOf: true }] }, { - code: "foo.lastIndexOf(NaN, b)", + code: "foo.lastIndexOf(NaN, b, c)", options: [{ enforceForIndexOf: true }] }, { @@ -271,7 +271,7 @@ ruleTester.run("use-isnan", rule, { options: [{ enforceForIndexOf: true }] }, { - code: "foo.lastIndexOf(NaN, NaN)", + code: "foo.lastIndexOf(NaN, NaN, b)", options: [{ enforceForIndexOf: true }] }, { @@ -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 }] }, { @@ -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: [] + }] } ] });