unicorn/prefer-at Pedantic
What it does
Prefer .at()
method for index access and String#charAt()
.
Why is this bad?
The .at()
method is more readable and consistent for accessing elements by index, especially for negative indices which access elements from the end.
Examples
Examples of incorrect code for this rule:
js
const foo = array[array.length - 1];
const foo = array.slice(-1)[0];
const foo = string.charAt(string.length - 1);
Examples of correct code for this rule:
js
const foo = array.at(-1);
const foo = array.at(-5);
const foo = string.at(-1);
How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny unicorn/prefer-at
json
{
"rules": {
"unicorn/prefer-at": "error"
}
}