Skip to content

useAtIndex

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useAtIndex": "error"
}
}
}
}

¥Description

使用 at() 代替整数索引访问。

¥Use at() instead of integer index access.

访问数组或字符串末尾的元素很不方便,因为需要从要访问元素的从 1 开始的后向索引中减去数组或字符串的长度。例如,要访问数组或字符串的最后一个元素,你需要编写 array[array.length - 1]。实现相同目标的更便捷方法是使用带有负索引的 at() 方法。要访问数组或字符串的最后一个元素,只需输入 array.at(-1)

¥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).

此规则强制要求在 at() 更方便的情况下,优先使用 at() 而不是索引访问、charAt()slice()[0]

¥This rule enforces the usage of at() over index access, charAt(), and slice()[0] when at() is more convenient.

¥Examples

¥Invalid

const foo = array[array.length - 1];
code-block.js:1:13 lint/style/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

const foo = array[array.length - 5];
code-block.js:1:13 lint/style/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

const foo = array.slice(-1)[0];
code-block.js:1:13 lint/style/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

const foo = array.slice(-1).pop();
code-block.js:1:13 lint/style/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

const foo = array.slice(-5).shift();
code-block.js:1:13 lint/style/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

const foo = string.charAt(string.length - 5);
code-block.js:1:13 lint/style/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

¥Valid

const foo = array.at(-1);
const foo = array.at(-5);
const foo = array[100];
const foo = array.at(array.length - 1);
array[array.length - 1] = foo;

¥Related links