From 915381398eb740d78d162427057954ab51d06125 Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 25 Jan 2021 00:00:26 +0800 Subject: [PATCH 1/2] Refactor tests --- test/prefer-includes.js | 74 ++-------- test/snapshots/prefer-includes.js.md | 181 +++++++++++++++++++++++++ test/snapshots/prefer-includes.js.snap | Bin 0 -> 660 bytes 3 files changed, 193 insertions(+), 62 deletions(-) create mode 100644 test/snapshots/prefer-includes.js.md create mode 100644 test/snapshots/prefer-includes.js.snap 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..98a0a0ee37 --- /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 0000000000000000000000000000000000000000..fbea5efc4a81a2742e77d36497bf0ba4fd5a66b5 GIT binary patch literal 660 zcmV;F0&D$2RzVVb9rg6&2^L-Bbaj*VUtxi)NP&5_0B(^iNEOlAU${(#~x z76yjw)wQwZQ*Lis#U{aX|0CCVCa~xhC=Or+syZDc&J|@YE_ZXu*^F;D?=gWztDsnj zje%jR(1G2|KPn0(>i+(^t7*;61QxZ1;;TS>*IeE%eOWLRQT*k%7w&U3p4sg?}1UwSt|kf&tJ(1)!2RJU}6|I8`A5 zVV7otjzUplNk(dsLP}xpS*_Zg_gD(RR;|E$Np=d)LBOT*dgasyO9_WU70HG3Mp9!(CKr1hh uR;f)TRH+e}q-g0QMOd=ZNK~k)QApAx(o0Cn(Gu7|cnJVmHyrNv2><~4oGg0) literal 0 HcmV?d00001 From 4db97c34ff69387b87ffb03d6d4a79785f0a5abe Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 25 Jan 2021 00:01:49 +0800 Subject: [PATCH 2/2] Improve report location --- rules/prefer-includes.js | 2 +- test/snapshots/prefer-includes.js.md | 22 +++++++++++----------- test/snapshots/prefer-includes.js.snap | Bin 660 -> 656 bytes 3 files changed, 12 insertions(+), 12 deletions(-) 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/snapshots/prefer-includes.js.md b/test/snapshots/prefer-includes.js.md index 98a0a0ee37..4f7feab13a 100644 --- a/test/snapshots/prefer-includes.js.md +++ b/test/snapshots/prefer-includes.js.md @@ -17,7 +17,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | 'foobar'.indexOf('foo') !== -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #2 @@ -33,7 +33,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | str.indexOf('foo') != -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #3 @@ -49,7 +49,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | str.indexOf('foo') > -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #4 @@ -65,7 +65,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | str.indexOf('foo') == -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #5 @@ -81,7 +81,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | 'foobar'.indexOf('foo') >= 0␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #6 @@ -97,7 +97,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | [1,2,3].indexOf(4) !== -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #7 @@ -113,7 +113,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | str.indexOf('foo') < 0␊ - | ^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #8 @@ -129,7 +129,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | ''.indexOf('foo') < 0␊ - | ^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #9 @@ -145,7 +145,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | (a || b).indexOf('foo') === -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #10 @@ -161,7 +161,7 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.indexOf(bar, 0) !== -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ ` ## Invalid #11 @@ -177,5 +177,5 @@ Generated by [AVA](https://avajs.dev). `␊ > 1 | foo.indexOf(bar, 1) !== -1␊ - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `.includes()`, rather than `.indexOf()`, when checking for existence.␊ + | ^^^^^^^ 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 index fbea5efc4a81a2742e77d36497bf0ba4fd5a66b5..581bf3ef2d70ead05b461e50346d8cb53993863b 100644 GIT binary patch delta 603 zcmV-h0;K(v1&{?IK~_N^Q*L2!b7*gLAa*he0sxk|OoyZgACfl`k9@WLyZ@0QCw~tC zvG)6UNsM97*>n{Z<@v6<@iKu$UqW#y&=l9R8{A`8`gt$-an0FX)cP9}Saco~s{@Vn zKm1n6|9fN7kF59evYPuGnZTj}P<#uBgM1fT9R2eo(#Nq{z`yX>ZzixP3o``w0C8;e zlE}4TJ8O2K0F@WfhB`w=I>xaG3rx_;=SEohj8F-&&jd?A)4}1S)h_LX pY8R|tptTn{Z<@v6<@iKu$ze4dupee3rH@L^H^z&ZuZf{z}Cc$(6BiDH*u;>;j4qyeU zIvpg=6=g0icXP?vjBhvZF@Z&^pje2Ffnlo9f!)kMDheg){{FhFY0b?97PW@rt3Z6$ zT;49^(jVhfOQwV_Tp_WM2`tLY4#CTT_^bUYb@rNPtcv2l4+d<${*nnSdS?xat2lrv z8Q8($%*e_h$f&@jpkSy_qoAIapP!Uiq^_5lmz-0Yl3J_*5>eOWLRQT*k%7w&U3p4s zg?}1UwSt|kf&tJ(1)!2RJU}6|I8`A5VV7otjzUplNk(dsLPc8+kf0}Ms2LtZ>PCU+6Bq9cA@9)9PaiB0064t8TkMJ