From b330dfd078ddb7e3b6bb40a8143a649ca83a0c65 Mon Sep 17 00:00:00 2001 From: Chas Emerick Date: Wed, 26 Oct 2022 21:49:25 -0400 Subject: [PATCH] add `flowCollectionPadding` toString option, fixes #419 --- docs/03_options.md | 1 + src/options.ts | 8 ++++++++ src/stringify/stringify.ts | 3 +++ src/stringify/stringifyCollection.ts | 3 ++- tests/doc/stringify.js | 14 ++++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/03_options.md b/docs/03_options.md index d2aa82ba..f1cb53d1 100644 --- a/docs/03_options.md +++ b/docs/03_options.md @@ -157,6 +157,7 @@ Used by: `stringify()` and `doc.toString()` | doubleQuotedAsJSON | `boolean` | `false` | Restrict double-quoted strings to use JSON-compatible syntax. | | doubleQuotedMinMultiLineLength | `number` | `40` | Minimum length for double-quoted strings to use multiple lines to represent the value. | | falseStr | `string` | `'false'` | String representation for `false` values. | +| flowCollectionPadding | `boolean` | `true` | When true, a single space of padding will be added inside the delimiters of non-empty single-line flow collections. | | indent | `number` | `2` | The number of spaces to use when indenting code. Should be a strictly positive integer. | | indentSeq | `boolean` | `true` | Whether block sequences should be indented. | | lineWidth | `number` | `80` | Maximum line width (set to `0` to disable folding). This is a soft limit, as only double-quoted semantics allow for inserting a line break in the middle of a word. | diff --git a/src/options.ts b/src/options.ts index e20e2a6f..ec132cad 100644 --- a/src/options.ts +++ b/src/options.ts @@ -300,6 +300,14 @@ export type ToStringOptions = { */ falseStr?: string + /** + * When true, a single space of padding will be added around sequences of + * items in flow collections. + * + * Default: `true` + */ + flowCollectionPadding?: boolean + /** * The number of spaces to use when indenting code. * diff --git a/src/stringify/stringify.ts b/src/stringify/stringify.ts index a9ea6f31..2d544728 100644 --- a/src/stringify/stringify.ts +++ b/src/stringify/stringify.ts @@ -27,6 +27,7 @@ export type StringifyContext = { indentAtStart?: number inFlow: boolean | null inStringifyKey?: boolean + flowCollectionPadding: string options: Readonly< Required> > @@ -47,6 +48,7 @@ export function createStringifyContext( doubleQuotedAsJSON: false, doubleQuotedMinMultiLineLength: 40, falseStr: 'false', + flowCollectionPadding: true, indentSeq: true, lineWidth: 80, minContentWidth: 20, @@ -75,6 +77,7 @@ export function createStringifyContext( return { anchors: new Set(), doc, + flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '', indent: '', indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ', inFlow, diff --git a/src/stringify/stringifyCollection.ts b/src/stringify/stringifyCollection.ts index 53b228a8..57aad59b 100644 --- a/src/stringify/stringifyCollection.ts +++ b/src/stringify/stringifyCollection.ts @@ -94,6 +94,7 @@ function stringifyFlowCollection( const { indent, indentStep, + flowCollectionPadding, options: { commentString } } = ctx itemIndent += indentStep @@ -155,7 +156,7 @@ function stringifyFlowCollection( str += line ? `\n${indentStep}${indent}${line}` : '\n' str += `\n${indent}${end}` } else { - str = `${start} ${lines.join(' ')} ${end}` + str = `${start}${flowCollectionPadding}${lines.join(' ')}${flowCollectionPadding}${end}` } } diff --git a/tests/doc/stringify.js b/tests/doc/stringify.js index 469a5523..04a3b1e6 100644 --- a/tests/doc/stringify.js +++ b/tests/doc/stringify.js @@ -1180,3 +1180,17 @@ describe('YAML.stringify on ast Document', () => { expect(YAML.stringify(doc)).toBe('null\n') }) }) + +describe('flow collection padding', () => { + const doc = new YAML.Document(); + doc.contents = new YAML.YAMLSeq(); + doc.contents.items = [1, 2]; + doc.contents.flow = true; + + test('default', () => { + expect(doc.toString()).toBe('[ 1, 2 ]\n') + }); + test('default', () => { + expect(doc.toString({flowCollectionPadding: false})).toBe('[1, 2]\n') + }); +}) \ No newline at end of file