eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Fix(prefer-at): remove unsafe autofix for `.slice` with 1 argument
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'},
]