Skip to content

Commit

Permalink
prefer-negative-index: Check TypedArray#subarray() (#2237)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Dec 20, 2023
1 parent ec67cbe commit 6708a30
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
8 changes: 1 addition & 7 deletions docs/rules/prefer-negative-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ Prefer negative index over calculating from `.length` for:
- [`Array#toSpliced()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced)
- [`Array#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with)
- [`TypedArray#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/with)

<!--
TODO: Use MDN links when available
- [`Array#toSpliced()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced)
- [`Array#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with)
- [`TypedArray#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/with)
-->
- [`TypedArray#subarray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray)

## Fail

Expand Down
7 changes: 7 additions & 0 deletions rules/prefer-negative-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ const methods = new Map([
]),
},
],
[
'subarray',
{
argumentsIndexes: [0, 1],
supportObjects: new Set(typedArray),
},
],
[
'splice',
{
Expand Down
5 changes: 5 additions & 0 deletions test/prefer-negative-index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ test.snapshot({
'String.prototype.with.call(foo, foo.length - 1)',
// There is no `TypedArray#toSpliced`
'Uint8Array.prototype.toSpliced.call(foo, foo.length - 1)',
// There is no `Array#subarray`
'Array.prototype.subarray.call(foo, foo.length - 1)',
],
invalid: [
'/**/foo.slice(foo.length - 2, foo.length - 1)',
Expand All @@ -372,5 +374,8 @@ test.snapshot({
'[].toSpliced.call(foo, foo.length - 3, foo.length - 6)',
'foo.with(foo.length - 3, foo.length - 6)',
'Array.prototype.with.call(foo, foo.length - 3, foo.length - 6)',
'foo.subarray(foo.length - 3, foo.length - 6)',
'Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)',
'Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])',
],
});
63 changes: 63 additions & 0 deletions test/snapshots/prefer-negative-index.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,66 @@ Generated by [AVA](https://avajs.dev).
> 1 | Array.prototype.with.call(foo, foo.length - 3, foo.length - 6)␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`with\`.␊
`

## invalid(13): foo.subarray(foo.length - 3, foo.length - 6)

> Input
`␊
1 | foo.subarray(foo.length - 3, foo.length - 6)␊
`

> Output
`␊
1 | foo.subarray(- 3, - 6)␊
`

> Error 1/1
`␊
> 1 | foo.subarray(foo.length - 3, foo.length - 6)␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`subarray\`.␊
`

## invalid(14): Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)

> Input
`␊
1 | Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)␊
`

> Output
`␊
1 | Uint8Array.prototype.subarray.call(foo, - 3, - 6)␊
`

> Error 1/1
`␊
> 1 | Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`subarray\`.␊
`

## invalid(15): Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])

> Input
`␊
1 | Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])␊
`

> Output
`␊
1 | Uint8Array.prototype.subarray.apply(foo, [- 3, - 6])␊
`

> Error 1/1
`␊
> 1 | Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`subarray\`.␊
`
Binary file modified test/snapshots/prefer-negative-index.mjs.snap
Binary file not shown.

0 comments on commit 6708a30

Please sign in to comment.