Skip to content

Commit

Permalink
prefer-set-has: Support Array#{toReversed,toSorted,toSpliced,with} (
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 2, 2023
1 parent 00883a8 commit fea5b42
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 23 deletions.
16 changes: 5 additions & 11 deletions rules/prefer-set-has.js
Expand Up @@ -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',
Expand All @@ -54,6 +44,10 @@ const arrayMethodSelector = methodCallSelector({
'slice',
'sort',
'splice',
'toReversed',
'toSorted',
'toSpliced',
'with',
],
path: 'init',
});
Expand Down
32 changes: 20 additions & 12 deletions test/prefer-set-has.mjs
Expand Up @@ -15,6 +15,10 @@ const methodsReturnsArray = [
'slice',
'sort',
'splice',
'toReversed',
'toSorted',
'toSpliced',
'with',
];

test.snapshot({
Expand Down Expand Up @@ -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}"]();
Expand Down
100 changes: 100 additions & 0 deletions test/snapshots/prefer-set-has.mjs.md
Expand Up @@ -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() {
Expand Down
Binary file modified test/snapshots/prefer-set-has.mjs.snap
Binary file not shown.

0 comments on commit fea5b42

Please sign in to comment.