diff --git a/rules/prefer-set-has.js b/rules/prefer-set-has.js index 48e02b135b..ad0df2e32a 100644 --- a/rules/prefer-set-has.js +++ b/rules/prefer-set-has.js @@ -30,17 +30,7 @@ const arrayStaticMethodSelector = methodCallSelector({ path: 'init', }); -// `array.concat()` -// `array.copyWithin()` -// `array.fill()` -// `array.filter()` -// `array.flat()` -// `array.flatMap()` -// `array.map()` -// `array.reverse()` -// `array.slice()` -// `array.sort()` -// `array.splice()` +// Array methods that return an array const arrayMethodSelector = methodCallSelector({ methods: [ 'concat', @@ -54,6 +44,10 @@ const arrayMethodSelector = methodCallSelector({ 'slice', 'sort', 'splice', + 'toReversed', + 'toSorted', + 'toSpliced', + 'with', ], path: 'init', }); diff --git a/test/prefer-set-has.mjs b/test/prefer-set-has.mjs index 4dd341e6c2..54733f26dc 100644 --- a/test/prefer-set-has.mjs +++ b/test/prefer-set-has.mjs @@ -15,6 +15,10 @@ const methodsReturnsArray = [ 'slice', 'sort', 'splice', + 'toReversed', + 'toSorted', + 'toSpliced', + 'with', ]; test.snapshot({ @@ -341,19 +345,23 @@ test.snapshot({ } `), // Not MemberExpression - ...methodsReturnsArray.map(method => outdent` - const foo = ${method}(); - function unicorn() { - return foo.includes(1); - } - `), + ...methodsReturnsArray + .filter(method => method !== 'with') + .map(method => outdent` + const foo = ${method}(); + function unicorn() { + return foo.includes(1); + } + `), // Computed - ...methodsReturnsArray.map(method => outdent` - const foo = bar[${method}](); - function unicorn() { - return foo.includes(1); - } - `), + ...methodsReturnsArray + .filter(method => method !== 'with') + .map(method => outdent` + const foo = bar[${method}](); + function unicorn() { + return foo.includes(1); + } + `), // Not `Identifier` ...methodsReturnsArray.map(method => outdent` const foo = bar["${method}"](); diff --git a/test/snapshots/prefer-set-has.mjs.md b/test/snapshots/prefer-set-has.mjs.md index 1a6e1f13ef..fbf5b9afb0 100644 --- a/test/snapshots/prefer-set-has.mjs.md +++ b/test/snapshots/prefer-set-has.mjs.md @@ -1067,6 +1067,106 @@ Generated by [AVA](https://avajs.dev). ` ## Invalid #37 + 1 | const foo = bar.toReversed(); + 2 | function unicorn() { + 3 | return foo.includes(1); + 4 | } + +> Output + + `␊ + 1 | const foo = new Set(bar.toReversed());␊ + 2 | function unicorn() {␊ + 3 | return foo.has(1);␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = bar.toReversed();␊ + | ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊ + 2 | function unicorn() {␊ + 3 | return foo.includes(1);␊ + 4 | }␊ + ` + +## Invalid #38 + 1 | const foo = bar.toSorted(); + 2 | function unicorn() { + 3 | return foo.includes(1); + 4 | } + +> Output + + `␊ + 1 | const foo = new Set(bar.toSorted());␊ + 2 | function unicorn() {␊ + 3 | return foo.has(1);␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = bar.toSorted();␊ + | ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊ + 2 | function unicorn() {␊ + 3 | return foo.includes(1);␊ + 4 | }␊ + ` + +## Invalid #39 + 1 | const foo = bar.toSpliced(); + 2 | function unicorn() { + 3 | return foo.includes(1); + 4 | } + +> Output + + `␊ + 1 | const foo = new Set(bar.toSpliced());␊ + 2 | function unicorn() {␊ + 3 | return foo.has(1);␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = bar.toSpliced();␊ + | ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊ + 2 | function unicorn() {␊ + 3 | return foo.includes(1);␊ + 4 | }␊ + ` + +## Invalid #40 + 1 | const foo = bar.with(); + 2 | function unicorn() { + 3 | return foo.includes(1); + 4 | } + +> Output + + `␊ + 1 | const foo = new Set(bar.with());␊ + 2 | function unicorn() {␊ + 3 | return foo.has(1);␊ + 4 | }␊ + ` + +> Error 1/1 + + `␊ + > 1 | const foo = bar.with();␊ + | ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊ + 2 | function unicorn() {␊ + 3 | return foo.includes(1);␊ + 4 | }␊ + ` + +## Invalid #41 1 | const foo = _([1,2,3]); 2 | const bar = foo.map(value => value); 3 | function unicorn() { diff --git a/test/snapshots/prefer-set-has.mjs.snap b/test/snapshots/prefer-set-has.mjs.snap index f61e80a7d2..6bcebc1f90 100644 Binary files a/test/snapshots/prefer-set-has.mjs.snap and b/test/snapshots/prefer-set-has.mjs.snap differ