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 5e61e6cada..7ac9b7366c 100644 Binary files a/test/snapshots/prefer-at.mjs.snap and b/test/snapshots/prefer-at.mjs.snap differ