From fa4a6f415ce7cee8dee16a88938d62acd0619112 Mon Sep 17 00:00:00 2001 From: Vasundhara Devi Date: Mon, 19 Oct 2026 18:58:56 +0530 Subject: [PATCH 1/2] fix: quote YAML 1.1 ambiguous strings --- lib/__tests__/json-utils.test.ts | 32 +++++++++++++++++++++++++++++--- lib/json-utils.ts | 9 +++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/__tests__/json-utils.test.ts b/lib/__tests__/json-utils.test.ts index 1635d3c..72ada16 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,42 @@ 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"'], ])("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..6397cd4 100644 --- a/lib/json-utils.ts +++ b/lib/json-utils.ts @@ -339,14 +339,19 @@ 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+(?::[0-5]?\d)+(?:\.\d+)?$/.test(s)) return true; + if (/^[+-]?\d+(?:_\d+)+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.test(s)) return true; + if (/^[+-]?0[xX][0-9a-fA-F_]*_[0-9a-fA-F_]*$/.test(s)) return true; + if (/^[+-]?0[bB][01]+(?:_[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; From 3743c2a1925950ec3c50ce6b28dc8e9ff575d551 Mon Sep 17 00:00:00 2001 From: Vasundhara Devi Date: Tue, 20 Oct 2026 12:42:02 +0530 Subject: [PATCH 2/2] fix: handle YAML 1.1 numeric underscores --- lib/__tests__/json-utils.test.ts | 8 ++++++++ lib/json-utils.ts | 7 ++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/__tests__/json-utils.test.ts b/lib/__tests__/json-utils.test.ts index 72ada16..687c29a 100644 --- a/lib/__tests__/json-utils.test.ts +++ b/lib/__tests__/json-utils.test.ts @@ -179,6 +179,14 @@ describe("toYAML quotes strings YAML would read as other types", () => { ["=", '"="'], ["<<", '"<<"'], ["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); diff --git a/lib/json-utils.ts b/lib/json-utils.ts index 6397cd4..153362a 100644 --- a/lib/json-utils.ts +++ b/lib/json-utils.ts @@ -346,10 +346,10 @@ function needsQuote(s: string): boolean { 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+(?::[0-5]?\d)+(?:\.\d+)?$/.test(s)) return true; - if (/^[+-]?\d+(?:_\d+)+(?:\.\d+)?(?:[eE][+-]?\d+)?$/.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]+(?:_[01]+)*$/.test(s)) return true; + if (/^[+-]?0[bB][01_]+$/.test(s)) return true; if (/^[+-]?\.(inf|nan)$/i.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; @@ -357,6 +357,7 @@ function needsQuote(s: string): boolean { 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");