unicorn/prefer-negative-index
Require a negative index instead of .length - index
fixable
slice, at, splice, toSpliced and subarray count a negative index from the end, so items.slice(items.length - 2) can be items.slice(-2).
The rule only looks at the index arguments. That's the first two for slice and subarray, and the first for the others.
--fix removes the .length part.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/prefer-negative-index" = "error" # or "warn"Examples#
example.js4 problems
const lastTwo = items.slice(items.length - 2)Prefer negative index over .length - index when possible.;
const last = items.at(items.length - 1)Prefer negative index over .length - index when possible.;
queue.splice(queue.length - 1, 1)Prefer negative index over .length - index when possible.;
const rest = Array.prototype.slice.call(list, list.length - 2)Prefer negative index over .length - index when possible.;example.jsNo problems
const lastTwo = items.slice(-2);
const last = items.at(-1);
queue.splice(-1, 1);
const rest = Array.prototype.slice.call(list, -2);The length must come from the same object, minus a number literal. Bracket access isn't reported, because items[-1] is undefined:
example.jsNo problems
const rest = items.slice(other.length - 2);
const tail = items.slice(items.length - count); // slice(-count) is the whole array when count is 0
const last = items[items.length - 1];