{"record":{"id":"502d0deb393462c1","repo":"karatelabs/karate","slug":"invalid-unicode-escape-sequence-in-template-literal","errorCode":null,"errorMessage":"invalid unicode escape sequence in template literal","messagePattern":"invalid unicode escape sequence in template literal","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsLexer.java","lineNumber":409,"sourceCode":"                    throw new ParserException(\"invalid escape sequence in template literal: \\\\\" + esc);\n                }\n                if (esc == '0' && peek(1) >= '0' && peek(1) <= '9') {\n                    throw new ParserException(\"invalid escape sequence in template literal: \\\\0\" + peek(1));\n                }\n                // Hex escape backslash-x HH — require exactly two hex digits.\n                if (esc == 'x') {\n                    if (!isHexDigit(peek(1)) || !isHexDigit(peek(2))) {\n                        throw new ParserException(\"invalid hex escape sequence in template literal\");\n                    }\n                }\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","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsLexer.java#L391-L427","documentation":"A \\u escape in a template literal must be either \\uHHHH (four hex digits) or \\u{H+} (braced codepoint with at least one hex digit). \\u{ with no hex digit before the closing brace is invalid, so the lexer throws. The ES spec classifies this as an early error in untagged templates.","triggerScenarios":"Template literal contains \\u{ or \\u followed by fewer than four non-hex characters, e.g. `\\u{}`, `\\u{z}`, `\\u12`, `\\uabc` — hit at JsLexer.java:409 in scanTemplateContent.","commonSituations":"Interpolated codepoints like `\\u{${cp}}` where cp failed to substitute or is empty, truncated four-digit escapes from generated code, regex \\u patterns pasted into template strings, typo'd unicode escapes.","solutions":["Provide a valid codepoint: `\\u{1F600}` or `\\u{41}` for the braced form","Use exactly four hex digits: `\\u0041` instead of `\\u41`","Escape the backslash if the \\u is literal text: `\\\\u{}`","If building dynamically, validate the hex string before interpolation and fall back to String.fromCodePoint(cp)"],"exampleFix":"// before\nvar ch = `\\u{${cp}}`; // cp undefined/empty -> `\\u{}`\n// after\nvar ch = (cp && /^[0-9a-fA-F]+$/.test(cp)) ? `\\u{${cp}}` : String.fromCodePoint(parseInt(cp || '0', 16) || 63);","handlingStrategy":"validation","validationCode":"function badUnicodeEscape(tpl) {\n  return /\\\\u(?![0-9a-fA-F]{4}|\\{[0-9a-fA-F]+\\})/.test(tpl);\n}\nif (badUnicodeEscape(tpl)) throw new Error('malformed \\\\u escape in template literal');","typeGuard":"null","tryCatchPattern":"try {\n  karate.eval('var s = `' + tpl + '`;');\n} catch (e) {\n  if (String(e.message).indexOf('invalid unicode escape') >= 0) {\n    tpl = tpl.replace(/\\\\u(?![0-9a-fA-F]{4|\\{)/g, '\\\\\\\\u');\n  } else throw e;\n}","preventionTips":["Zero-pad \\u escapes to exactly four hex digits","Use the braced \\u{H+} form for variable-length codepoints","Validate interpolated codepoint strings are non-empty hex before embedding","Use String.fromCodePoint for dynamic characters instead of escapes"],"tags":["javascript","lexer","template-literal","unicode","escape-sequence"],"backgroundTag":"invalid-escape-sequence","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"}