toon-format/toon · error · SyntaxError

Invalid escape sequence: truncated \u escape at "${value.sli

Error message

Invalid escape sequence: truncated \u escape at "${value.slice(i, i + 6)}"

What it means

A \u escape requires exactly the characters \u plus 4 following characters. When fewer than 4 characters remain in the string after \u, the escape is truncated and unescapeString throws this SyntaxError, including the partial text for diagnosis.

Source

Thrown at packages/toon/src/shared/string-utils.ts:86

      }
      if (next === 'r') {
        unescaped += CARRIAGE_RETURN
        i += 2
        continue
      }
      if (next === BACKSLASH) {
        unescaped += BACKSLASH
        i += 2
        continue
      }
      if (next === DOUBLE_QUOTE) {
        unescaped += DOUBLE_QUOTE
        i += 2
        continue
      }
      if (next === 'u') {
        if (i + 6 > value.length) {
          throw new SyntaxError(`Invalid escape sequence: truncated \\u escape at "${value.slice(i, i + 6)}"`)
        }
        const hex = value.slice(i + 2, i + 6)
        if (!/^[0-9a-f]{4}$/i.test(hex)) {
          throw new SyntaxError(`Invalid escape sequence: \\u must be followed by 4 hex digits, got "${hex}"`)
        }
        const codeUnit = Number.parseInt(hex, 16)
        if (codeUnit >= 0xD800 && codeUnit <= 0xDFFF) {
          throw new SyntaxError(`Invalid escape sequence: \\u${hex} is a lone surrogate. Supplementary code points MUST appear as literal UTF-8`)
        }
        unescaped += String.fromCodePoint(codeUnit)
        i += 6
        continue
      }

      throw new SyntaxError(`Invalid escape sequence: \\${next}`)
    }

    unescaped += value[i]

View on GitHub (pinned to 604eac266e)

Solutions

  1. Complete the escape with all 4 hex digits (e.g. \u0041)
  2. Use the literal UTF-8 character instead of the \u escape
  3. Verify the input file is not truncated at the point of the escape

Example fix

// before (TOON input)
label: "A\u0"
// after
label: "A\u0041"  // or label: "AA" with the literal character
Defensive patterns

Strategy: validation

Validate before calling

function hasCompleteUnicodeEscapes(s: string): boolean {
  return !/\\u(?![0-9a-fA-F]{4})/.test(s)
}

Try / catch

try {
  const value = decode(input)
} catch (e) {
  if (e instanceof SyntaxError && e.message.includes('truncated \\u escape')) {
    // log the reported slice, fix input, or re-request the source
  }
  throw e
}

Prevention

When it happens

Trigger: Parsing a quoted TOON string whose escape sequence ends the string prematurely, e.g. "\\u0" or "\\u12" — i + 6 exceeds value.length.

Common situations: Files truncated by copy-paste limits or line-based tools; manual authoring of unicode escapes without completing them; programmatic string slicing that cut mid-escape.

Related errors


AI-assisted analysis of toon-format/toon@604eac266e (2026-08-31). Data as JSON: /api/errors/d604465de317578e. Report an issue: GitHub.