Skip to main content
interlace
Plugin: conventionsRules

prefer-code-point

Prefer String.codePointAt() over String.charCodeAt(). This rule is part of eslint-plugin-conventions.

Prefer String.codePointAt() over String.charCodeAt(). This rule is part of eslint-plugin-conventions.

Quick Summary

AspectDetails
SeverityWarning (correctness)
Auto-Fix✅ Yes (converts method)
CategoryQuality
ESLint MCP✅ Optimized for ESLint MCP integration
Best ForUnicode handling, emoji support

Rule Details

charCodeAt() only works for Basic Multilingual Plane characters. codePointAt() handles all Unicode including emoji.

Why This Matters

IssueImpactSolution
😀 Emoji handlingIncorrect code pointscodePointAt()
🌍 Unicode supportAstral characters failFull Unicode support
📏 String lengthSurrogate pairs miscountedCorrect iteration

Examples

❌ Incorrect

const code = string.charCodeAt(0);  // Fails for emoji
'😀'.charCodeAt(0);  // Returns 55357 (wrong!)

✅ Correct

const code = string.codePointAt(0);  // Works for all Unicode
'😀'.codePointAt(0);  // Returns 128512 (correct!)

// Iterate over code points
for (const char of string) {
  const codePoint = char.codePointAt(0);
}

Configuration Examples

Basic Usage

{
  rules: {
    'conventions/prefer-code-point': 'warn'
  }
}

Further Reading

Known False Negatives

The following patterns are not detected due to static analysis limitations:

Dynamic Variable References

Why: Static analysis cannot trace values stored in variables or passed through function parameters.

// ❌ NOT DETECTED - Value from variable
const value = externalSource();
processValue(value); // Variable origin not tracked

Mitigation: Implement runtime validation and review code manually. Consider using TypeScript branded types for validated inputs.

Imported Values

Why: When values come from imports, the rule cannot analyze their origin or construction.

// ❌ NOT DETECTED - Value from import
import { getValue } from './helpers';
processValue(getValue()); // Cross-file not tracked

Mitigation: Ensure imported values follow the same constraints. Use TypeScript for type safety.

Use this rule

npm install -D oxlint-plugin-conventions
.oxlintrc.json
{  "jsPlugins": ["oxlint-plugin-conventions"],  "rules": {    "conventions/prefer-code-point": "error"  }}
vite.config.ts
import { defineConfig } from 'vite-plus';export default defineConfig({  lint: {    jsPlugins: ['oxlint-plugin-conventions'],    rules: {      'conventions/prefer-code-point': 'error',    },  },});

oxlint-plugin-conventions re-exports eslint-plugin-conventions/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.

Did this rule catch something? Star the repo to get new CWE coverage as we ship it — or follow the AI-code-security benchmarks behind these rules.