Skip to content

Commit

Permalink
prefer-array-index-of: Add missing message (#1924)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 5, 2022
1 parent 468d5ad commit a981a13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions rules/shared/simple-array-search-rule.js
Expand Up @@ -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.`,
};

Expand Down
26 changes: 13 additions & 13 deletions test/snapshots/prefer-array-index-of.mjs.md
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 */␊
Expand All @@ -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 | });␊
Expand All @@ -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 | });␊
`

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 | }␊
`

Expand All @@ -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 | }␊
`

Expand All @@ -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()\`.␊
Expand All @@ -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)␊
Expand Down
Binary file modified test/snapshots/prefer-array-index-of.mjs.snap
Binary file not shown.
6 changes: 6 additions & 0 deletions test/utils/snapshot-rule-tester.mjs
Expand Up @@ -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;
Expand Down

0 comments on commit a981a13

Please sign in to comment.