From 6708a30f763bcc9bb0b4d5053be4c14d13cd7bf0 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 20 Dec 2023 19:51:45 +0800 Subject: [PATCH] `prefer-negative-index`: Check `TypedArray#subarray()` (#2237) --- docs/rules/prefer-negative-index.md | 8 +-- rules/prefer-negative-index.js | 7 ++ test/prefer-negative-index.mjs | 5 ++ test/snapshots/prefer-negative-index.mjs.md | 63 ++++++++++++++++++ test/snapshots/prefer-negative-index.mjs.snap | Bin 941 -> 1098 bytes 5 files changed, 76 insertions(+), 7 deletions(-) diff --git a/docs/rules/prefer-negative-index.md b/docs/rules/prefer-negative-index.md index 9b2eed8225..89c4e33dd0 100644 --- a/docs/rules/prefer-negative-index.md +++ b/docs/rules/prefer-negative-index.md @@ -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) - - +- [`TypedArray#subarray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray) ## Fail diff --git a/rules/prefer-negative-index.js b/rules/prefer-negative-index.js index d6dfb09a65..09255cbe47 100644 --- a/rules/prefer-negative-index.js +++ b/rules/prefer-negative-index.js @@ -27,6 +27,13 @@ const methods = new Map([ ]), }, ], + [ + 'subarray', + { + argumentsIndexes: [0, 1], + supportObjects: new Set(typedArray), + }, + ], [ 'splice', { diff --git a/test/prefer-negative-index.mjs b/test/prefer-negative-index.mjs index 0f57e33892..c2dac1fb65 100644 --- a/test/prefer-negative-index.mjs +++ b/test/prefer-negative-index.mjs @@ -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)', @@ -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])', ], }); diff --git a/test/snapshots/prefer-negative-index.mjs.md b/test/snapshots/prefer-negative-index.mjs.md index 4d46a5da0e..8defb7bf68 100644 --- a/test/snapshots/prefer-negative-index.mjs.md +++ b/test/snapshots/prefer-negative-index.mjs.md @@ -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\`.␊ + ` diff --git a/test/snapshots/prefer-negative-index.mjs.snap b/test/snapshots/prefer-negative-index.mjs.snap index 67b45d5490e4260db30a923a98f1f0510ea73f0e..7eb3bcab85d03a007bb22176d85c8527bda40db1 100644 GIT binary patch literal 1098 zcmV-Q1hxA?RzVL_cKM;EJsGzksfIdP&B9i`H@ zYq|wv64JN(fSnXCq6nTGdf8CiT|w~f!TW9=M7((rFM>x&o1}T#H|hJP>DI$oNAiE4 z_y7ApPd>I?DXIF>gX)VNnJ7e+*C!Qi6{|{lP!zH#H8hl}=z6lMDogU9^_wbd%VY%& zqjUm+@1meoHEdK@bW(l1Q!ZnI@68;J9gA6?zN4|@eVI5yBHH8=jms)7$*Mf788u?d z4g=XURN1NH&no)5Xa0bHK9aq%sNkwqx~jSc`t8~~D#t%sz9&1Qu1{UvPn7lIol3ye$H z#<6J8Fbo2xYA8jW*6 z@RXmRFOc^D$SV&Jri$2^^m;yJY%C(ESnLFZKA$=Y(NGKksS`9QUktAShG$+3!F4bK z=xIlc+13w~usIN2M=;3obp#L$(-ABc9e77Pa8~%36 zPcBr&9F5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