toon-format/toon · error · SyntaxError

Invalid escape sequence: \${next}

Error message

Invalid escape sequence: \${next}

What it means

unescapeString only permits the escapes \n, \t, \", \\ and \uXXXX. Any other character following a backslash is an unknown escape and throws this SyntaxError naming the offending character.

Source

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

      }
      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]
    i++
  }

  return unescaped
}

/** Finds the index of the closing double quote, accounting for escape sequences. */
export function findClosingQuote(content: string, start: number): number {
  let i = start + 1
  while (i < content.length) {
    if (content[i] === BACKSLASH && i + 1 < content.length) {
      i += 2
      continue
    }
    if (content[i] === DOUBLE_QUOTE) {

View on GitHub (pinned to 604eac266e)

Solutions

  1. Replace the unsupported escape with the literal character (e.g. \' → ')
  2. Use only the supported escapes: \n \t \" \\ and \uXXXX
  3. Re-encode the string with the library's encoder to produce valid escaping

Example fix

// before (TOON input)
greeting: "It\'s fine"
// after
greeting: "It's fine"
Defensive patterns

Strategy: validation

Validate before calling

function hasOnlySupportedEscapes(s: string): boolean {
  return !/\\(?![nt"\\u])/.test(s.replace(/\\u[0-9a-fA-F]{4}/g, ''))
}

Try / catch

try {
  const value = decode(input)
} catch (e) {
  if (e instanceof SyntaxError && e.message.startsWith('Invalid escape sequence: \\')) {
    // strip/replace the unsupported escape reported in the message
  }
  throw e
}

Prevention

When it happens

Trigger: Parsing a quoted TOON string with escapes like \x41, \0, \' or a raw \ followed by any character other than n, t, ", \ or u.

Common situations: Authors copying escape conventions from JavaScript/regex strings (\x, \0, \/) into TOON files; single-quoted strings converted to double-quoted TOON strings leaving \' escapes behind.

Related errors


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