Skip to content

Commit ab8970e

Browse files
authored
prefer-set-has: Check Iterator#toArray() and String#split() (#2829)
1 parent aa67ca3 commit ab8970e

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

.github/workflows/update-snapshots.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- run: npm run lint
2020
- run: rm -rf test/snapshots
2121
# Force update snapshots, https://github.com/avajs/ava/discussions/2754
22-
- run: npx c8 ava --update-snapshots
22+
- run: npx ava --update-snapshots
2323
env:
2424
AVA_FORCE_CI: not-ci
2525
- uses: autofix-ci/action@v1

rules/prefer-set-has.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@ const messages = {
99
[MESSAGE_ID_SUGGESTION]: 'Switch `{{name}}` to `Set`.',
1010
};
1111

12-
const arrayMethodsReturnsArray = [
13-
'concat',
12+
/*
13+
Some of these methods can be `Iterator`.
14+
15+
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
16+
*/
17+
const methodsReturnsArray = [
18+
// `Array`
1419
'copyWithin',
1520
'fill',
1621
'filter',
1722
'flat',
1823
'flatMap',
1924
'map',
2025
'reverse',
21-
'slice',
2226
'sort',
2327
'splice',
2428
'toReversed',
2529
'toSorted',
2630
'toSpliced',
2731
'with',
32+
33+
// `Array` or `String` (unsafe)
34+
'slice',
35+
'concat',
36+
37+
// `String`
38+
'split',
39+
40+
// `Iterator`
41+
'toArray',
2842
];
2943

3044
const isIncludesCall = node =>
@@ -95,9 +109,9 @@ const create = context => {
95109
optionalCall: false,
96110
optionalMember: false,
97111
})
98-
// Array methods that return an array
112+
// Methods that return an array
99113
|| isMethodCall(parent.init, {
100-
methods: arrayMethodsReturnsArray,
114+
methods: methodsReturnsArray,
101115
optionalCall: false,
102116
optionalMember: false,
103117
})

test/prefer-set-has.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const methodsReturnsArray = [
1919
'toSorted',
2020
'toSpliced',
2121
'with',
22+
'toArray',
23+
'split',
2224
];
2325

2426
test.snapshot({

test/snapshots/prefer-set-has.js.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,63 @@ Generated by [AVA](https://avajs.dev).
13461346
4 | }␊
13471347
`
13481348

1349-
## invalid(41): const foo = _([1,2,3]); const bar = foo.map(value => value); function unicorn() { return bar.includes(1); }
1349+
## invalid(41): const foo = bar.toArray(); function unicorn() { return foo.includes(1); }
1350+
1351+
> Input
1352+
1353+
`␊
1354+
1 | const foo = bar.toArray();␊
1355+
2 | function unicorn() {␊
1356+
3 | return foo.includes(1);␊
1357+
4 | }␊
1358+
`
1359+
1360+
> Error 1/1
1361+
1362+
`␊
1363+
Message:␊
1364+
> 1 | const foo = bar.toArray();␊
1365+
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
1366+
2 | function unicorn() {␊
1367+
3 | return foo.includes(1);␊
1368+
4 | }␊
1369+
1370+
Output:␊
1371+
1 | const foo = new Set(bar.toArray());␊
1372+
2 | function unicorn() {␊
1373+
3 | return foo.has(1);␊
1374+
4 | }␊
1375+
`
1376+
1377+
## invalid(42): const foo = bar.split(); function unicorn() { return foo.includes(1); }
1378+
1379+
> Input
1380+
1381+
`␊
1382+
1 | const foo = bar.split();␊
1383+
2 | function unicorn() {␊
1384+
3 | return foo.includes(1);␊
1385+
4 | }␊
1386+
`
1387+
1388+
> Error 1/1
1389+
1390+
`␊
1391+
Message:␊
1392+
> 1 | const foo = bar.split();␊
1393+
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
1394+
2 | function unicorn() {␊
1395+
3 | return foo.includes(1);␊
1396+
4 | }␊
1397+
1398+
Output:␊
1399+
1 | const foo = new Set(bar.split());␊
1400+
2 | function unicorn() {␊
1401+
3 | return foo.has(1);␊
1402+
4 | }␊
1403+
`
1404+
1405+
## invalid(43): const foo = _([1,2,3]); const bar = foo.map(value => value); function unicorn() { return bar.includes(1); }
13501406

13511407
> Input
13521408
54 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)