diff --git a/lib/__tests__/json-utils.test.ts b/lib/__tests__/json-utils.test.ts index 1635d3c..687c29a 100644 --- a/lib/__tests__/json-utils.test.ts +++ b/lib/__tests__/json-utils.test.ts @@ -152,6 +152,7 @@ describe("diffLines large-input guard", () => { }); }); + describe("toYAML quotes strings YAML would read as other types", () => { it.each([ [".5", '".5"'], @@ -162,17 +163,50 @@ describe("toYAML quotes strings YAML would read as other types", () => { [".NaN", '".NaN"'], ["2026-04-15", '"2026-04-15"'], ["2026-04-15T10:00:00Z", '"2026-04-15T10:00:00Z"'], + ["1:30", '"1:30"'], + ["12:30:45", '"12:30:45"'], + ["1:30.5", '"1:30.5"'], + ["1_000", '"1_000"'], + ["1_000.5", '"1_000.5"'], + ["0x_1F", '"0x_1F"'], + ["0b101", '"0b101"'], + ["+0b101", '"+0b101"'], + ["0b1010_0111", '"0b1010_0111"'], + ["y", '"y"'], + ["Y", '"Y"'], + ["n", '"n"'], + ["N", '"N"'], + ["=", '"="'], + ["<<", '"<<"'], + ["2001-1-1 1:00:00", '"2001-1-1 1:00:00"'], + ["1__000", '"1__000"'], + ["1000_", '"1000_"'], + ["1.0_0", '"1.0_0"'], + [".5_0", '".5_0"'], + ["0b_101", '"0b_101"'], + ["0b101_", '"0b101_"'], + ["1_0:30", '"1_0:30"'], + ["1:30.", '"1:30."'], ])("quotes %s", (input, expected) => { const out = toYAML(JSON.stringify({ a: input })); expect(out.ok).toBe(true); if (out.ok) expect(out.value).toBe(`a: ${expected}\n`); }); - it.each(["1.4.0", "v2", "hello"])("leaves %s unquoted", (input) => { - const out = toYAML(JSON.stringify({ a: input })); + it("quotes YAML merge keys", () => { + const out = toYAML('{"<<":"x"}'); expect(out.ok).toBe(true); - if (out.ok) expect(out.value).toBe(`a: ${input}\n`); + if (out.ok) expect(out.value).toBe('"<<": x\n'); }); + + it.each(["1.4.0", "v2", "hello", "1,000", "_1", "0b2", "a:b"])( + "leaves %s unquoted", + (input) => { + const out = toYAML(JSON.stringify({ a: input })); + expect(out.ok).toBe(true); + if (out.ok) expect(out.value).toBe(`a: ${input}\n`); + }, + ); }); describe("toYAML empty array items", () => { diff --git a/lib/json-utils.ts b/lib/json-utils.ts index 5c8e9fe..153362a 100644 --- a/lib/json-utils.ts +++ b/lib/json-utils.ts @@ -339,19 +339,25 @@ export function toTypeScript(raw: string, rootName = "Root"): Result { /* ------- YAML ------- */ function needsQuote(s: string): boolean { if (s === "") return true; + if (s === "=" || s === "<<") return true; if (/^[\s-?:,\[\]{}#&*!|>'"%@`]/.test(s)) return true; if (/[\s:#]$/.test(s)) return true; - if (/^(true|false|null|yes|no|on|off|~)$/i.test(s)) return true; + if (/^(true|false|null|yes|no|on|off|y|n|~)$/i.test(s)) return true; if (/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(s)) return true; if (/^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(s)) return true; if (/^[+-]?0([xX][0-9a-fA-F]+|[oO][0-7]+)$/.test(s)) return true; + if (/^[+-]?\d[\d_]*(?::[0-5]?\d[\d_]*)+(?:\.[\d_]*)?$/.test(s)) return true; + if (/^[+-]?(?:\d[\d_]*(?:\.[\d_]*)?|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?$/.test(s)) return true; + if (/^[+-]?0[xX][0-9a-fA-F_]*_[0-9a-fA-F_]*$/.test(s)) return true; + if (/^[+-]?0[bB][01_]+$/.test(s)) return true; if (/^[+-]?\.(inf|nan)$/i.test(s)) return true; - if (/^\d{4}-\d{2}-\d{2}(?:[Tt ].*)?$/.test(s)) return true; + if (/^\d{4}-\d{1,2}-\d{1,2}(?:[Tt ].*)?$/.test(s)) return true; if (/[:#]\s|\s#/.test(s)) return true; if (/[\n\t\r"]/.test(s)) return true; return false; } + function yamlString(s: string): string { if (!needsQuote(s)) return s; const escaped = s.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\t/g, "\\t").replace(/\r/g, "\\r");