Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-at: remove unsafe autofix for .slice with 1 argument #2476

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 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
&& (
firstElementGetMethod === 'zero-index'
|| firstElementGetMethod === 'shift'
|| (startIndex === -1 && firstElementGetMethod === 'pop')
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect, the value of startIndexNode doesn't matter. It only result differently when the index is out of the boundary of string. And sting don't have .shift/.pop, so it doesn't matter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker we're checking startIndexNode because .slice(-1)[0] can still be safely converted to .at(-1), but only for -1, not any other number. For any other number the behaviour is different if the index is out of bounds

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right.

But i think we should still report array.slice(-9)[0], just remove the autofix.

) {
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.
Loading