From dd95bffd53d736cd5d3d92da47a3269b0fffd5b4 Mon Sep 17 00:00:00 2001 From: Kyle Hensel Date: Fri, 11 Oct 2024 18:52:49 +1100 Subject: [PATCH] fix(prefer-at): remove unsafe autofix for `.slice` with 1 argument --- docs/rules/prefer-at.md | 2 +- rules/prefer-at.js | 9 ++-- test/prefer-at.mjs | 7 +-- test/snapshots/prefer-at.mjs.md | 83 ++++-------------------------- test/snapshots/prefer-at.mjs.snap | Bin 3575 -> 3501 bytes 5 files changed, 21 insertions(+), 80 deletions(-) diff --git a/docs/rules/prefer-at.md b/docs/rules/prefer-at.md index 50079b3777..4c29157e50 100644 --- a/docs/rules/prefer-at.md +++ b/docs/rules/prefer-at.md @@ -28,7 +28,7 @@ const foo = array.slice(-1).pop(); ``` ```js -const foo = array.slice(-5).shift(); +const foo = array.slice(-1).shift(); ``` ```js diff --git a/rules/prefer-at.js b/rules/prefer-at.js index 9986a8e10d..37970fd3c3 100644 --- a/rules/prefer-at.js +++ b/rules/prefer-at.js @@ -96,9 +96,12 @@ function checkSliceCall(node) { const startIndex = -startIndexNode.argument.value; if (sliceArgumentsLength === 1) { if ( - firstElementGetMethod === 'zero-index' - || firstElementGetMethod === 'shift' - || (startIndex === -1 && firstElementGetMethod === 'pop') + startIndexNode.argument.value === 1 + && ( + firstElementGetMethod === 'zero-index' + || firstElementGetMethod === 'shift' + || (startIndex === -1 && firstElementGetMethod === 'pop') + ) ) { return {safeToFix: true, firstElementGetMethod}; } diff --git a/test/prefer-at.mjs b/test/prefer-at.mjs index 6ced5f4e63..1d9761aef3 100644 --- a/test/prefer-at.mjs +++ b/test/prefer-at.mjs @@ -93,14 +93,15 @@ test.snapshot({ '++ array.slice(-1)[0]', 'array.slice(-1)[0] --', 'delete array.slice(-1)[0]', + + 'array.slice(-9)[0]', + 'array.slice(-9).shift()', + 'array.slice(-0xA)[0b000]', ], invalid: [ 'array.slice(-1)[0]', 'array.slice(-1).pop()', 'array.slice(-1.0).shift()', - 'array.slice(-9)[0]', - 'array.slice(-0xA)[0b000]', - 'array.slice(-9).shift()', 'array.slice(-1)[(( 0 ))];', 'array.slice(-(( 1 )))[0];', 'array.slice((( -1 )))[0];', diff --git a/test/snapshots/prefer-at.mjs.md b/test/snapshots/prefer-at.mjs.md index 0af95522dd..18bcd5bbb1 100644 --- a/test/snapshots/prefer-at.mjs.md +++ b/test/snapshots/prefer-at.mjs.md @@ -652,70 +652,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(4): array.slice(-9)[0] - -> Input - - `␊ - 1 | array.slice(-9)[0]␊ - ` - -> Output - - `␊ - 1 | array.at(-9)␊ - ` - -> Error 1/1 - - `␊ - > 1 | array.slice(-9)[0]␊ - | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ - ` - -## invalid(5): array.slice(-0xA)[0b000] - -> Input - - `␊ - 1 | array.slice(-0xA)[0b000]␊ - ` - -> Output - - `␊ - 1 | array.at(-0xA)␊ - ` - -> Error 1/1 - - `␊ - > 1 | array.slice(-0xA)[0b000]␊ - | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ - ` - -## invalid(6): array.slice(-9).shift() - -> Input - - `␊ - 1 | array.slice(-9).shift()␊ - ` - -> Output - - `␊ - 1 | array.at(-9)␊ - ` - -> Error 1/1 - - `␊ - > 1 | array.slice(-9).shift()␊ - | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ - ` - -## invalid(7): array.slice(-1)[(( 0 ))]; +## invalid(4): array.slice(-1)[(( 0 ))]; > Input @@ -736,7 +673,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(8): array.slice(-(( 1 )))[0]; +## invalid(5): array.slice(-(( 1 )))[0]; > Input @@ -757,7 +694,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(9): array.slice((( -1 )))[0]; +## invalid(6): array.slice((( -1 )))[0]; > Input @@ -778,7 +715,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(10): (( array.slice(-1) ))[0]; +## invalid(7): (( array.slice(-1) ))[0]; > Input @@ -799,7 +736,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(11): (( array )).slice(-1)[0]; +## invalid(8): (( array )).slice(-1)[0]; > Input @@ -820,7 +757,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(12): (( array.slice(-1)[0] )); +## invalid(9): (( array.slice(-1)[0] )); > Input @@ -841,7 +778,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(13): (( array.slice(-1) )).pop(); +## invalid(10): (( array.slice(-1) )).pop(); > Input @@ -862,7 +799,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(14): (( array.slice(-1).pop ))(); +## invalid(11): (( array.slice(-1).pop ))(); > Input @@ -883,7 +820,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(15): (( array.slice(-1).pop() )); +## invalid(12): (( array.slice(-1).pop() )); > Input @@ -904,7 +841,7 @@ Generated by [AVA](https://avajs.dev). | ^^^^^ Prefer \`.at(…)\` over the first element from \`.slice(…)\`.␊ ` -## invalid(16): array.slice(-1)[0].pop().shift().slice(-1) +## invalid(13): array.slice(-1)[0].pop().shift().slice(-1) > Input diff --git a/test/snapshots/prefer-at.mjs.snap b/test/snapshots/prefer-at.mjs.snap index 5e61e6cada6fa14b6a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