unicorn/prefer-code-point 严格
它做了什么
优先使用 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);如何使用
通过配置文件或 CLI 启用此规则,可以使用:
json
{
"rules": {
"unicorn/prefer-code-point": "error"
}
}bash
oxlint --deny unicorn/prefer-code-point