yaml-compat
yaml-compat.ts — a partial compatibility shim for selected yaml v2 calls
(github.com/eemeli/yaml — also this repo’s own correctness reference /
oracle, see bench/oracle.ts), backed by lightning-yaml’s own parser
(./core.ts).
This module doc block is the MASTER SOURCE for yaml compatibility: it is
published verbatim to the website’s API reference (site/astro.config.mjs
wires this file through starlight-typedoc), so keep it accurate and up to date.
Compatibility level TODAY
Section titled “Compatibility level TODAY”Partial API compatibility, not a drop-in replacement. This module
exports parse, parseAllDocuments, parseDocument, stringify, the
standalone LineCounter, and the complete upstream Lexer and Parser
token APIs, the source-preserving CST helper namespace, plus the
CompatDocument and Reviver types and a convenience default object.
CST supports token inspection and editing, without Composer, AST nodes, or
source-aware Document integration.
It does not mirror the full yaml export surface or every call signature;
Composer, AST nodes, public error classes, and the top-level AST visitor are
not provided. The visitor inside CST walks source collection items.
LineCounter
maps caller-supplied UTF-16 offsets to line and
column positions; it does not scan source text or populate the rejected
lineCounter parse option. Options are honoured on a growing allowlist, and
anything not yet honoured throws rather than silently diverging. Today
only the parse reviver runs, plus schema / version accepted as the
1.2-core defaults ("core" / "1.2"), and any
boolean flag left at the value lightning-yaml already produces (usually
false, e.g. mapAsMap: false) — e.g. parse(text, { mapAsMap: true }) or
stringify(value, { indent: 4 }) throws until that option’s sub-task lands. A
relied-upon option fails loud instead
of producing silently-wrong output (walking .contents as an AST — see the
Document note below — remains a documented gap).
Improve compatibility for these entry points without ever compromising the two things that outrank it: YAML-1.2-spec correctness and core (./core.ts) speed. Per-option cost is paid either in this shim (pre-/post-processing the plain-JS value, as the reviver already does — proof a hook here costs the core nothing) or behind a gated core seam that leaves the options-free fast path byte-identical. An option we can’t yet honour fails loud (throws) rather than being silently ignored; each option sub-task moves its key onto the honoured allowlist.
Option support matrix
Section titled “Option support matrix”path — done: already honoured · compat: addable in THIS shim, no core
change and no core perf cost · core: gated core change, options-free fast
path stays byte-identical · feature: needs a parser/dumper capability that
does not exist yet.
parse / parseDocument / parseAllDocuments (ParseOptions·DocumentOptions·SchemaOptions·ToJSOptions) reviver JSON.parse-style revive walk done prettyErrors line/col in errors done (our errors carry them) mapAsMap mappings as Map, not Object compat [1] (deep Object->Map post-parse) intAsBigInt big ints as exact BigInt core (cold >15-digit fallback fork) uniqueKeys dup-key throw/comparator vs keep core [2] stringKeys require scalar string keys core maxAliasCount cap alias expansions compat/core version 1.1 | 1.2 | next scalar typing core [3] schema failsafe / core / json / yaml-1.1 core [3] customTags plug in custom tag resolvers core (needs a tag registry) resolveKnownTags !!omap/!!set/!!timestamp under core core (we resolve !!binary only) merge enable `<<` merge keys feature (merge keys unimplemented) keepSourceTokens · onAnchor feature (no document Composer integration) lineCounter option feature (standalone class exported; parse integration remains unsupported)
stringify (ToStringOptions·CreateNodeOptions·SchemaOptions) replacer JSON.stringify-style replacer compat (pre-process the value) sortMapEntries sort map keys on output compat <- easy win (pre-sort the graph) indent block indent width (we hardcode 2) core nullStr/trueStr/falseStr spelling of null/true/false core singleQuote prefer single quotes core indentSeq indent block sequences core directives emit `---` / %YAML markers core lineWidth · minContentWidth · blockQuote (folding) feature (no line folding exists) collectionStyle:flow · flowCollectionPadding · trailingComma feature (no flow-collection writer) aliasDuplicateObjects / noRefs · anchorPrefix core/feature (see js-yaml-compat noRefs note)[1] Our core already coerces non-scalar keys to strings, so mapAsMap keys
come back as strings — partial fidelity vs real yaml.
[2] Our core is last-wins by default (= uniqueKeys: false). Throw-on-dup is
a yaml-parity knob, NOT a spec/suite win — the yaml-test-suite treats
duplicate keys as VALID (see js-yaml-compat.ts note [1]).
[3] Default 1.2-core already matches yaml’s own default; only version: 1.1
/ a non-core schema changes typing (yes->true, sexagesimal, legacy octal).
The remaining known simplifications, called out where they matter below:
- The public
Parserproduces source-preserving CST tokens; it does not compose documents or validate YAML. Document wrappers (parseDocument/parseAllDocuments) still return a MINIMAL stand-in:{ toJS(), toJSON(), contents, errors, warnings }wherecontentsis already the plain JS value (realyamlgives you an ASTNodethere — Map/Seq/Scalar — and only.toJS()converts it). That’s a deliberate, documented simplification (per the task brief) — fine for the overwhelmingly commondoc.toJS()/doc.toJSON()call pattern, not fine for code that walks.contentsas a CST/AST. stringifydelegates to ourstringify(implemented: block-style output with 1.2-core-safe quoting).- We don’t wrap thrown errors in
yaml’s ownYAMLParseError/YAMLWarningclasses (unlike js-yaml-compat.ts, which IS required to rethrow as its ownYAMLException— see that file). Confusingly, our own error class (./core.ts) is ALSO namedYAMLParseError, purely by coincidence — a different class with a different prototype chain, soe instanceof (real yaml's) YAMLParseErrorwill not match ours. Not chased further for this milestone.
Namespaces
Section titled “Namespaces”Classes
Section titled “Classes”Interfaces
Section titled “Interfaces”Type Aliases
Section titled “Type Aliases”Variables
Section titled “Variables”Functions
Section titled “Functions”References
Section titled “References”CSTSourceToken
Section titled “CSTSourceToken”Renames and re-exports SourceToken
CSTToken
Section titled “CSTToken”Renames and re-exports Token