Skip to content

Commit 4e539b4

Browse files
authoredJan 17, 2025··
prefer-includes: Support Vue template (#2526)
1 parent e42118d commit 4e539b4

File tree

4 files changed

+259
-25
lines changed

4 files changed

+259
-25
lines changed
 

Diff for: ‎rules/prefer-includes.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const {checkVueTemplate} = require('./utils/rule.js');
23
const isMethodNamed = require('./utils/is-method-named.js');
34
const simpleArraySearchRule = require('./shared/simple-array-search-rule.js');
45
const {isLiteral, isNegativeOne} = require('./ast/index.js');
@@ -15,8 +16,10 @@ const isNegativeResult = node => ['===', '==', '<'].includes(node.operator);
1516

1617
const getProblem = (context, node, target, argumentsNodes) => {
1718
const {sourceCode} = context;
19+
const tokenStore = sourceCode.parserServices.getTemplateBodyTokenStore?.() ?? sourceCode;
20+
1821
const memberExpressionNode = target.parent;
19-
const dotToken = sourceCode.getTokenBefore(memberExpressionNode.property);
22+
const dotToken = tokenStore.getTokenBefore(memberExpressionNode.property);
2023
const targetSource = sourceCode.getText().slice(memberExpressionNode.range[0], dotToken.range[0]);
2124

2225
// Strip default `fromIndex`
@@ -84,7 +87,7 @@ const create = context => {
8487

8588
/** @type {import('eslint').Rule.RuleModule} */
8689
module.exports = {
87-
create,
90+
create: checkVueTemplate(create),
8891
meta: {
8992
type: 'suggestion',
9093
docs: {

Diff for: ‎test/prefer-includes.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getTester} from './utils/test.mjs';
1+
import {getTester, parsers} from './utils/test.mjs';
22
import tests from './shared/simple-array-search-rule-tests.mjs';
33

44
const {test} = getTester(import.meta);
@@ -16,7 +16,7 @@ test.snapshot({
1616
'_.indexOf(foo, bar) !== -1',
1717
'lodash.indexOf(foo, bar) !== -1',
1818
'underscore.indexOf(foo, bar) !== -1',
19-
].flatMap(code => [code, code.replace('.indexOf', '.lastIndexOf')]),
19+
].flatMap(code => [code, code.replace('.indexOf', '.lastIndexOf'), {code: `<template><div v-if="${code}"></div></template>`, languageOptions: {parser: parsers.vue}}]),
2020
'str.includes(\'foo\')',
2121
'\'foobar\'.includes(\'foo\')',
2222
'[1,2,3].includes(4)',
@@ -34,7 +34,7 @@ test.snapshot({
3434
'(a || b).indexOf(\'foo\') === -1',
3535
'foo.indexOf(bar, 0) !== -1',
3636
'foo.indexOf(bar, 1) !== -1',
37-
].flatMap(code => [code, code.replace('.indexOf', '.lastIndexOf')]),
37+
].flatMap(code => [code, code.replace('.indexOf', '.lastIndexOf'), {code: `<template><div v-if="${code}"></div></template>`, languageOptions: {parser: parsers.vue}}]),
3838
});
3939

4040
const {snapshot, typescript} = tests({

Diff for: ‎test/snapshots/prefer-includes.mjs.md

+251-20
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,28 @@ Generated by [AVA](https://avajs.dev).
4646
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
4747
`
4848

49-
## invalid(3): str.indexOf('foo') != -1
49+
## invalid(3): <template><div v-if="'foobar'.indexOf('foo') !== -1"></div></template>
50+
51+
> Input
52+
53+
`␊
54+
1 | <template><div v-if="'foobar'.indexOf('foo') !== -1"></div></template>␊
55+
`
56+
57+
> Output
58+
59+
`␊
60+
1 | <template><div v-if="'foobar'.includes('foo')"></div></template>␊
61+
`
62+
63+
> Error 1/1
64+
65+
`␊
66+
> 1 | <template><div v-if="'foobar'.indexOf('foo') !== -1"></div></template>␊
67+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
68+
`
69+
70+
## invalid(4): str.indexOf('foo') != -1
5071

5172
> Input
5273
@@ -67,7 +88,7 @@ Generated by [AVA](https://avajs.dev).
6788
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
6889
`
6990

70-
## invalid(4): str.lastIndexOf('foo') != -1
91+
## invalid(5): str.lastIndexOf('foo') != -1
7192

7293
> Input
7394
@@ -88,7 +109,28 @@ Generated by [AVA](https://avajs.dev).
88109
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
89110
`
90111

91-
## invalid(5): str.indexOf('foo') > -1
112+
## invalid(6): <template><div v-if="str.indexOf('foo') != -1"></div></template>
113+
114+
> Input
115+
116+
`␊
117+
1 | <template><div v-if="str.indexOf('foo') != -1"></div></template>␊
118+
`
119+
120+
> Output
121+
122+
`␊
123+
1 | <template><div v-if="str.includes('foo')"></div></template>␊
124+
`
125+
126+
> Error 1/1
127+
128+
`␊
129+
> 1 | <template><div v-if="str.indexOf('foo') != -1"></div></template>␊
130+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
131+
`
132+
133+
## invalid(7): str.indexOf('foo') > -1
92134

93135
> Input
94136
@@ -109,7 +151,7 @@ Generated by [AVA](https://avajs.dev).
109151
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
110152
`
111153

112-
## invalid(6): str.lastIndexOf('foo') > -1
154+
## invalid(8): str.lastIndexOf('foo') > -1
113155

114156
> Input
115157
@@ -130,7 +172,28 @@ Generated by [AVA](https://avajs.dev).
130172
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
131173
`
132174

133-
## invalid(7): str.indexOf('foo') == -1
175+
## invalid(9): <template><div v-if="str.indexOf('foo') > -1"></div></template>
176+
177+
> Input
178+
179+
`␊
180+
1 | <template><div v-if="str.indexOf('foo') > -1"></div></template>␊
181+
`
182+
183+
> Output
184+
185+
`␊
186+
1 | <template><div v-if="str.includes('foo')"></div></template>␊
187+
`
188+
189+
> Error 1/1
190+
191+
`␊
192+
> 1 | <template><div v-if="str.indexOf('foo') > -1"></div></template>␊
193+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
194+
`
195+
196+
## invalid(10): str.indexOf('foo') == -1
134197

135198
> Input
136199
@@ -151,7 +214,7 @@ Generated by [AVA](https://avajs.dev).
151214
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
152215
`
153216

154-
## invalid(8): str.lastIndexOf('foo') == -1
217+
## invalid(11): str.lastIndexOf('foo') == -1
155218

156219
> Input
157220
@@ -172,7 +235,28 @@ Generated by [AVA](https://avajs.dev).
172235
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
173236
`
174237

175-
## invalid(9): 'foobar'.indexOf('foo') >= 0
238+
## invalid(12): <template><div v-if="str.indexOf('foo') == -1"></div></template>
239+
240+
> Input
241+
242+
`␊
243+
1 | <template><div v-if="str.indexOf('foo') == -1"></div></template>␊
244+
`
245+
246+
> Output
247+
248+
`␊
249+
1 | <template><div v-if="!str.includes('foo')"></div></template>␊
250+
`
251+
252+
> Error 1/1
253+
254+
`␊
255+
> 1 | <template><div v-if="str.indexOf('foo') == -1"></div></template>␊
256+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
257+
`
258+
259+
## invalid(13): 'foobar'.indexOf('foo') >= 0
176260

177261
> Input
178262
@@ -193,7 +277,7 @@ Generated by [AVA](https://avajs.dev).
193277
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
194278
`
195279

196-
## invalid(10): 'foobar'.lastIndexOf('foo') >= 0
280+
## invalid(14): 'foobar'.lastIndexOf('foo') >= 0
197281

198282
> Input
199283
@@ -214,7 +298,28 @@ Generated by [AVA](https://avajs.dev).
214298
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
215299
`
216300

217-
## invalid(11): [1,2,3].indexOf(4) !== -1
301+
## invalid(15): <template><div v-if="'foobar'.indexOf('foo') >= 0"></div></template>
302+
303+
> Input
304+
305+
`␊
306+
1 | <template><div v-if="'foobar'.indexOf('foo') >= 0"></div></template>␊
307+
`
308+
309+
> Output
310+
311+
`␊
312+
1 | <template><div v-if="'foobar'.includes('foo')"></div></template>␊
313+
`
314+
315+
> Error 1/1
316+
317+
`␊
318+
> 1 | <template><div v-if="'foobar'.indexOf('foo') >= 0"></div></template>␊
319+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
320+
`
321+
322+
## invalid(16): [1,2,3].indexOf(4) !== -1
218323

219324
> Input
220325
@@ -235,7 +340,7 @@ Generated by [AVA](https://avajs.dev).
235340
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
236341
`
237342

238-
## invalid(12): [1,2,3].lastIndexOf(4) !== -1
343+
## invalid(17): [1,2,3].lastIndexOf(4) !== -1
239344

240345
> Input
241346
@@ -256,7 +361,28 @@ Generated by [AVA](https://avajs.dev).
256361
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
257362
`
258363

259-
## invalid(13): str.indexOf('foo') < 0
364+
## invalid(18): <template><div v-if="[1,2,3].indexOf(4) !== -1"></div></template>
365+
366+
> Input
367+
368+
`␊
369+
1 | <template><div v-if="[1,2,3].indexOf(4) !== -1"></div></template>␊
370+
`
371+
372+
> Output
373+
374+
`␊
375+
1 | <template><div v-if="[1,2,3].includes(4)"></div></template>␊
376+
`
377+
378+
> Error 1/1
379+
380+
`␊
381+
> 1 | <template><div v-if="[1,2,3].indexOf(4) !== -1"></div></template>␊
382+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
383+
`
384+
385+
## invalid(19): str.indexOf('foo') < 0
260386

261387
> Input
262388
@@ -277,7 +403,7 @@ Generated by [AVA](https://avajs.dev).
277403
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
278404
`
279405

280-
## invalid(14): str.lastIndexOf('foo') < 0
406+
## invalid(20): str.lastIndexOf('foo') < 0
281407

282408
> Input
283409
@@ -298,7 +424,28 @@ Generated by [AVA](https://avajs.dev).
298424
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
299425
`
300426

301-
## invalid(15): ''.indexOf('foo') < 0
427+
## invalid(21): <template><div v-if="str.indexOf('foo') < 0"></div></template>
428+
429+
> Input
430+
431+
`␊
432+
1 | <template><div v-if="str.indexOf('foo') < 0"></div></template>␊
433+
`
434+
435+
> Output
436+
437+
`␊
438+
1 | <template><div v-if="!str.includes('foo')"></div></template>␊
439+
`
440+
441+
> Error 1/1
442+
443+
`␊
444+
> 1 | <template><div v-if="str.indexOf('foo') < 0"></div></template>␊
445+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
446+
`
447+
448+
## invalid(22): ''.indexOf('foo') < 0
302449

303450
> Input
304451
@@ -319,7 +466,7 @@ Generated by [AVA](https://avajs.dev).
319466
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
320467
`
321468

322-
## invalid(16): ''.lastIndexOf('foo') < 0
469+
## invalid(23): ''.lastIndexOf('foo') < 0
323470

324471
> Input
325472
@@ -340,7 +487,28 @@ Generated by [AVA](https://avajs.dev).
340487
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
341488
`
342489

343-
## invalid(17): (a || b).indexOf('foo') === -1
490+
## invalid(24): <template><div v-if="''.indexOf('foo') < 0"></div></template>
491+
492+
> Input
493+
494+
`␊
495+
1 | <template><div v-if="''.indexOf('foo') < 0"></div></template>␊
496+
`
497+
498+
> Output
499+
500+
`␊
501+
1 | <template><div v-if="!''.includes('foo')"></div></template>␊
502+
`
503+
504+
> Error 1/1
505+
506+
`␊
507+
> 1 | <template><div v-if="''.indexOf('foo') < 0"></div></template>␊
508+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
509+
`
510+
511+
## invalid(25): (a || b).indexOf('foo') === -1
344512

345513
> Input
346514
@@ -361,7 +529,7 @@ Generated by [AVA](https://avajs.dev).
361529
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
362530
`
363531

364-
## invalid(18): (a || b).lastIndexOf('foo') === -1
532+
## invalid(26): (a || b).lastIndexOf('foo') === -1
365533

366534
> Input
367535
@@ -382,7 +550,28 @@ Generated by [AVA](https://avajs.dev).
382550
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
383551
`
384552

385-
## invalid(19): foo.indexOf(bar, 0) !== -1
553+
## invalid(27): <template><div v-if="(a || b).indexOf('foo') === -1"></div></template>
554+
555+
> Input
556+
557+
`␊
558+
1 | <template><div v-if="(a || b).indexOf('foo') === -1"></div></template>␊
559+
`
560+
561+
> Output
562+
563+
`␊
564+
1 | <template><div v-if="!(a || b).includes('foo')"></div></template>␊
565+
`
566+
567+
> Error 1/1
568+
569+
`␊
570+
> 1 | <template><div v-if="(a || b).indexOf('foo') === -1"></div></template>␊
571+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
572+
`
573+
574+
## invalid(28): foo.indexOf(bar, 0) !== -1
386575

387576
> Input
388577
@@ -403,7 +592,7 @@ Generated by [AVA](https://avajs.dev).
403592
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
404593
`
405594

406-
## invalid(20): foo.lastIndexOf(bar, 0) !== -1
595+
## invalid(29): foo.lastIndexOf(bar, 0) !== -1
407596

408597
> Input
409598
@@ -424,7 +613,28 @@ Generated by [AVA](https://avajs.dev).
424613
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
425614
`
426615

427-
## invalid(21): foo.indexOf(bar, 1) !== -1
616+
## invalid(30): <template><div v-if="foo.indexOf(bar, 0) !== -1"></div></template>
617+
618+
> Input
619+
620+
`␊
621+
1 | <template><div v-if="foo.indexOf(bar, 0) !== -1"></div></template>␊
622+
`
623+
624+
> Output
625+
626+
`␊
627+
1 | <template><div v-if="foo.includes(bar)"></div></template>␊
628+
`
629+
630+
> Error 1/1
631+
632+
`␊
633+
> 1 | <template><div v-if="foo.indexOf(bar, 0) !== -1"></div></template>␊
634+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
635+
`
636+
637+
## invalid(31): foo.indexOf(bar, 1) !== -1
428638

429639
> Input
430640
@@ -445,7 +655,7 @@ Generated by [AVA](https://avajs.dev).
445655
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
446656
`
447657

448-
## invalid(22): foo.lastIndexOf(bar, 1) !== -1
658+
## invalid(32): foo.lastIndexOf(bar, 1) !== -1
449659

450660
> Input
451661
@@ -466,6 +676,27 @@ Generated by [AVA](https://avajs.dev).
466676
| ^^^^^^^^^^^ Use \`.includes()\`, rather than \`.lastIndexOf()\`, when checking for existence.␊
467677
`
468678

679+
## invalid(33): <template><div v-if="foo.indexOf(bar, 1) !== -1"></div></template>
680+
681+
> Input
682+
683+
`␊
684+
1 | <template><div v-if="foo.indexOf(bar, 1) !== -1"></div></template>␊
685+
`
686+
687+
> Output
688+
689+
`␊
690+
1 | <template><div v-if="foo.includes(bar, 1)"></div></template>␊
691+
`
692+
693+
> Error 1/1
694+
695+
`␊
696+
> 1 | <template><div v-if="foo.indexOf(bar, 1) !== -1"></div></template>␊
697+
| ^^^^^^^ Use \`.includes()\`, rather than \`.indexOf()\`, when checking for existence.␊
698+
`
699+
469700
## invalid(1): values.some(x => x === "foo")
470701

471702
> Input

Diff for: ‎test/snapshots/prefer-includes.mjs.snap

478 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.