diff --git a/rules/shared/simple-array-search-rule.js b/rules/shared/simple-array-search-rule.js index 1fb34338df..4b648a4034 100644 --- a/rules/shared/simple-array-search-rule.js +++ b/rules/shared/simple-array-search-rule.js @@ -38,10 +38,11 @@ const isIdentifierNamed = ({type, name}, expectName) => type === 'Identifier' && function simpleArraySearchRule({method, replacement}) { // Add prefix to avoid conflicts in `prefer-includes` rule const MESSAGE_ID_PREFIX = `prefer-${replacement}-over-${method}/`; - const ERROR = `${MESSAGE_ID_PREFIX}/error`; - const SUGGESTION = `${MESSAGE_ID_PREFIX}/suggestion`; + const ERROR = `${MESSAGE_ID_PREFIX}error`; + const SUGGESTION = `${MESSAGE_ID_PREFIX}suggestion`; const ERROR_MESSAGES = { findIndex: 'Use `.indexOf()` instead of `.findIndex()` when looking for the index of an item.', + findLastIndex: 'Use `.lastIndexOf()` instead of `findLastIndex() when looking for the index of an item.`', some: `Use \`.${replacement}()\` instead of \`.${method}()\` when checking value existence.`, }; diff --git a/test/snapshots/prefer-array-index-of.mjs.md b/test/snapshots/prefer-array-index-of.mjs.md index 982c408769..4e6c7b0fd4 100644 --- a/test/snapshots/prefer-array-index-of.mjs.md +++ b/test/snapshots/prefer-array-index-of.mjs.md @@ -264,7 +264,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => x === "foo")␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #2 @@ -280,7 +280,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => "foo" === x)␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #3 @@ -296,7 +296,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => {return x === "foo";})␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #4 @@ -312,7 +312,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(function (x) {return x === "foo";})␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #5 @@ -343,7 +343,7 @@ Generated by [AVA](https://avajs.dev). 2 | (0, values)␊ 3 | // 2␊ > 4 | ./* 3 */findLastIndex /* 3 */ (␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 5 | /* 4 */␊ 6 | x /* 5 */ => /* 6 */ x /* 7 */ === /* 8 */ "foo" /* 9 */␊ 7 | ) /* 10 */␊ @@ -366,7 +366,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.findLastIndex(function (element) {␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 2 | return element === bar.findLastIndex(x => x === 1);␊ 3 | });␊ ␊ @@ -380,7 +380,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | foo.findLastIndex(function (element) {␊ > 2 | return element === bar.findLastIndex(x => x === 1);␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 3 | });␊ ` @@ -397,7 +397,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => x === (0, "foo"))␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #8 @@ -413,7 +413,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex((x => x === (0, "foo")))␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ` ## Invalid #9 @@ -434,7 +434,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | function fn() {␊ > 2 | foo.findLastIndex(x => x === arguments.length)␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 3 | }␊ ` @@ -456,7 +456,7 @@ Generated by [AVA](https://avajs.dev). `␊ 1 | function fn() {␊ > 2 | foo.findLastIndex(x => x === this[1])␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 3 | }␊ ` @@ -467,7 +467,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | values.findLastIndex(x => x === foo())␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ ␊ --------------------------------------------------------------------------------␊ Suggestion 1/1: Replace \`.findLastIndex()\` with \`.lastIndexOf()\`.␊ @@ -485,7 +485,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.findLastIndex(function a(x) {␊ - | ^^^^^^^^^^^^^␊ + | ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`␊ 2 | return x === (function (a) {␊ 3 | return a(this) === arguments[1]␊ 4 | }).call(thisObject, anotherFunctionNamedA, secondArgument)␊ diff --git a/test/snapshots/prefer-array-index-of.mjs.snap b/test/snapshots/prefer-array-index-of.mjs.snap index ef25c456aa..15e4309358 100644 Binary files a/test/snapshots/prefer-array-index-of.mjs.snap and b/test/snapshots/prefer-array-index-of.mjs.snap differ diff --git a/test/utils/snapshot-rule-tester.mjs b/test/utils/snapshot-rule-tester.mjs index a4c5179463..fad22df243 100644 --- a/test/utils/snapshot-rule-tester.mjs +++ b/test/utils/snapshot-rule-tester.mjs @@ -126,6 +126,12 @@ function defineParser(linter, parser) { function verify(linter, code, verifyConfig, {filename}) { const messages = linter.verify(code, verifyConfig, {filename}); + // Missed `message`, #1923 + const invalidMessage = messages.find(({message}) => typeof message !== 'string'); + if (invalidMessage) { + throw Object.assign(new Error('Unexpected message.'), {eslintMessage: invalidMessage}); + } + const fatalError = messages.find(({fatal}) => fatal); if (fatalError) { const {line, column, message} = fatalError;