fix: reject lone surrogates in stringify instead of emitting invalid TOML - #60
fix: reject lone surrogates in stringify instead of emitting invalid TOML#60mahirhir wants to merge 1 commit into
Conversation
|
If the library doesn't have to report the location and codepoint of the lone surrogate, it would be more performant to use String.prototype.isWellFormed() instead of a regex. |
|
Agreed in principle, and the reasoning holds even harder than you put it: this throws a flat The one thing stopping me swapping it is the support range. So the options are to leave the regex until 18 is dropped, or write |
stringifybuilds basic strings withJSON.stringify, which escapes a lone surrogate (an unpaired\uD800-\uDFFFcode unit) as a\uXXXXsequence. TOML only allows\u/\Uescapes for valid Unicode scalar values, and the parser rejects surrogate code points (src/primitive.ts):So
stringifycan emit a document that its ownparsecannot read:A lone surrogate has no representation in a TOML document, so this is the same category as the values
stringifyalready refuses (invalid dates, symbols, functions,null/undefinedin arrays). This makesformatStringthrow aTypeErrorfor lone surrogates rather than producing output that round-trips into a parse error. The guard lives informatString, so it covers both string values and quoted keys. Valid surrogate pairs such as𝄞are scalar values and are still serialized.Added a test next to the existing
rejects ...cases.pnpm testis green.