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-set-has: Support Array#{toReversed,toSorted,toSpliced,with} #2032

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions rules/prefer-set-has.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.