Skip to content

Commit

Permalink
fix(prefer-at): remove unsafe autofix for .slice with 1 argument
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Oct 11, 2024
1 parent 2b469be commit fad6911
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 80 deletions.
2 changes: 1 addition & 1 deletion docs/rules/prefer-at.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const foo = array.slice(-1).pop();
```

```js
const foo = array.slice(-5).shift();
const foo = array.slice(-1).shift();
```

```js
Expand Down
9 changes: 6 additions & 3 deletions rules/prefer-at.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ function checkSliceCall(node) {
const startIndex = -startIndexNode.argument.value;
if (sliceArgumentsLength === 1) {
if (
firstElementGetMethod === 'zero-index'
|| firstElementGetMethod === 'shift'
|| (startIndex === -1 && firstElementGetMethod === 'pop')
startIndexNode.argument.value === 1 &&

Check failure on line 99 in rules/prefer-at.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

'&&' should be placed at the beginning of the line.
(
firstElementGetMethod === 'zero-index'
|| firstElementGetMethod === 'shift'
|| (startIndex === -1 && firstElementGetMethod === 'pop')
)
) {
return {safeToFix: true, firstElementGetMethod};
}
Expand Down
7 changes: 4 additions & 3 deletions test/prefer-at.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ test.snapshot({
'++ array.slice(-1)[0]',
'array.slice(-1)[0] --',
'delete array.slice(-1)[0]',

'array.slice(-9)[0]',
'array.slice(-9).shift()',
'array.slice(-0xA)[0b000]',
],
invalid: [
'array.slice(-1)[0]',
'array.slice(-1).pop()',
'array.slice(-1.0).shift()',
'array.slice(-9)[0]',
'array.slice(-0xA)[0b000]',
'array.slice(-9).shift()',
'array.slice(-1)[(( 0 ))];',
'array.slice(-(( 1 )))[0];',
'array.slice((( -1 )))[0];',
Expand Down
83 changes: 10 additions & 73 deletions test/snapshots/prefer-at.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,70 +652,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
`

## invalid(4): array.slice(-9)[0]

> Input
`␊
1 | array.slice(-9)[0]␊
`

> Output
`␊
1 | array.at(-9)␊
`

> Error 1/1
`␊
> 1 | array.slice(-9)[0]␊
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
`

## invalid(5): array.slice(-0xA)[0b000]

> Input
`␊
1 | array.slice(-0xA)[0b000]␊
`

> Output
`␊
1 | array.at(-0xA)␊
`

> Error 1/1
`␊
> 1 | array.slice(-0xA)[0b000]␊
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
`

## invalid(6): array.slice(-9).shift()

> Input
`␊
1 | array.slice(-9).shift()␊
`

> Output
`␊
1 | array.at(-9)␊
`

> Error 1/1
`␊
> 1 | array.slice(-9).shift()␊
| ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊
`

## invalid(7): array.slice(-1)[(( 0 ))];
## invalid(4): array.slice(-1)[(( 0 ))];

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

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

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

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

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

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

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

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

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

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

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

## invalid(13): (( array.slice(-1) )).pop();
## invalid(10): (( array.slice(-1) )).pop();

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

## invalid(14): (( array.slice(-1).pop ))();
## invalid(11): (( array.slice(-1).pop ))();

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

## invalid(15): (( array.slice(-1).pop() ));
## invalid(12): (( array.slice(-1).pop() ));

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

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

> Input
Expand Down
Binary file modified test/snapshots/prefer-at.mjs.snap
Binary file not shown.

0 comments on commit fad6911

Please sign in to comment.