From 73849fa42c36c03ceba3ce539666f2e1aeed05e4 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 26 Aug 2023 15:05:08 -0400 Subject: [PATCH] fix: Do not require quotes for implicit keys with flow indicators --- src/stringify/stringifyString.ts | 2 +- tests/doc/stringify.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/stringify/stringifyString.ts b/src/stringify/stringifyString.ts index 04a33945..9caab598 100644 --- a/src/stringify/stringifyString.ts +++ b/src/stringify/stringifyString.ts @@ -280,7 +280,7 @@ function plainString( const { type, value } = item const { actualString, implicitKey, indent, indentStep, inFlow } = ctx if ( - (implicitKey && /[\n[\]{},]/.test(value)) || + (implicitKey && value.includes('\n')) || (inFlow && /[[\]{},]/.test(value)) ) { return quotedString(value, ctx) diff --git a/tests/doc/stringify.ts b/tests/doc/stringify.ts index 5f68dfd4..a094203a 100644 --- a/tests/doc/stringify.ts +++ b/tests/doc/stringify.ts @@ -336,6 +336,11 @@ z: expect(String(doc)).toBe('a: 1\n? b: 2\n c: 3\n') }) + test('plain key containing flow indicators', () => { + const doc = YAML.parseDocument('foo[{}]: bar') + expect(doc.toString()).toBe('foo[{}]: bar\n') + }) + describe('No extra whitespace for empty values', () => { const getDoc = () => new YAML.Document, false>({