{"record":{"id":"2f657403025676dd","repo":"oxc-project/oxc","slug":"unexpected-surrogate-pair-in-character-class","errorCode":null,"errorMessage":"Unexpected surrogate pair in character class.","messagePattern":"Unexpected surrogate pair in character class\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_misleading_character_class.rs","lineNumber":30,"sourceCode":"    visit::{RegExpAstKind, Visit},\n};\nuse oxc_span::{GetSpan, Span};\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::{DefaultRuleConfig, Rule},\n    utils::{\n        RegexFlagsParseResult, get_regex_flags_span, get_regex_pattern_span, is_regexp_callee,\n        run_on_regex_node,\n    },\n};\n\nfn surrogate_pair_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected surrogate pair in character class.\")\n        .with_help(\"Use Unicode code point escapes (e.g., \\\\u{1F44D}) instead of surrogate pairs.\")\n        .with_label(span)\n}\n\nfn surrogate_pair_without_flag_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected surrogate pair in character class.\")\n        .with_help(\"Add the Unicode flag 'u'.\")\n        .with_label(span)\n}\n\nfn combining_class_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected combining class in character class.\")\n        .with_help(\"Replace the character with its normalized form (NFC) or use Unicode code point escapes instead of combining sequences.\")\n        .with_label(span)\n}\n\nfn emoji_modifiers_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected emoji modifier in character class.\")","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_misleading_character_class.rs#L12-L48","documentation":"Diagnostic from oxlint's eslint/no-misleading-character-class rule (crates/oxc_linter/src/rules/eslint/no_misleading_character_class.rs:30). It reports a surrogate pair written as two \\uD800-\\uDFFF escapes inside a character class of a unicode-mode regex (/u or /v): in unicode mode each escape is a code unit, so /[\\uD83D\\uDC4D]/u matches the two lone surrogates, not the emoji. The help tells you to write the code point directly with \\u{...}.","triggerScenarios":"/[\\uD83D\\uDC4D]/u, new RegExp('[\\\\u{1F44D}]|\\\\uD83D\\\\uDC4D', 'u'), or character classes in v-mode regexes containing paired surrogate escapes; both literal and dynamically built patterns reaching the RegExp constructor are checked.","commonSituations":"Porting pre-ES2015 emoji regexes to unicode mode by just adding the u flag; escaping non-BMP characters with tools that emit UTF-16 code-unit escapes; filtering emoji from usernames/chat while missing astral characters.","solutions":["Replace each surrogate pair with a code point escape: /[\\u{1F44D}]/u","Or match the literal character directly: /👍/u","For dynamic patterns, build from code points (String.fromCodePoint) rather than concatenated surrogate escapes"],"exampleFix":"// before\nconst emoji = /[\\uD83D\\uDC4D]/u; // matches two lone surrogates, not 👍\n\n// after\nconst emoji = /[\\u{1F44D}]/u; // code point escape for 👍","handlingStrategy":"validation","validationCode":"// Reject surrogate escapes in unicode-mode patterns before they ship\nfunction assertNoSurrogateEscapes(pattern, flags) {\n  if ((flags.includes('u') || flags.includes('v')) && /\\\\uD[89A-F][\\\\dA-F]{2}/i.test(pattern)) {\n    throw new Error('surrogate escapes in a /u regex character class; use \\\\u{...} code point escapes');\n  }\n}","typeGuard":"const usesCodePointEscapes = (pattern) => /\\\\u\\{[0-9a-f]+\\}/i.test(pattern);","tryCatchPattern":null,"preventionTips":["Always write astral characters in unicode-mode regexes as \\u{...} or literal characters","Add unit tests matching astral inputs (emoji, CJK ext-B) to every unicode regex","Run no-misleading-character-class in CI to catch surrogate escapes introduced by refactors"],"tags":["lint","regex","unicode","surrogate-pairs","emoji","javascript"],"backgroundTag":"regex-unicode-escapes","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}