diff --git a/rules/prefer-includes.js b/rules/prefer-includes.js index 1346dd03c1..c023d6b92d 100644 --- a/rules/prefer-includes.js +++ b/rules/prefer-includes.js @@ -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(', ')})`; diff --git a/test/prefer-includes.js b/test/prefer-includes.js index 051881f200..d6c77b5e0d 100644 --- a/test/prefer-includes.js +++ b/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', @@ -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' ] }); diff --git a/test/snapshots/prefer-includes.js.md b/test/snapshots/prefer-includes.js.md new file mode 100644 index 0000000000..4f7feab13a --- /dev/null +++ b/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.␊ + ` diff --git a/test/snapshots/prefer-includes.js.snap b/test/snapshots/prefer-includes.js.snap new file mode 100644 index 0000000000..581bf3ef2d Binary files /dev/null and b/test/snapshots/prefer-includes.js.snap differ