Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions lib/__tests__/json-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ describe("diffLines large-input guard", () => {
});
});


describe("toYAML quotes strings YAML would read as other types", () => {
it.each([
[".5", '".5"'],
Expand All @@ -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", () => {
Expand Down
10 changes: 8 additions & 2 deletions lib/json-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,19 +339,25 @@ export function toTypeScript(raw: string, rootName = "Root"): Result<string> {
/* ------- 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");
Expand Down
Loading