Skip to content

Commit fea5b42

Browse files
authoredFeb 2, 2023
prefer-set-has: Support Array#{toReversed,toSorted,toSpliced,with} (#2032)
1 parent 00883a8 commit fea5b42

File tree

4 files changed

+125
-23
lines changed

4 files changed

+125
-23
lines changed
 

‎rules/prefer-set-has.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@ const arrayStaticMethodSelector = methodCallSelector({
3030
path: 'init',
3131
});
3232

33-
// `array.concat()`
34-
// `array.copyWithin()`
35-
// `array.fill()`
36-
// `array.filter()`
37-
// `array.flat()`
38-
// `array.flatMap()`
39-
// `array.map()`
40-
// `array.reverse()`
41-
// `array.slice()`
42-
// `array.sort()`
43-
// `array.splice()`
33+
// Array methods that return an array
4434
const arrayMethodSelector = methodCallSelector({
4535
methods: [
4636
'concat',
@@ -54,6 +44,10 @@ const arrayMethodSelector = methodCallSelector({
5444
'slice',
5545
'sort',
5646
'splice',
47+
'toReversed',
48+
'toSorted',
49+
'toSpliced',
50+
'with',
5751
],
5852
path: 'init',
5953
});

‎test/prefer-set-has.mjs

+20-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const methodsReturnsArray = [
1515
'slice',
1616
'sort',
1717
'splice',
18+
'toReversed',
19+
'toSorted',
20+
'toSpliced',
21+
'with',
1822
];
1923

2024
test.snapshot({
@@ -341,19 +345,23 @@ test.snapshot({
341345
}
342346
`),
343347
// Not MemberExpression
344-
...methodsReturnsArray.map(method => outdent`
345-
const foo = ${method}();
346-
function unicorn() {
347-
return foo.includes(1);
348-
}
349-
`),
348+
...methodsReturnsArray
349+
.filter(method => method !== 'with')
350+
.map(method => outdent`
351+
const foo = ${method}();
352+
function unicorn() {
353+
return foo.includes(1);
354+
}
355+
`),
350356
// Computed
351-
...methodsReturnsArray.map(method => outdent`
352-
const foo = bar[${method}]();
353-
function unicorn() {
354-
return foo.includes(1);
355-
}
356-
`),
357+
...methodsReturnsArray
358+
.filter(method => method !== 'with')
359+
.map(method => outdent`
360+
const foo = bar[${method}]();
361+
function unicorn() {
362+
return foo.includes(1);
363+
}
364+
`),
357365
// Not `Identifier`
358366
...methodsReturnsArray.map(method => outdent`
359367
const foo = bar["${method}"]();

‎test/snapshots/prefer-set-has.mjs.md

+100
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,106 @@ Generated by [AVA](https://avajs.dev).
10671067
`
10681068

10691069
## Invalid #37
1070+
1 | const foo = bar.toReversed();
1071+
2 | function unicorn() {
1072+
3 | return foo.includes(1);
1073+
4 | }
1074+
1075+
> Output
1076+
1077+
`␊
1078+
1 | const foo = new Set(bar.toReversed());␊
1079+
2 | function unicorn() {␊
1080+
3 | return foo.has(1);␊
1081+
4 | }␊
1082+
`
1083+
1084+
> Error 1/1
1085+
1086+
`␊
1087+
> 1 | const foo = bar.toReversed();␊
1088+
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
1089+
2 | function unicorn() {␊
1090+
3 | return foo.includes(1);␊
1091+
4 | }␊
1092+
`
1093+
1094+
## Invalid #38
1095+
1 | const foo = bar.toSorted();
1096+
2 | function unicorn() {
1097+
3 | return foo.includes(1);
1098+
4 | }
1099+
1100+
> Output
1101+
1102+
`␊
1103+
1 | const foo = new Set(bar.toSorted());␊
1104+
2 | function unicorn() {␊
1105+
3 | return foo.has(1);␊
1106+
4 | }␊
1107+
`
1108+
1109+
> Error 1/1
1110+
1111+
`␊
1112+
> 1 | const foo = bar.toSorted();␊
1113+
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
1114+
2 | function unicorn() {␊
1115+
3 | return foo.includes(1);␊
1116+
4 | }␊
1117+
`
1118+
1119+
## Invalid #39
1120+
1 | const foo = bar.toSpliced();
1121+
2 | function unicorn() {
1122+
3 | return foo.includes(1);
1123+
4 | }
1124+
1125+
> Output
1126+
1127+
`␊
1128+
1 | const foo = new Set(bar.toSpliced());␊
1129+
2 | function unicorn() {␊
1130+
3 | return foo.has(1);␊
1131+
4 | }␊
1132+
`
1133+
1134+
> Error 1/1
1135+
1136+
`␊
1137+
> 1 | const foo = bar.toSpliced();␊
1138+
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
1139+
2 | function unicorn() {␊
1140+
3 | return foo.includes(1);␊
1141+
4 | }␊
1142+
`
1143+
1144+
## Invalid #40
1145+
1 | const foo = bar.with();
1146+
2 | function unicorn() {
1147+
3 | return foo.includes(1);
1148+
4 | }
1149+
1150+
> Output
1151+
1152+
`␊
1153+
1 | const foo = new Set(bar.with());␊
1154+
2 | function unicorn() {␊
1155+
3 | return foo.has(1);␊
1156+
4 | }␊
1157+
`
1158+
1159+
> Error 1/1
1160+
1161+
`␊
1162+
> 1 | const foo = bar.with();␊
1163+
| ^^^ \`foo\` should be a \`Set\`, and use \`foo.has()\` to check existence or non-existence.␊
1164+
2 | function unicorn() {␊
1165+
3 | return foo.includes(1);␊
1166+
4 | }␊
1167+
`
1168+
1169+
## Invalid #41
10701170
1 | const foo = _([1,2,3]);
10711171
2 | const bar = foo.map(value => value);
10721172
3 | function unicorn() {
182 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.