Skip to content

Commit

Permalink
prefer-includes: Improve report location (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 24, 2021
1 parent aca2ec5 commit 83575a8
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 63 deletions.
2 changes: 1 addition & 1 deletion rules/prefer-includes.js
Expand Up @@ -28,7 +28,7 @@ const report = (context, node, target, argumentsNodes) => {
const argumentsSource = argumentsNodes.map(argument => sourceCode.getText(argument));

context.report({
node,
node: memberExpressionNode.property,
messageId: MESSAGE_ID,
fix: fixer => {
const replacement = `${isNegativeResult(node) ? '!' : ''}${targetSource}.includes(${argumentsSource.join(', ')})`;
Expand Down
74 changes: 12 additions & 62 deletions test/prefer-includes.js
@@ -1,12 +1,6 @@
import {test} from './utils/test.js';

const errors = [
{
message: 'Use `.includes()`, rather than `.indexOf()`, when checking for existence.'
}
];

test({
test.snapshot({
valid: [
'str.indexOf(\'foo\') !== -n',
'str.indexOf(\'foo\') !== 1',
Expand All @@ -24,60 +18,16 @@ test({
'underscore.indexOf(foo, bar) !== -1'
],
invalid: [
{
code: '\'foobar\'.indexOf(\'foo\') !== -1',
output: '\'foobar\'.includes(\'foo\')',
errors
},
{
code: 'str.indexOf(\'foo\') != -1',
output: 'str.includes(\'foo\')',
errors
},
{
code: 'str.indexOf(\'foo\') > -1',
output: 'str.includes(\'foo\')',
errors
},
{
code: 'str.indexOf(\'foo\') == -1',
output: '!str.includes(\'foo\')',
errors
},
{
code: '\'foobar\'.indexOf(\'foo\') >= 0',
output: '\'foobar\'.includes(\'foo\')',
errors
},
{
code: '[1,2,3].indexOf(4) !== -1',
output: '[1,2,3].includes(4)',
errors
},
{
code: 'str.indexOf(\'foo\') < 0',
output: '!str.includes(\'foo\')',
errors
},
{
code: '\'\'.indexOf(\'foo\') < 0',
output: '!\'\'.includes(\'foo\')',
errors
},
{
code: '(a || b).indexOf(\'foo\') === -1',
output: '!(a || b).includes(\'foo\')',
errors
},
{
code: 'foo.indexOf(bar, 0) !== -1',
output: 'foo.includes(bar)',
errors
},
{
code: 'foo.indexOf(bar, 1) !== -1',
output: 'foo.includes(bar, 1)',
errors
}
'\'foobar\'.indexOf(\'foo\') !== -1',
'str.indexOf(\'foo\') != -1',
'str.indexOf(\'foo\') > -1',
'str.indexOf(\'foo\') == -1',
'\'foobar\'.indexOf(\'foo\') >= 0',
'[1,2,3].indexOf(4) !== -1',
'str.indexOf(\'foo\') < 0',
'\'\'.indexOf(\'foo\') < 0',
'(a || b).indexOf(\'foo\') === -1',
'foo.indexOf(bar, 0) !== -1',
'foo.indexOf(bar, 1) !== -1'
]
});
181 changes: 181 additions & 0 deletions test/snapshots/prefer-includes.js.md
@@ -0,0 +1,181 @@
# Snapshot report for `test/prefer-includes.js`

The actual snapshot is saved in `prefer-includes.js.snap`.

Generated by [AVA](https://avajs.dev).

## Invalid #1
1 | 'foobar'.indexOf('foo') !== -1

> Output
`␊
1 | 'foobar'.includes('foo')␊
`

> Error 1/1
`␊
> 1 | 'foobar'.indexOf('foo') !== -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #2
1 | str.indexOf('foo') != -1

> Output
`␊
1 | str.includes('foo')␊
`

> Error 1/1
`␊
> 1 | str.indexOf('foo') != -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #3
1 | str.indexOf('foo') > -1

> Output
`␊
1 | str.includes('foo')␊
`

> Error 1/1
`␊
> 1 | str.indexOf('foo') > -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #4
1 | str.indexOf('foo') == -1

> Output
`␊
1 | !str.includes('foo')␊
`

> Error 1/1
`␊
> 1 | str.indexOf('foo') == -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #5
1 | 'foobar'.indexOf('foo') >= 0

> Output
`␊
1 | 'foobar'.includes('foo')␊
`

> Error 1/1
`␊
> 1 | 'foobar'.indexOf('foo') >= 0␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #6
1 | [1,2,3].indexOf(4) !== -1

> Output
`␊
1 | [1,2,3].includes(4)␊
`

> Error 1/1
`␊
> 1 | [1,2,3].indexOf(4) !== -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #7
1 | str.indexOf('foo') < 0

> Output
`␊
1 | !str.includes('foo')␊
`

> Error 1/1
`␊
> 1 | str.indexOf('foo') < 0␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #8
1 | ''.indexOf('foo') < 0

> Output
`␊
1 | !''.includes('foo')␊
`

> Error 1/1
`␊
> 1 | ''.indexOf('foo') < 0␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #9
1 | (a || b).indexOf('foo') === -1

> Output
`␊
1 | !(a || b).includes('foo')␊
`

> Error 1/1
`␊
> 1 | (a || b).indexOf('foo') === -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #10
1 | foo.indexOf(bar, 0) !== -1

> Output
`␊
1 | foo.includes(bar)␊
`

> Error 1/1
`␊
> 1 | foo.indexOf(bar, 0) !== -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`

## Invalid #11
1 | foo.indexOf(bar, 1) !== -1

> Output
`␊
1 | foo.includes(bar, 1)␊
`

> Error 1/1
`␊
> 1 | foo.indexOf(bar, 1) !== -1␊
| ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊
`
Binary file added test/snapshots/prefer-includes.js.snap
Binary file not shown.

0 comments on commit 83575a8

Please sign in to comment.