Skip to content

Latest commit

Β 

History

History
70 lines (51 loc) Β· 2.38 KB

File metadata and controls

70 lines (51 loc) Β· 2.38 KB

prefer-negative-index

πŸ“ Prefer negative index over .length - index when possible.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

Prefer negative index over calculating from .length for:

Examples

// ❌
foo.slice(foo.length - 2, foo.length - 1);

// βœ…
foo.slice(-2, -1);
// ❌
foo.splice(foo.length - 1, 1);

// βœ…
foo.splice(-1, 1);
// ❌
foo.at(foo.length - 1);

// βœ…
foo.at(-1);
// ❌
Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);

// βœ…
Array.prototype.slice.call(foo, -2, -1);
// ❌
Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);

// βœ…
Array.prototype.slice.apply(foo, [-2, -1]);

Related rules