Skip to content

Commit 6708a30

Browse files
authoredDec 20, 2023
prefer-negative-index: Check TypedArray#subarray() (#2237)
1 parent ec67cbe commit 6708a30

5 files changed

+76
-7
lines changed
 

‎docs/rules/prefer-negative-index.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ Prefer negative index over calculating from `.length` for:
1919
- [`Array#toSpliced()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced)
2020
- [`Array#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with)
2121
- [`TypedArray#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/with)
22-
23-
<!--
24-
TODO: Use MDN links when available
25-
- [`Array#toSpliced()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced)
26-
- [`Array#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with)
27-
- [`TypedArray#with()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/with)
28-
-->
22+
- [`TypedArray#subarray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray)
2923

3024
## Fail
3125

‎rules/prefer-negative-index.js

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const methods = new Map([
2727
]),
2828
},
2929
],
30+
[
31+
'subarray',
32+
{
33+
argumentsIndexes: [0, 1],
34+
supportObjects: new Set(typedArray),
35+
},
36+
],
3037
[
3138
'splice',
3239
{

‎test/prefer-negative-index.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ test.snapshot({
350350
'String.prototype.with.call(foo, foo.length - 1)',
351351
// There is no `TypedArray#toSpliced`
352352
'Uint8Array.prototype.toSpliced.call(foo, foo.length - 1)',
353+
// There is no `Array#subarray`
354+
'Array.prototype.subarray.call(foo, foo.length - 1)',
353355
],
354356
invalid: [
355357
'/**/foo.slice(foo.length - 2, foo.length - 1)',
@@ -372,5 +374,8 @@ test.snapshot({
372374
'[].toSpliced.call(foo, foo.length - 3, foo.length - 6)',
373375
'foo.with(foo.length - 3, foo.length - 6)',
374376
'Array.prototype.with.call(foo, foo.length - 3, foo.length - 6)',
377+
'foo.subarray(foo.length - 3, foo.length - 6)',
378+
'Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)',
379+
'Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])',
375380
],
376381
});

‎test/snapshots/prefer-negative-index.mjs.md

+63
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,66 @@ Generated by [AVA](https://avajs.dev).
279279
> 1 | Array.prototype.with.call(foo, foo.length - 3, foo.length - 6)␊
280280
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`with\`.␊
281281
`
282+
283+
## invalid(13): foo.subarray(foo.length - 3, foo.length - 6)
284+
285+
> Input
286+
287+
`␊
288+
1 | foo.subarray(foo.length - 3, foo.length - 6)␊
289+
`
290+
291+
> Output
292+
293+
`␊
294+
1 | foo.subarray(- 3, - 6)␊
295+
`
296+
297+
> Error 1/1
298+
299+
`␊
300+
> 1 | foo.subarray(foo.length - 3, foo.length - 6)␊
301+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`subarray\`.␊
302+
`
303+
304+
## invalid(14): Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)
305+
306+
> Input
307+
308+
`␊
309+
1 | Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)␊
310+
`
311+
312+
> Output
313+
314+
`␊
315+
1 | Uint8Array.prototype.subarray.call(foo, - 3, - 6)␊
316+
`
317+
318+
> Error 1/1
319+
320+
`␊
321+
> 1 | Uint8Array.prototype.subarray.call(foo, foo.length - 3, foo.length - 6)␊
322+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`subarray\`.␊
323+
`
324+
325+
## invalid(15): Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])
326+
327+
> Input
328+
329+
`␊
330+
1 | Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])␊
331+
`
332+
333+
> Output
334+
335+
`␊
336+
1 | Uint8Array.prototype.subarray.apply(foo, [- 3, - 6])␊
337+
`
338+
339+
> Error 1/1
340+
341+
`␊
342+
> 1 | Uint8Array.prototype.subarray.apply(foo, [foo.length - 3, foo.length - 6])␊
343+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`subarray\`.␊
344+
`
157 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.