Skip to content

Commit 6443ee3

Browse files
authored
prefer-negative-index: Support Array#at() (sindresorhus#1217)
1 parent 6a745a0 commit 6443ee3

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

docs/rules/prefer-negative-index.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1-
# Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()` and `Array#splice()`
1+
# Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()`, `Array#splice()` and `Array#at()`
22

3-
Prefer negative index over calculating from `.length` for [`String#slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice), [`Array#slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice), [`TypedArray#slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice) and [`Array#splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)
3+
Prefer negative index over calculating from `.length` for [`String#slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice), [`Array#slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice), [`TypedArray#slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice) , [`Array#splice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) and [`Array#at()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at)
44

55
This rule is fixable.
66

77
## Fail
88

99
```js
1010
foo.slice(foo.length - 2, foo.length - 1);
11+
```
12+
13+
```js
1114
foo.splice(foo.length - 1, 1);
15+
```
16+
17+
```js
18+
foo.at(foo.length - 1);
19+
```
20+
21+
```js
1222
Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);
23+
```
24+
25+
```js
1326
Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);
1427
```
1528

1629
## Pass
1730

1831
```js
1932
foo.slice(-2, -1);
33+
```
34+
35+
```js
2036
foo.splice(-1, 1);
37+
```
38+
39+
```js
40+
foo.at(-1);
41+
```
42+
43+
```js
2144
Array.prototype.slice.call(foo, -2, -1);
45+
```
46+
47+
```js
2248
Array.prototype.slice.apply(foo, [-2, -1]);
2349
```

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Each rule has emojis denoting:
187187
| [prefer-math-trunc](docs/rules/prefer-math-trunc.md) | Enforce the use of `Math.trunc` instead of bitwise operators. || 🔧 |
188188
| [prefer-modern-dom-apis](docs/rules/prefer-modern-dom-apis.md) | Prefer `.before()` over `.insertBefore()`, `.replaceWith()` over `.replaceChild()`, prefer one of `.before()`, `.after()`, `.append()` or `.prepend()` over `insertAdjacentText()` and `insertAdjacentElement()`. || 🔧 |
189189
| [prefer-module](docs/rules/prefer-module.md) | Prefer JavaScript modules (ESM) over CommonJS. || 🔧 |
190-
| [prefer-negative-index](docs/rules/prefer-negative-index.md) | Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()` and `Array#splice()`. || 🔧 |
190+
| [prefer-negative-index](docs/rules/prefer-negative-index.md) | Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()`, `Array#splice()` and `Array#at()`. || 🔧 |
191191
| [prefer-node-protocol](docs/rules/prefer-node-protocol.md) | Prefer using the `node:` protocol when importing Node.js builtin modules. || 🔧 |
192192
| [prefer-number-properties](docs/rules/prefer-number-properties.md) | Prefer `Number` static properties over global ones. || 🔧 |
193193
| [prefer-optional-catch-binding](docs/rules/prefer-optional-catch-binding.md) | Prefer omitting the `catch` binding parameter. || 🔧 |

rules/prefer-negative-index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ const methods = new Map([
4242
'Array'
4343
])
4444
}
45+
],
46+
[
47+
'at',
48+
{
49+
argumentsIndexes: [0],
50+
supportObjects: new Set([
51+
'Array'
52+
])
53+
}
4554
]
4655
]);
4756

@@ -255,7 +264,7 @@ module.exports = {
255264
meta: {
256265
type: 'suggestion',
257266
docs: {
258-
description: 'Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()` and `Array#splice()`.',
267+
description: 'Prefer negative index over `.length - index` for `{String,Array,TypedArray}#slice()`, `Array#splice()` and `Array#at()`.',
259268
url: getDocumentationUrl(__filename)
260269
},
261270
fixable: 'code',

test/prefer-negative-index.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ test.snapshot({
355355
// Foo[1] and foo["1"]
356356
'foo[1].slice(foo["1"].length - 1)',
357357
// Foo['bar'] & foo["bar"]
358-
'foo[\'bar\'].slice(foo["bar"].length - 1)'
358+
'foo[\'bar\'].slice(foo["bar"].length - 1)',
359+
outdent`
360+
foo.at(foo.length - 1);
361+
Array.prototype.at.call(foo, foo.length - 2);
362+
Array.prototype.at.apply(foo, [foo.length - 3]);
363+
`
359364
]
360365
});

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,43 @@ Generated by [AVA](https://avajs.dev).
9999
> 1 | foo['bar'].slice(foo["bar"].length - 1)␊
100100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`slice\`.␊
101101
`
102+
103+
## Invalid #7
104+
1 | foo.at(foo.length - 1);
105+
2 | Array.prototype.at.call(foo, foo.length - 2);
106+
3 | Array.prototype.at.apply(foo, [foo.length - 3]);
107+
108+
> Output
109+
110+
`␊
111+
1 | foo.at(- 1);␊
112+
2 | Array.prototype.at.call(foo, - 2);␊
113+
3 | Array.prototype.at.apply(foo, [- 3]);␊
114+
`
115+
116+
> Error 1/3
117+
118+
`␊
119+
> 1 | foo.at(foo.length - 1);␊
120+
| ^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`at\`.␊
121+
2 | Array.prototype.at.call(foo, foo.length - 2);␊
122+
3 | Array.prototype.at.apply(foo, [foo.length - 3]);␊
123+
`
124+
125+
> Error 2/3
126+
127+
`␊
128+
1 | foo.at(foo.length - 1);␊
129+
> 2 | Array.prototype.at.call(foo, foo.length - 2);␊
130+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`at\`.␊
131+
3 | Array.prototype.at.apply(foo, [foo.length - 3]);␊
132+
`
133+
134+
> Error 3/3
135+
136+
`␊
137+
1 | foo.at(foo.length - 1);␊
138+
2 | Array.prototype.at.call(foo, foo.length - 2);␊
139+
> 3 | Array.prototype.at.apply(foo, [foo.length - 3]);␊
140+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negative index over length minus index for \`at\`.␊
141+
`
167 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)