{"record":{"id":"3300e2ee8d93f439","repo":"karatelabs/karate","slug":"unicode-escape-sequence-out-of-range-in-template-literal","errorCode":null,"errorMessage":"unicode escape sequence out of range in template literal","messagePattern":"unicode escape sequence out of range in template literal","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsLexer.java","lineNumber":419,"sourceCode":"                }\n                // Unicode escape backslash-u HHHH or backslash-u {H...} — require proper form.\n                if (esc == 'u') {\n                    char next = peek(1);\n                    if (next == '{') {\n                        // backslash-u { codepoint } — one or more hex digits, closed by }\n                        int j = 2;\n                        if (!isHexDigit(peek(j))) {\n                            throw new ParserException(\"invalid unicode escape sequence in template literal\");\n                        }\n                        int codepoint = 0;\n                        while (isHexDigit(peek(j))) {\n                            char h = peek(j);\n                            int v = (h >= '0' && h <= '9') ? h - '0'\n                                    : (h >= 'a' && h <= 'f') ? h - 'a' + 10\n                                    : h - 'A' + 10;\n                            codepoint = (codepoint << 4) | v;\n                            if (codepoint > 0x10FFFF) {\n                                throw new ParserException(\"unicode escape sequence out of range in template literal\");\n                            }\n                            j++;\n                        }\n                        if (peek(j) != '}') {\n                            throw new ParserException(\"invalid unicode escape sequence in template literal\");\n                        }\n                    } else {\n                        // backslash-u HHHH — four hex digits\n                        if (!isHexDigit(peek(1)) || !isHexDigit(peek(2))\n                                || !isHexDigit(peek(3)) || !isHexDigit(peek(4))) {\n                            throw new ParserException(\"invalid unicode escape sequence in template literal\");\n                        }\n                    }\n                }\n                advance();\n                continue;\n            }\n            advance();","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsLexer.java#L401-L437","documentation":"The \\u{...} codepoint escape must not exceed 0x10FFFF, the maximum Unicode codepoint. karate-js accumulates hex digits and throws as soon as the value overflows, per the ES spec early error for untagged templates.","triggerScenarios":"Template literal contains \\u{...} with a hex value greater than 10FFFF, e.g. `\\u{110000}`, `\\u{FFFFFF}`, or an interpolated value like `\\u{${bigNumber}}` that isn't a valid codepoint; thrown at JsLexer.java:419.","commonSituations":"Off-by-one when computing surrogate pairs (e.g. 0x10000 + 0x10FFFF), decimal values interpolated as hex, generated emoji/codepoint tables with bad bounds, porting code that used plain decimal escapes.","solutions":["Clamp or correct the codepoint to <= 0x10FFFF before embedding","Convert from decimal first: use `\\u{${cp.toString(16)}}` so the number is hex-encoded","Use String.fromCodePoint(cp) at runtime instead of an escape when the value is dynamic","If pairing surrogates, use the standard formula and validate 0 <= cp <= 0x10FFFF"],"exampleFix":"// before\nvar ch = `\\u{${cp}}`;       // cp = 1114112 (0x110000) -> out of range\n// after\nvar ch = String.fromCodePoint(cp); // throws a clearer RangeError if invalid\n// or: var esc = `\\u{${cp.toString(16)}}`;","handlingStrategy":"validation","validationCode":"function codepointInRange(hex) {\n  var cp = parseInt(hex, 16);\n  return Number.isFinite(cp) && cp >= 0 && cp <= 0x10FFFF;\n}\nif (!codepointInRange(cpHex)) throw new Error('codepoint exceeds 0x10FFFF: ' + cpHex);","typeGuard":"null","tryCatchPattern":"try {\n  karate.eval('var s = `\\u{' + cpHex + '}`;');\n} catch (e) {\n  if (String(e.message).indexOf('out of range') >= 0) {\n    var ch = String.fromCodePoint(0x10FFFF); // or clamp/substitute your fallback\n  } else throw e;\n}","preventionTips":["Clamp computed codepoints to 0..0x10FFFF before embedding","Encode numbers to hex (toString(16)) — never interpolate decimal values into \\u{}","Use String.fromCodePoint at runtime for dynamic codepoints","Verify surrogate-pair math: base 0x10000 plus offset <= 0xFFFFF"],"tags":["javascript","lexer","template-literal","unicode","escape-sequence"],"backgroundTag":"value-out-of-range","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}