Skip to content

Latest commit

 

History

History
58 lines (41 loc) · 1.63 KB

prefer-negative-index.md

File metadata and controls

58 lines (41 loc) · 1.63 KB

Prefer negative index over .length - index for {String,Array,TypedArray}#slice(), Array#splice() and Array#at()

This rule is part of the recommended config.

🔧 This rule is auto-fixable.

Prefer negative index over calculating from .length for String#slice(), Array#slice, TypedArray#slice , Array#splice() and Array#at()

Fail

foo.slice(foo.length - 2, foo.length - 1);
foo.splice(foo.length - 1, 1);
foo.at(foo.length - 1);
Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);
Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);

Pass

foo.slice(-2, -1);
foo.splice(-1, 1);
foo.at(-1);
Array.prototype.slice.call(foo, -2, -1);
Array.prototype.slice.apply(foo, [-2, -1]);

Related rules