prefer-at
UnreleasedConfiguration
Rule details
Prefer .at() to read an element or character from an array, typed array, or
string. Negative indices count from the end.
The rule checks length-based indices such as items[items.length - 1],
one-element reads from .slice(), and calls to last-element helpers such as
lodash.last(). It also checks charAt() and one-character substring() calls.
Examples of incorrect code for this rule
Examples of correct code for this rule
The rule ignores indexed writes, updates, method calls, and tagged templates
because these operations need the original property access. It also ignores
indexed reads on arguments and recognized DOM collections, such as
element.children and the result of document.querySelectorAll().
Fixes and suggestions
The rule can autofix length-based reads, one-element .slice() patterns, and
last-element helper calls. It offers editor suggestions for string methods so
you can check how the replacement handles out-of-range indices. For example,
''.charAt(0) returns an empty string and ''.at(0) returns undefined.
Options
checkAllIndexAccess
Defaults to false. Set it to true to check non-negative integer indices
that can be determined from the code, such as items[0], and additional
charAt() calls. The rule still allows charAt(-1) because it returns an
empty string while at(-1) reads from the end.
getLastElementFunctions
Defaults to []. Add names or dotted paths such as getLast or
utils.lastElement for helpers that return the last element of their argument.
The rule always recognizes _.last, lodash.last, and underscore.last.
Differences from upstream
Rslint withholds autofixes on obvious non-array values such as numbers and
plain objects. For example, an object literal can provide numeric keys and a
length while lacking an .at() method.
The rule reports the pattern and leaves the edit for you.
Rslint also ignores arguments in slice patterns and through TypeScript
assertions. A rest parameter gives you an array with an .at() method.