Skip to content

Commit a981a13

Browse files
authored
prefer-array-index-of: Add missing message (#1924)
1 parent 468d5ad commit a981a13

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

rules/shared/simple-array-search-rule.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ const isIdentifierNamed = ({type, name}, expectName) => type === 'Identifier' &&
3838
function simpleArraySearchRule({method, replacement}) {
3939
// Add prefix to avoid conflicts in `prefer-includes` rule
4040
const MESSAGE_ID_PREFIX = `prefer-${replacement}-over-${method}/`;
41-
const ERROR = `${MESSAGE_ID_PREFIX}/error`;
42-
const SUGGESTION = `${MESSAGE_ID_PREFIX}/suggestion`;
41+
const ERROR = `${MESSAGE_ID_PREFIX}error`;
42+
const SUGGESTION = `${MESSAGE_ID_PREFIX}suggestion`;
4343
const ERROR_MESSAGES = {
4444
findIndex: 'Use `.indexOf()` instead of `.findIndex()` when looking for the index of an item.',
45+
findLastIndex: 'Use `.lastIndexOf()` instead of `findLastIndex() when looking for the index of an item.`',
4546
some: `Use \`.${replacement}()\` instead of \`.${method}()\` when checking value existence.`,
4647
};
4748

test/snapshots/prefer-array-index-of.mjs.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Generated by [AVA](https://avajs.dev).
264264
265265
`␊
266266
> 1 | values.findLastIndex(x => x === "foo")␊
267-
| ^^^^^^^^^^^^^␊
267+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
268268
`
269269

270270
## Invalid #2
@@ -280,7 +280,7 @@ Generated by [AVA](https://avajs.dev).
280280
281281
`␊
282282
> 1 | values.findLastIndex(x => "foo" === x)␊
283-
| ^^^^^^^^^^^^^␊
283+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
284284
`
285285

286286
## Invalid #3
@@ -296,7 +296,7 @@ Generated by [AVA](https://avajs.dev).
296296
297297
`␊
298298
> 1 | values.findLastIndex(x => {return x === "foo";})␊
299-
| ^^^^^^^^^^^^^␊
299+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
300300
`
301301

302302
## Invalid #4
@@ -312,7 +312,7 @@ Generated by [AVA](https://avajs.dev).
312312
313313
`␊
314314
> 1 | values.findLastIndex(function (x) {return x === "foo";})␊
315-
| ^^^^^^^^^^^^^␊
315+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
316316
`
317317

318318
## Invalid #5
@@ -343,7 +343,7 @@ Generated by [AVA](https://avajs.dev).
343343
2 | (0, values)␊
344344
3 | // 2␊
345345
> 4 | ./* 3 */findLastIndex /* 3 */ (␊
346-
| ^^^^^^^^^^^^^␊
346+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
347347
5 | /* 4 */␊
348348
6 | x /* 5 */ => /* 6 */ x /* 7 */ === /* 8 */ "foo" /* 9 */␊
349349
7 | ) /* 10 */␊
@@ -366,7 +366,7 @@ Generated by [AVA](https://avajs.dev).
366366
367367
`␊
368368
> 1 | foo.findLastIndex(function (element) {␊
369-
| ^^^^^^^^^^^^^␊
369+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
370370
2 | return element === bar.findLastIndex(x => x === 1);␊
371371
3 | });␊
372372
@@ -380,7 +380,7 @@ Generated by [AVA](https://avajs.dev).
380380
`␊
381381
1 | foo.findLastIndex(function (element) {␊
382382
> 2 | return element === bar.findLastIndex(x => x === 1);␊
383-
| ^^^^^^^^^^^^^␊
383+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
384384
3 | });␊
385385
`
386386

@@ -397,7 +397,7 @@ Generated by [AVA](https://avajs.dev).
397397
398398
`␊
399399
> 1 | values.findLastIndex(x => x === (0, "foo"))␊
400-
| ^^^^^^^^^^^^^␊
400+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
401401
`
402402

403403
## Invalid #8
@@ -413,7 +413,7 @@ Generated by [AVA](https://avajs.dev).
413413
414414
`␊
415415
> 1 | values.findLastIndex((x => x === (0, "foo")))␊
416-
| ^^^^^^^^^^^^^␊
416+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
417417
`
418418

419419
## Invalid #9
@@ -434,7 +434,7 @@ Generated by [AVA](https://avajs.dev).
434434
`␊
435435
1 | function fn() {␊
436436
> 2 | foo.findLastIndex(x => x === arguments.length)␊
437-
| ^^^^^^^^^^^^^␊
437+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
438438
3 | }␊
439439
`
440440

@@ -456,7 +456,7 @@ Generated by [AVA](https://avajs.dev).
456456
`␊
457457
1 | function fn() {␊
458458
> 2 | foo.findLastIndex(x => x === this[1])␊
459-
| ^^^^^^^^^^^^^␊
459+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
460460
3 | }␊
461461
`
462462

@@ -467,7 +467,7 @@ Generated by [AVA](https://avajs.dev).
467467
468468
`␊
469469
> 1 | values.findLastIndex(x => x === foo())␊
470-
| ^^^^^^^^^^^^^␊
470+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
471471
472472
--------------------------------------------------------------------------------␊
473473
Suggestion 1/1: Replace \`.findLastIndex()\` with \`.lastIndexOf()\`.␊
@@ -485,7 +485,7 @@ Generated by [AVA](https://avajs.dev).
485485
486486
`␊
487487
> 1 | foo.findLastIndex(function a(x) {␊
488-
| ^^^^^^^^^^^^^␊
488+
| ^^^^^^^^^^^^^ Use \`.lastIndexOf()\` instead of \`findLastIndex() when looking for the index of an item.\`
489489
2 | return x === (function (a) {␊
490490
3 | return a(this) === arguments[1]␊
491491
4 | }).call(thisObject, anotherFunctionNamedA, secondArgument)␊
33 Bytes
Binary file not shown.

test/utils/snapshot-rule-tester.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ function defineParser(linter, parser) {
126126
function verify(linter, code, verifyConfig, {filename}) {
127127
const messages = linter.verify(code, verifyConfig, {filename});
128128

129+
// Missed `message`, #1923
130+
const invalidMessage = messages.find(({message}) => typeof message !== 'string');
131+
if (invalidMessage) {
132+
throw Object.assign(new Error('Unexpected message.'), {eslintMessage: invalidMessage});
133+
}
134+
129135
const fatalError = messages.find(({fatal}) => fatal);
130136
if (fatalError) {
131137
const {line, column, message} = fatalError;

0 commit comments

Comments
 (0)