Skip to content
← Back to rules

unicorn/prefer-at Pedantic

⚠️ 🛠️ A dangerous auto-fix is available for this rule.

作用

优先使用用于索引访问的 Array#at()String#at() 方法。

此规则也不鼓励使用 String#charAt()

为什么这不好?

.at() 方法在按索引访问元素时更易读,也更一致, 尤其是对于负索引,它们会从数组或字符串末尾开始访问元素。

示例

以下是此规则的错误代码示例:

js
const foo = array[array.length - 1];
const foo = array.slice(-1)[0];
const foo = string.charAt(string.length - 1);

以下是此规则的正确代码示例:

js
const foo = array.at(-1);
const foo = array.at(-5);
const foo = string.at(-1);

配置

此规则接受一个包含以下属性的配置对象:

checkAllIndexAccess

type: boolean

default: false

检查所有索引访问,而不仅仅是像 array.length - 1 这样的特殊模式。 启用后,array[0]array[1] 等也会被标记。

getLastElementFunctions

type: string[]

default: []

将函数名称列表视为“获取最后一个元素”函数。 这些函数将检查是否使用了 .at(-1)

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-at": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-at": "error",
  },
});
bash
oxlint --deny unicorn/prefer-at

版本

此规则在 v1.20.0 中添加。

参考资料