Skip to content
← Back to rules

unicorn/prefer-code-point 书呆子风格

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

它是做什么的 ​

优先使用 String#codePointAt 而不是 String#charCodeAt。 优先使用 String.fromCodePoint 而不是 String.fromCharCode。

为什么这不好? ​

Unicode 在 String#codePointAt() 和 String.fromCodePoint() 中支持得更好。

String.fromCodePoint() 和 String.fromCharCode() 的区别

示例 ​

此规则的错误代码示例:

javascript
"🦄".charCodeAt(0);
String.fromCharCode(0x1f984);

此规则的正确代码示例:

javascript
"🦄".codePointAt(0);
String.fromCodePoint(0x1f984);

如何使用 ​

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

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

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

版本 ​

此规则是在 v0.0.16 中添加的。

参考资料 ​