oxc-project/oxc · info · OxcDiagnostic
Use Unicode escapes instead of hexadecimal escapes.
Error message
Use Unicode escapes instead of hexadecimal escapes.
What it means
Diagnostic from the oxlint rule `unicorn/no-hex-escape`. It enforces Unicode escapes (`\u0041`, `\u{1B}`) over hexadecimal escapes (`\x41`) in string literals, template literals, and regular expressions. Both forms produce identical strings, but the `\u` forms are self-consistent and generalize to all code points, while `\xNN` only covers Latin-1. `String.raw` tagged templates are exempt because they produce literal backslash sequences, not escapes.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_hex_escape.rs:15
use oxc_ast::{AstKind, ast::StringLiteral};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::{
ast::{Character, CharacterKind},
visit::Visit,
};
use oxc_span::Span;
use crate::{
AstNode, context::LintContext, rule::Rule, utils::is_string_raw_tagged_template_expression,
};
fn no_hex_escape_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Use Unicode escapes instead of hexadecimal escapes.").with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoHexEscape;
declare_oxc_lint!(
/// ### What it does
///
/// Enforces a convention of using [Unicode escapes](https://mathiasbynens.be/notes/javascript-escapes#unicode)
/// instead of [hexadecimal escapes](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) for
/// consistency and clarity.
///
/// ### Why is this bad?
///
/// Using hexadecimal escapes can be less readable and harder to understand
/// when compared to Unicode escapes.
///
/// ### ExamplesView on GitHub (pinned to e1e7af627c)
Solutions
- Convert to Unicode escapes: `\x1B` becomes `\u{1B}` or `\u001B`; `\x41` becomes `\u0041`
- When you need literal backslash-x text, use String.raw`\x1B`, which the rule ignores
- In regexes, prefer the u flag with `\u{...}` or Unicode property escapes
Example fix
// before
const ESC = '\x1B';
// after
const ESC = '\u{1B}'; Defensive patterns
Strategy: validation
Prevention
- Use \u escapes everywhere; \xNN is Latin-1-only and inconsistent with astral escapes
- Use String.raw when literal backslash sequences are needed
- Settle on one escape style per file; this rule exists to stop style drift
When it happens
Trigger: Any `\xNN` escape in a string literal (`'\x1B[31m'`), a template literal, or a RegExp character class (`/[\x00-\x1f]/`) — except inside String.raw tagged templates.
Common situations: Terminal ANSI escape codes, binary protocol literals, regex character classes over control characters; mixed escape styles after merging code from several authors or eras.
Related errors
- Use uppercase characters for the value of the escape sequenc
- Use a regular expression literal instead of the `RegExp` con
- Use the 'v' flag.
- Use the 'u' flag.
- Passing `length` as the end argument of a `slice` call is un
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/dc14eb2a4ff76b6a.
Report an issue: GitHub.