Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/update-snapshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- run: npm run lint
- run: rm -rf test/snapshots
# Force update snapshots, https://github.com/avajs/ava/discussions/2754
- run: npx c8 ava --update-snapshots
- run: npx ava --update-snapshots
env:
AVA_FORCE_CI: not-ci
- uses: autofix-ci/action@v1
Expand Down
24 changes: 19 additions & 5 deletions rules/prefer-set-has.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,36 @@ const messages = {
[MESSAGE_ID_SUGGESTION]: 'Switch `{{name}}` to `Set`.',
};

const arrayMethodsReturnsArray = [
'concat',
/*
Some of these methods can be `Iterator`.

Since `Iterator` don't have an `includes()` method, we are safe to assume they are array. Except `concat` and `slice` which can be a string: https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2216
*/
const methodsReturnsArray = [
// `Array`
'copyWithin',
'fill',
'filter',
'flat',
'flatMap',
'map',
'reverse',
'slice',
'sort',
'splice',
'toReversed',
'toSorted',
'toSpliced',
'with',

// `Array` or `String` (unsafe)
'slice',
'concat',

// `String`
'split',

// `Iterator`
'toArray',
];

const isIncludesCall = node =>
Expand Down Expand Up @@ -95,9 +109,9 @@ const create = context => {
optionalCall: false,
optionalMember: false,
})
// Array methods that return an array
// Methods that return an array
|| isMethodCall(parent.init, {
methods: arrayMethodsReturnsArray,
methods: methodsReturnsArray,
optionalCall: false,
optionalMember: false,
})
Expand Down
2 changes: 2 additions & 0 deletions test/prefer-set-has.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const methodsReturnsArray = [
'toSorted',
'toSpliced',
'with',
'toArray',
'split',
];

test.snapshot({
Expand Down
58 changes: 57 additions & 1 deletion test/snapshots/prefer-set-has.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,63 @@ Generated by [AVA](https://avajs.dev).
4 | }␊
`

## invalid(41): const foo = _([1,2,3]); const bar = foo.map(value => value); function unicorn() { return bar.includes(1); }
## invalid(41): const foo = bar.toArray(); function unicorn() { return foo.includes(1); }

> Input

`␊
1 | const foo = bar.toArray();␊
2 | function unicorn() {␊
3 | return foo.includes(1);␊
4 | }␊
`

> Error 1/1

`␊
Message:␊
> 1 | const foo = bar.toArray();␊
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
2 | function unicorn() {␊
3 | return foo.includes(1);␊
4 | }␊
Output:␊
1 | const foo = new Set(bar.toArray());␊
2 | function unicorn() {␊
3 | return foo.has(1);␊
4 | }␊
`

## invalid(42): const foo = bar.split(); function unicorn() { return foo.includes(1); }

> Input

`␊
1 | const foo = bar.split();␊
2 | function unicorn() {␊
3 | return foo.includes(1);␊
4 | }␊
`

> Error 1/1

`␊
Message:␊
> 1 | const foo = bar.split();␊
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
2 | function unicorn() {␊
3 | return foo.includes(1);␊
4 | }␊
Output:␊
1 | const foo = new Set(bar.split());␊
2 | function unicorn() {␊
3 | return foo.has(1);␊
4 | }␊
`

## invalid(43): const foo = _([1,2,3]); const bar = foo.map(value => value); function unicorn() { return bar.includes(1); }

> Input

Expand Down
Binary file modified test/snapshots/prefer-set-has.js.snap
Binary file not shown.
Loading