Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flowCollectionPadding toString option #420

Merged
merged 1 commit into from Nov 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/03_options.md
Expand Up @@ -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. |
Expand Down
8 changes: 8 additions & 0 deletions src/options.ts
Expand Up @@ -300,6 +300,14 @@ export type ToStringOptions = {
*/
falseStr?: string

/**
* When true, a single space of padding will be added inside the delimiters
* of non-empty single-line flow collections.
*
* Default: `true`
*/
flowCollectionPadding?: boolean

/**
* The number of spaces to use when indenting code.
*
Expand Down
3 changes: 3 additions & 0 deletions src/stringify/stringify.ts
Expand Up @@ -27,6 +27,7 @@ export type StringifyContext = {
indentAtStart?: number
inFlow: boolean | null
inStringifyKey?: boolean
flowCollectionPadding: string
options: Readonly<
Required<Omit<ToStringOptions, 'collectionStyle' | 'indent'>>
>
Expand All @@ -47,6 +48,7 @@ export function createStringifyContext(
doubleQuotedAsJSON: false,
doubleQuotedMinMultiLineLength: 40,
falseStr: 'false',
flowCollectionPadding: true,
indentSeq: true,
lineWidth: 80,
minContentWidth: 20,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/stringify/stringifyCollection.ts
Expand Up @@ -94,6 +94,7 @@ function stringifyFlowCollection(
const {
indent,
indentStep,
flowCollectionPadding,
options: { commentString }
} = ctx
itemIndent += indentStep
Expand Down Expand Up @@ -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}`
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/doc/stringify.js
Expand Up @@ -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')
});
})