From 534d8ad66866965bf788e38f9ee04443c49f312d Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Wed, 24 Jul 2024 13:21:57 +0300 Subject: [PATCH] fix: Ignore minContentWidth if greater than lineWidth (fixes #562) --- docs/03_options.md | 2 +- src/stringify/foldFlowLines.ts | 1 + tests/doc/stringify.ts | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/03_options.md b/docs/03_options.md index 644afd8a..92c7d627 100644 --- a/docs/03_options.md +++ b/docs/03_options.md @@ -161,7 +161,7 @@ Used by: `stringify()` and `doc.toString()` | 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. | -| minContentWidth | `number` | `20` | Minimum line width for highly-indented content (set to `0` to disable). | +| minContentWidth | `number` | `20` | Minimum line width for highly-indented content (set to `0` to disable). Ignored if greater than lineWidth. | | nullStr | `string` | `'null'` | String representation for `null` values. | | simpleKeys | `boolean` | `false` | Require keys to be scalars and always use implicit rather than explicit notation. | | singleQuote | `boolean ⎮ null` | `null` | Use 'single quote' rather than "double quote" where applicable. Set to `false` to disable single quotes completely. | diff --git a/src/stringify/foldFlowLines.ts b/src/stringify/foldFlowLines.ts index 60633f90..ea15a47c 100644 --- a/src/stringify/foldFlowLines.ts +++ b/src/stringify/foldFlowLines.ts @@ -51,6 +51,7 @@ export function foldFlowLines( }: FoldOptions = {} ) { if (!lineWidth || lineWidth < 0) return text + if (lineWidth < minContentWidth) minContentWidth = 0 const endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length) if (text.length <= endStep) return text const folds = [] diff --git a/tests/doc/stringify.ts b/tests/doc/stringify.ts index 7a150e63..400b051f 100644 --- a/tests/doc/stringify.ts +++ b/tests/doc/stringify.ts @@ -920,6 +920,13 @@ describe('lineWidth', () => { "[ 'Sed', 'ut', 'perspiciatis', 'unde', 'omnis', 'iste', 'natus', 'error', 'sit', 'voluptatem', 'accusantium', 'doloremque', 'laudantium,', 'totam' ]\n" ) }) + + test('less than minContentWidth (eemeli/yaml#562)', () => { + const doc = YAML.parseDocument('>\ntest test test test test') + expect(doc.toString({ lineWidth: 18, minContentWidth: 20 })).toBe( + '>\ntest test test\ntest test\n' + ) + }) }) describe('collectionStyle', () => {