Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-includes: Improve report location #1061

Merged
merged 2 commits into from Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.