useAtIndex
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/nursery/useAtIndex
Since: v1.9.4
Sources:
- Inspired from:
unicorn/prefer-at
Use at()
instead of integer index access.
Accessing an element at the end of an array or a string is inconvenient because you have to subtract the length of the array or the string from the backward 1-based index of the element to access.
For example, to access the last element of an array or a string, you would have to write array[array.length - 1]
.
A more convenient way to achieve the same thing is to use the at()
method with a negative index.
To access the last element of an array or a string just write array.at(-1)
.
This rule enforces the usage of at()
over index access, charAt()
, and slice()[0]
when at()
is more convenient.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:13 lint/nursery/useAtIndex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer X.at(-Y) over X[X.length - Y].
> 1 │ const foo = array[array.length - 1];
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using .at() is more convenient and is easier to read.
ℹ Unsafe fix: Use .at().
1 │ - const·foo·=·array[array.length·-·1];
1 │ + const·foo·=·array.at(-1);
2 2 │
code-block.js:1:13 lint/nursery/useAtIndex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer X.at(-Y) over X[X.length - Y].
> 1 │ const foo = array[array.length - 5];
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using .at() is more convenient and is easier to read.
ℹ Unsafe fix: Use .at().
1 │ - const·foo·=·array[array.length·-·5];
1 │ + const·foo·=·array.at(-5);
2 2 │
code-block.js:1:13 lint/nursery/useAtIndex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer X.at(Y) over X.slice(Y)[0].
> 1 │ const foo = array.slice(-1)[0];
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Using .at() is more convenient and is easier to read.
ℹ Unsafe fix: Use .at().
1 │ - const·foo·=·array.slice(-1)[0];
1 │ + const·foo·=·array.at(-1);
2 2 │
code-block.js:1:13 lint/nursery/useAtIndex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer X.at(-1) over X.slice(-a).pop().
> 1 │ const foo = array.slice(-1).pop();
│ ^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using .at() is more convenient and is easier to read.
ℹ Unsafe fix: Use .at().
1 │ - const·foo·=·array.slice(-1).pop();
1 │ + const·foo·=·array.at(-1);
2 2 │
code-block.js:1:13 lint/nursery/useAtIndex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer X.at(Y) over X.slice(Y).shift().
> 1 │ const foo = array.slice(-5).shift();
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using .at() is more convenient and is easier to read.
ℹ Unsafe fix: Use .at().
1 │ - const·foo·=·array.slice(-5).shift();
1 │ + const·foo·=·array.at(-5);
2 2 │
code-block.js:1:13 lint/nursery/useAtIndex FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Prefer X.at(-Y) over X.charAt(X.length - Y).
> 1 │ const foo = string.charAt(string.length - 5);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Using .at() is more convenient and is easier to read.
ℹ Unsafe fix: Use .at().
1 │ - const·foo·=·string.charAt(string.length·-·5);
1 │ + const·foo·=·string.at(-5);
2 2 │