Skip to content
← Back to rules

oxc/bad-char-at-comparison 正确性

This rule is turned on by default.
An auto-fix is available for this rule.

它的作用

此规则会在通过 charAtat 或括号表示法访问的字符与长度大于 1 的字符串进行比较时发出警告。

为什么这不好?

字符访问返回的字符串长度最多为 1。如果将返回值与长度大于 1 的字符串进行比较,比较结果将始终为 false。

示例

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

javascript
a.charAt(4) === "a2";
"abc".at(4) === "a2";
"abc"[4] === "a2";
a.charAt(4) === "/n";

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

javascript
a.charAt(4) === "a";
a.charAt(4) === "\n";

如何使用

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

json
{
  "rules": {
    "oxc/bad-char-at-comparison": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "oxc/bad-char-at-comparison": "error",
  },
});
bash
oxlint --deny oxc/bad-char-at-comparison

版本

此规则于 v0.0.22 中添加。

参考资料