Skip to content
← Back to rules

unicorn/prefer-code-point Pedantic

🛠️ An auto-fix is available for this rule.

它是做什么的

更倾向于使用 String.prototype.codePointAt 而不是 String.prototype.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 中添加的。

参考资料