For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/prefer-code-point.md.
close

prefer-code-point

Unreleased

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-code-point': 'error',
    },
  },
]);

Rule Details

String#codePointAt() and String.fromCodePoint() handle the full Unicode range, including characters outside the Basic Multilingual Plane that JavaScript represents with surrogate pairs. String#charCodeAt() and String.fromCharCode() only operate on individual UTF-16 code units, so they truncate emoji and other high code points.

Examples of incorrect code for this rule:

const unicorn = '🦄'.charCodeAt(0).toString(16);
const first = String.fromCharCode(0x1f984);

Examples of correct code for this rule:

const unicorn = '🦄'.codePointAt(0).toString(16);
const first = String.fromCodePoint(0x1f984);

Suggestions

The rule offers a suggestion that renames charCodeAt to codePointAt only when the result is not consumed as a number. In string hashing or digit parsing such as string.charCodeAt(index) - 48, swapping in codePointAt would change the computed value: a code point outside the BMP occupies two code units, and iterating by string.length would still visit the trailing surrogate separately. The diagnostic is still reported there, but the fix is left for manual review.

// Iterate by code point when computing a code-point hash.
let hash = 0;
for (const character of string) {
	hash = (((hash * 31) | 0) + character.codePointAt(0)) | 0;
}

String.fromCharCode is always reported with a suggestion because the call shape does not change; callers that intentionally build strings from UTF-16 code units can disable the rule for that line.

Original Documentation