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

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

k-yle
Copy link
Contributor

@k-yle k-yle commented Oct 11, 2024

It's not safe to change array.slice(-n)[0] to array.at(-n). This code is not equivalent for arrays with only 1 item. It's only equivalent for .slice(-1)

Proof:

testCases = [ 'a', 'ab', 'abc', 'abcd' ];
testCases.map(array => ({
    original: array.slice(-2)[0],
    autofixed: array.at(-2),
}));

// output:
[ 
  {original: 'a', autofixed: undefined}, // 🔴 this is different
  {original: 'a', autofixed: 'a'},
  {original: 'b', autofixed: 'b'},
  {original: 'c', autofixed: 'c'},
]

@github-actions github-actions bot changed the title fix(prefer-at): remove unsafe autofix for .slice with 1 argument Fix(prefer-at): remove unsafe autofix for .slice with 1 argument Oct 11, 2024
@fregante fregante changed the title Fix(prefer-at): remove unsafe autofix for .slice with 1 argument fix(prefer-at): remove unsafe autofix for .slice with 1 argument Oct 23, 2024
@github-actions github-actions bot changed the title fix(prefer-at): remove unsafe autofix for .slice with 1 argument Fixprefer-at: remove unsafe autofix for .slice with 1 argument Oct 23, 2024
@fregante fregante changed the title Fixprefer-at: remove unsafe autofix for .slice with 1 argument prefer-at: remove unsafe autofix for .slice with 1 argument Oct 23, 2024
@sindresorhus
Copy link
Owner

Bump :)

@fisker
Copy link
Collaborator

fisker commented Jan 21, 2025

@k-yle Can you fix conflicts?

@k-yle
Copy link
Contributor Author

k-yle commented Jan 21, 2025

@fisker done

@sindresorhus sindresorhus changed the title prefer-at: remove unsafe autofix for .slice with 1 argument prefer-at: Remove unsafe autofix for .slice with 1 argument Jan 24, 2025
@sindresorhus sindresorhus merged commit 2985ecc into sindresorhus:main Jan 24, 2025
17 checks passed
@k-yle k-yle deleted the kh/slice-bug branch January 29, 2025 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants