declaration-helpers.ts view source
also exported from index.ts
(key: string, value: unknown): unknown import {compactReplacer} from 'svelte-docinfo/declaration-helpers.js'; JSON replacer that strips Zod default values for compact serialization.
Strips empty arrays and false booleans — both are Zod .default() values
restored on .parse(), so the round-trip is lossless for svelte-docinfo types.
Assumes all boolean fields in the schema default to false — a true-defaulted
boolean would need its false values preserved, breaking the round-trip.
One keyed exemption: value is never stripped. TypeJson's literal nodes
carry data there ({kind: 'literal', value: false} is the literal type
false, required by the schema), not a defaulted flag — no other output
field is named value, and any future one must not be a false-defaulted
boolean.
Root-value caveat: JSON.stringify([], compactReplacer) returns the JS
undefined (not the string '[]'), and JSON.stringify(false, compactReplacer)
returns the JS undefined too. Object-rooted callers (AnalyzeResultJson
envelope, CLI output) don't hit this — empty inner arrays strip and
AnalyzeResultJson.parse restores them on the consumer side. Array-rooted
callers (Vite plugin, anyone splicing the JSON into a source template)
must handle the empty case themselves before calling this; see
vite.ts:updateOutputFromQuery for the pattern.
Two guard tests in declaration-helpers.test.ts lock this in:
every z.boolean().default in types.ts uses false— source-regex check that fails on a newz.boolean().default(true).- `parse → stringify(compactReplacer) → parse is a faithful round-trip
across every variant` — exercises every variant and member through a
full round-trip, catching regressions where a
.default(false)or.default([])is removed (or a new field is added that the replacer drops but Zod doesn't restore).
key
stringvalue
unknownreturns
unknown examples
const result = await analyze({sourceFiles, sourceOptions});
const json = JSON.stringify(result, compactReplacer);
// On the consumer side, restore Zod defaults:
const restored = AnalyzeResultJson.parse(JSON.parse(json));