Skip to content

Commit 83575a8

Browse files
authored
prefer-includes: Improve report location (#1061)
1 parent aca2ec5 commit 83575a8

File tree

4 files changed

+194
-63
lines changed

4 files changed

+194
-63
lines changed

rules/prefer-includes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const report = (context, node, target, argumentsNodes) => {
2828
const argumentsSource = argumentsNodes.map(argument => sourceCode.getText(argument));
2929

3030
context.report({
31-
node,
31+
node: memberExpressionNode.property,
3232
messageId: MESSAGE_ID,
3333
fix: fixer => {
3434
const replacement = `${isNegativeResult(node) ? '!' : ''}${targetSource}.includes(${argumentsSource.join(', ')})`;

test/prefer-includes.js

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import {test} from './utils/test.js';
22

3-
const errors = [
4-
{
5-
message: 'Use `.includes()`, rather than `.indexOf()`, when checking for existence.'
6-
}
7-
];
8-
9-
test({
3+
test.snapshot({
104
valid: [
115
'str.indexOf(\'foo\') !== -n',
126
'str.indexOf(\'foo\') !== 1',
@@ -24,60 +18,16 @@ test({
2418
'underscore.indexOf(foo, bar) !== -1'
2519
],
2620
invalid: [
27-
{
28-
code: '\'foobar\'.indexOf(\'foo\') !== -1',
29-
output: '\'foobar\'.includes(\'foo\')',
30-
errors
31-
},
32-
{
33-
code: 'str.indexOf(\'foo\') != -1',
34-
output: 'str.includes(\'foo\')',
35-
errors
36-
},
37-
{
38-
code: 'str.indexOf(\'foo\') > -1',
39-
output: 'str.includes(\'foo\')',
40-
errors
41-
},
42-
{
43-
code: 'str.indexOf(\'foo\') == -1',
44-
output: '!str.includes(\'foo\')',
45-
errors
46-
},
47-
{
48-
code: '\'foobar\'.indexOf(\'foo\') >= 0',
49-
output: '\'foobar\'.includes(\'foo\')',
50-
errors
51-
},
52-
{
53-
code: '[1,2,3].indexOf(4) !== -1',
54-
output: '[1,2,3].includes(4)',
55-
errors
56-
},
57-
{
58-
code: 'str.indexOf(\'foo\') < 0',
59-
output: '!str.includes(\'foo\')',
60-
errors
61-
},
62-
{
63-
code: '\'\'.indexOf(\'foo\') < 0',
64-
output: '!\'\'.includes(\'foo\')',
65-
errors
66-
},
67-
{
68-
code: '(a || b).indexOf(\'foo\') === -1',
69-
output: '!(a || b).includes(\'foo\')',
70-
errors
71-
},
72-
{
73-
code: 'foo.indexOf(bar, 0) !== -1',
74-
output: 'foo.includes(bar)',
75-
errors
76-
},
77-
{
78-
code: 'foo.indexOf(bar, 1) !== -1',
79-
output: 'foo.includes(bar, 1)',
80-
errors
81-
}
21+
'\'foobar\'.indexOf(\'foo\') !== -1',
22+
'str.indexOf(\'foo\') != -1',
23+
'str.indexOf(\'foo\') > -1',
24+
'str.indexOf(\'foo\') == -1',
25+
'\'foobar\'.indexOf(\'foo\') >= 0',
26+
'[1,2,3].indexOf(4) !== -1',
27+
'str.indexOf(\'foo\') < 0',
28+
'\'\'.indexOf(\'foo\') < 0',
29+
'(a || b).indexOf(\'foo\') === -1',
30+
'foo.indexOf(bar, 0) !== -1',
31+
'foo.indexOf(bar, 1) !== -1'
8232
]
8333
});

test/snapshots/prefer-includes.js.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Snapshot report for `test/prefer-includes.js`
2+
3+
The actual snapshot is saved in `prefer-includes.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## Invalid #1
8+
1 | 'foobar'.indexOf('foo') !== -1
9+
10+
> Output
11+
12+
`␊
13+
1 | 'foobar'.includes('foo')␊
14+
`
15+
16+
> Error 1/1
17+
18+
`␊
19+
> 1 | 'foobar'.indexOf('foo') !== -1␊
20+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
21+
`
22+
23+
## Invalid #2
24+
1 | str.indexOf('foo') != -1
25+
26+
> Output
27+
28+
`␊
29+
1 | str.includes('foo')␊
30+
`
31+
32+
> Error 1/1
33+
34+
`␊
35+
> 1 | str.indexOf('foo') != -1␊
36+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
37+
`
38+
39+
## Invalid #3
40+
1 | str.indexOf('foo') > -1
41+
42+
> Output
43+
44+
`␊
45+
1 | str.includes('foo')␊
46+
`
47+
48+
> Error 1/1
49+
50+
`␊
51+
> 1 | str.indexOf('foo') > -1␊
52+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
53+
`
54+
55+
## Invalid #4
56+
1 | str.indexOf('foo') == -1
57+
58+
> Output
59+
60+
`␊
61+
1 | !str.includes('foo')␊
62+
`
63+
64+
> Error 1/1
65+
66+
`␊
67+
> 1 | str.indexOf('foo') == -1␊
68+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
69+
`
70+
71+
## Invalid #5
72+
1 | 'foobar'.indexOf('foo') >= 0
73+
74+
> Output
75+
76+
`␊
77+
1 | 'foobar'.includes('foo')␊
78+
`
79+
80+
> Error 1/1
81+
82+
`␊
83+
> 1 | 'foobar'.indexOf('foo') >= 0␊
84+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
85+
`
86+
87+
## Invalid #6
88+
1 | [1,2,3].indexOf(4) !== -1
89+
90+
> Output
91+
92+
`␊
93+
1 | [1,2,3].includes(4)␊
94+
`
95+
96+
> Error 1/1
97+
98+
`␊
99+
> 1 | [1,2,3].indexOf(4) !== -1␊
100+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
101+
`
102+
103+
## Invalid #7
104+
1 | str.indexOf('foo') < 0
105+
106+
> Output
107+
108+
`␊
109+
1 | !str.includes('foo')␊
110+
`
111+
112+
> Error 1/1
113+
114+
`␊
115+
> 1 | str.indexOf('foo') < 0␊
116+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
117+
`
118+
119+
## Invalid #8
120+
1 | ''.indexOf('foo') < 0
121+
122+
> Output
123+
124+
`␊
125+
1 | !''.includes('foo')␊
126+
`
127+
128+
> Error 1/1
129+
130+
`␊
131+
> 1 | ''.indexOf('foo') < 0␊
132+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
133+
`
134+
135+
## Invalid #9
136+
1 | (a || b).indexOf('foo') === -1
137+
138+
> Output
139+
140+
`␊
141+
1 | !(a || b).includes('foo')␊
142+
`
143+
144+
> Error 1/1
145+
146+
`␊
147+
> 1 | (a || b).indexOf('foo') === -1␊
148+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
149+
`
150+
151+
## Invalid #10
152+
1 | foo.indexOf(bar, 0) !== -1
153+
154+
> Output
155+
156+
`␊
157+
1 | foo.includes(bar)␊
158+
`
159+
160+
> Error 1/1
161+
162+
`␊
163+
> 1 | foo.indexOf(bar, 0) !== -1␊
164+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
165+
`
166+
167+
## Invalid #11
168+
1 | foo.indexOf(bar, 1) !== -1
169+
170+
> Output
171+
172+
`␊
173+
1 | foo.includes(bar, 1)␊
174+
`
175+
176+
> Error 1/1
177+
178+
`␊
179+
> 1 | foo.indexOf(bar, 1) !== -1␊
180+
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
181+
`
656 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)