Skip to content

Commit 9558e96

Browse files
authored
fix: Do not require quotes for implicit keys with flow indicators (#494)
1 parent 07063bc commit 9558e96

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/stringify/stringifyString.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function plainString(
280280
const { type, value } = item
281281
const { actualString, implicitKey, indent, indentStep, inFlow } = ctx
282282
if (
283-
(implicitKey && /[\n[\]{},]/.test(value)) ||
283+
(implicitKey && value.includes('\n')) ||
284284
(inFlow && /[[\]{},]/.test(value))
285285
) {
286286
return quotedString(value, ctx)

tests/doc/stringify.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ z:
336336
expect(String(doc)).toBe('a: 1\n? b: 2\n c: 3\n')
337337
})
338338

339+
test('plain key containing flow indicators', () => {
340+
const doc = YAML.parseDocument('foo[{}]: bar')
341+
expect(doc.toString()).toBe('foo[{}]: bar\n')
342+
})
343+
339344
describe('No extra whitespace for empty values', () => {
340345
const getDoc = () =>
341346
new YAML.Document<YAML.YAMLMap<YAML.Scalar, YAML.Scalar>, false>({

0 commit comments

Comments
 (0)