Skip to content

Commit 2985ecc

Browse files
authoredJan 24, 2025··
prefer-at: Remove unsafe autofix for .slice with 1 argument (#2476)
1 parent 92b5671 commit 2985ecc

File tree

5 files changed

+21
-80
lines changed

5 files changed

+21
-80
lines changed
 

‎docs/rules/prefer-at.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const foo = array.slice(-1).pop();
2828
```
2929

3030
```js
31-
const foo = array.slice(-5).shift();
31+
const foo = array.slice(-1).shift();
3232
```
3333

3434
```js

‎rules/prefer-at.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ function checkSliceCall(node) {
9999
const startIndex = -startIndexNode.argument.value;
100100
if (sliceArgumentsLength === 1) {
101101
if (
102-
firstElementGetMethod === 'zero-index'
103-
|| firstElementGetMethod === 'shift'
104-
|| (startIndex === -1 && firstElementGetMethod === 'pop')
102+
startIndexNode.argument.value === 1
103+
&& (
104+
firstElementGetMethod === 'zero-index'
105+
|| firstElementGetMethod === 'shift'
106+
|| (startIndex === -1 && firstElementGetMethod === 'pop')
107+
)
105108
) {
106109
return {safeToFix: true, firstElementGetMethod};
107110
}

‎test/prefer-at.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ test.snapshot({
9393
'++ array.slice(-1)[0]',
9494
'array.slice(-1)[0] --',
9595
'delete array.slice(-1)[0]',
96+
97+
'array.slice(-9)[0]',
98+
'array.slice(-9).shift()',
99+
'array.slice(-0xA)[0b000]',
96100
],
97101
invalid: [
98102
'array.slice(-1)[0]',
99103
'array.slice(-1).pop()',
100104
'array.slice(-1.0).shift()',
101-
'array.slice(-9)[0]',
102-
'array.slice(-0xA)[0b000]',
103-
'array.slice(-9).shift()',
104105
'array.slice(-1)[(( 0 ))];',
105106
'array.slice(-(( 1 )))[0];',
106107
'array.slice((( -1 )))[0];',

‎test/snapshots/prefer-at.js.md

+10-73
Original file line numberDiff line numberDiff line change
@@ -652,70 +652,7 @@ Generated by [AVA](https://avajs.dev).
652652
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
653653
`
654654

655-
## invalid(4): array.slice(-9)[0]
656-
657-
> Input
658-
659-
`␊
660-
1 | array.slice(-9)[0]␊
661-
`
662-
663-
> Output
664-
665-
`␊
666-
1 | array.at(-9)␊
667-
`
668-
669-
> Error 1/1
670-
671-
`␊
672-
> 1 | array.slice(-9)[0]␊
673-
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
674-
`
675-
676-
## invalid(5): array.slice(-0xA)[0b000]
677-
678-
> Input
679-
680-
`␊
681-
1 | array.slice(-0xA)[0b000]␊
682-
`
683-
684-
> Output
685-
686-
`␊
687-
1 | array.at(-0xA)␊
688-
`
689-
690-
> Error 1/1
691-
692-
`␊
693-
> 1 | array.slice(-0xA)[0b000]␊
694-
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
695-
`
696-
697-
## invalid(6): array.slice(-9).shift()
698-
699-
> Input
700-
701-
`␊
702-
1 | array.slice(-9).shift()␊
703-
`
704-
705-
> Output
706-
707-
`␊
708-
1 | array.at(-9)␊
709-
`
710-
711-
> Error 1/1
712-
713-
`␊
714-
> 1 | array.slice(-9).shift()␊
715-
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
716-
`
717-
718-
## invalid(7): array.slice(-1)[(( 0 ))];
655+
## invalid(4): array.slice(-1)[(( 0 ))];
719656

720657
> Input
721658
@@ -736,7 +673,7 @@ Generated by [AVA](https://avajs.dev).
736673
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
737674
`
738675

739-
## invalid(8): array.slice(-(( 1 )))[0];
676+
## invalid(5): array.slice(-(( 1 )))[0];
740677

741678
> Input
742679
@@ -757,7 +694,7 @@ Generated by [AVA](https://avajs.dev).
757694
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
758695
`
759696

760-
## invalid(9): array.slice((( -1 )))[0];
697+
## invalid(6): array.slice((( -1 )))[0];
761698

762699
> Input
763700
@@ -778,7 +715,7 @@ Generated by [AVA](https://avajs.dev).
778715
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
779716
`
780717

781-
## invalid(10): (( array.slice(-1) ))[0];
718+
## invalid(7): (( array.slice(-1) ))[0];
782719

783720
> Input
784721
@@ -799,7 +736,7 @@ Generated by [AVA](https://avajs.dev).
799736
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
800737
`
801738

802-
## invalid(11): (( array )).slice(-1)[0];
739+
## invalid(8): (( array )).slice(-1)[0];
803740

804741
> Input
805742
@@ -820,7 +757,7 @@ Generated by [AVA](https://avajs.dev).
820757
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
821758
`
822759

823-
## invalid(12): (( array.slice(-1)[0] ));
760+
## invalid(9): (( array.slice(-1)[0] ));
824761

825762
> Input
826763
@@ -841,7 +778,7 @@ Generated by [AVA](https://avajs.dev).
841778
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
842779
`
843780

844-
## invalid(13): (( array.slice(-1) )).pop();
781+
## invalid(10): (( array.slice(-1) )).pop();
845782

846783
> Input
847784
@@ -862,7 +799,7 @@ Generated by [AVA](https://avajs.dev).
862799
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
863800
`
864801

865-
## invalid(14): (( array.slice(-1).pop ))();
802+
## invalid(11): (( array.slice(-1).pop ))();
866803

867804
> Input
868805
@@ -883,7 +820,7 @@ Generated by [AVA](https://avajs.dev).
883820
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
884821
`
885822

886-
## invalid(15): (( array.slice(-1).pop() ));
823+
## invalid(12): (( array.slice(-1).pop() ));
887824

888825
> Input
889826
@@ -904,7 +841,7 @@ Generated by [AVA](https://avajs.dev).
904841
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
905842
`
906843

907-
## invalid(16): array.slice(-1)[0].pop().shift().slice(-1)
844+
## invalid(13): array.slice(-1)[0].pop().shift().slice(-1)
908845

909846
> Input
910847

‎test/snapshots/prefer-at.js.snap

-83 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.