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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling single & block quotes completely #326

Merged
merged 3 commits into from
Nov 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions docs/03_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,20 @@ The `doc.toString()` method may be called with additional options to control the

Used by: `stringify()` and `doc.toString()`

| Name | Type | Default | Description |
| ------------------------------ | ---------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| defaultKeyType | `Type ⎮ null` | `null` | If not `null`, overrides `defaultStringType` for implicit key values. |
| defaultStringType | `Type` | `'PLAIN'` | The default type of string literal used to stringify values. |
| directives | `boolean ⎮ null` | `null` | Include directives in the output. If `true`, at least the document-start marker `---` is always included. If `false`, no directives or marker is ever included. If `null`, directives and marker may be included if required. |
| 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. |
| 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). |
| 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` | `false` | Prefer 'single quote' rather than "double quote" where applicable. |
| trueStr | `string` | `'true'` | String representation for `true` values. |
| Name | Type | Default | Description |
| ------------------------------ | -------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| blockQuote | `boolean ⎮ 'folded' ⎮ 'literal'` | `true` | Use block quote styles for scalar values where applicable. Set to `false` to disable block quotes completely. |
| defaultKeyType | `Type ⎮ null` | `null` | If not `null`, overrides `defaultStringType` for implicit key values. |
| defaultStringType | `Type` | `'PLAIN'` | The default type of string literal used to stringify values. |
| directives | `boolean ⎮ null` | `null` | Include directives in the output. If `true`, at least the document-start marker `---` is always included. If `false`, no directives or marker is ever included. If `null`, directives and marker may be included if required. |
| 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. |
| 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). |
| 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. |
| trueStr | `string` | `'true'` | String representation for `true` values. |
15 changes: 12 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ export type ToJSOptions = {
}

export type ToStringOptions = {
/**
* Use block quote styles for scalar values where applicable.
* Set to `false` to disable block quotes completely.
*
* Default: `true`
*/
blockQuote?: boolean | 'folded' | 'literal'

/**
* The default type of string literal used to stringify implicit key values.
* Output may use other types if required to fully represent the value.
Expand Down Expand Up @@ -298,11 +306,12 @@ export type ToStringOptions = {
simpleKeys?: boolean

/**
* Prefer 'single quote' rather than "double quote" where applicable.
* Use 'single quote' rather than "double quote" where applicable.
* Set to `false` to disable single quotes completely.
*
* Default: `false`
* Default: `null`
*/
singleQuote?: boolean
singleQuote?: boolean | null

/**
* String representation for `true`.
Expand Down
4 changes: 2 additions & 2 deletions src/parse/cst-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function createScalarToken(
implicitKey,
indent: indent > 0 ? ' '.repeat(indent) : '',
inFlow,
options: { lineWidth: -1 }
options: { blockQuote: true, lineWidth: -1 }
} as StringifyContext
)
const end = context.end ?? [
Expand Down Expand Up @@ -180,7 +180,7 @@ export function setScalarValue(
implicitKey: implicitKey || indent === null,
indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
inFlow,
options: { lineWidth: -1 }
options: { blockQuote: true, lineWidth: -1 }
} as StringifyContext
)
switch (source[0]) {
Expand Down
3 changes: 2 additions & 1 deletion src/stringify/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const createStringifyContext = (
typeof options.indent === 'number' ? ' '.repeat(options.indent) : ' ',
options: Object.assign(
{
blockQuote: true,
defaultKeyType: null,
defaultStringType: 'PLAIN',
directives: null,
Expand All @@ -50,7 +51,7 @@ export const createStringifyContext = (
minContentWidth: 20,
nullStr: 'null',
simpleKeys: false,
singleQuote: false,
singleQuote: null,
trueStr: 'true',
verifyAliasOrder: true
},
Expand Down
57 changes: 32 additions & 25 deletions src/stringify/stringifyString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ function doubleQuotedString(value: string, ctx: StringifyContext) {
}

function singleQuotedString(value: string, ctx: StringifyContext) {
if (ctx.implicitKey) {
if (/\n/.test(value)) return doubleQuotedString(value, ctx)
} else {
// single quoted string can't have leading or trailing whitespace around newline
if (/[ \t]\n|\n[ \t]/.test(value)) return doubleQuotedString(value, ctx)
}
if (
ctx.options.singleQuote === false ||
(ctx.implicitKey && value.includes('\n')) ||
/[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
)
return doubleQuotedString(value, ctx)

const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '')
const res =
"'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"
Expand All @@ -149,26 +150,44 @@ function singleQuotedString(value: string, ctx: StringifyContext) {
: foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx))
}

function quotedString(value: string, ctx: StringifyContext) {
const { singleQuote } = ctx.options
let qs
if (singleQuote === false) qs = doubleQuotedString
else {
const hasDouble = value.includes('"')
const hasSingle = value.includes("'")
if (hasDouble && !hasSingle) qs = singleQuotedString
else if (hasSingle && !hasDouble) qs = doubleQuotedString
else qs = singleQuote ? singleQuotedString : doubleQuotedString
}
return qs(value, ctx)
}

function blockString(
{ comment, type, value }: StringifyScalar,
ctx: StringifyContext,
onComment?: () => void,
onChompKeep?: () => void
) {
const { lineWidth, blockQuote } = ctx.options
// 1. Block can't end in whitespace unless the last line is non-empty.
// 2. Strings consisting of only whitespace are best rendered explicitly.
if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
return doubleQuotedString(value, ctx)
if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
return quotedString(value, ctx)
}

const indent =
ctx.indent ||
(ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '')
const literal =
type === Scalar.BLOCK_FOLDED
blockQuote === 'literal'
? true
: blockQuote === 'folded' || type === Scalar.BLOCK_FOLDED
? false
: type === Scalar.BLOCK_LITERAL
? true
: !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length)
: !lineLengthOverLimit(value, lineWidth, indent.length)
if (!value) return literal ? '|\n' : '>\n'

// determine chomping from whitespace at value end
Expand Down Expand Up @@ -251,26 +270,14 @@ function plainString(
(implicitKey && /[\n[\]{},]/.test(value)) ||
(inFlow && /[[\]{},]/.test(value))
) {
return doubleQuotedString(value, ctx)
return quotedString(value, ctx)
}
if (
!value ||
/^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(
value
)
) {
const hasDouble = value.indexOf('"') !== -1
const hasSingle = value.indexOf("'") !== -1
let quotedString
if (hasDouble && !hasSingle) {
quotedString = singleQuotedString
} else if (hasSingle && !hasDouble) {
quotedString = doubleQuotedString
} else if (ctx.options.singleQuote) {
quotedString = singleQuotedString
} else {
quotedString = doubleQuotedString
}
// not allowed:
// - empty string, '-' or '?'
// - start with an indicator character (except [?:-]) or /[?-] /
Expand Down Expand Up @@ -305,7 +312,7 @@ function plainString(
tag.tag !== 'tag:yaml.org,2002:str' &&
tag.test?.test(str)
)
return doubleQuotedString(value, ctx)
return quotedString(value, ctx)
}
}
return implicitKey
Expand Down Expand Up @@ -337,7 +344,7 @@ export function stringifyString(
case Scalar.BLOCK_FOLDED:
case Scalar.BLOCK_LITERAL:
return implicitKey || inFlow
? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
: blockString(ss, ctx, onComment, onChompKeep)
case Scalar.QUOTE_DOUBLE:
return doubleQuotedString(ss.value, ctx)
Expand Down
88 changes: 79 additions & 9 deletions tests/doc/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,28 +732,98 @@ describe('Scalar options', () => {
})
})

for (const { bool, exp } of [
{ bool: false, exp: '"foo #bar"\n' },
{ bool: true, exp: "'foo #bar'\n" }
for (const { singleQuote, numeric, upgrade, parsed } of [
{
singleQuote: null,
numeric: '"123"\n',
upgrade: '"foo #bar"\n',
parsed: "'foo'\n"
},
{
singleQuote: false,
numeric: '"123"\n',
upgrade: '"foo #bar"\n',
parsed: '"foo"\n'
},
{
singleQuote: true,
numeric: "'123'\n",
upgrade: "'foo #bar'\n",
parsed: "'foo'\n"
}
]) {
describe(`singleQuote: ${bool}`, () => {
const opt = { singleQuote: bool }
describe(`singleQuote: ${singleQuote}`, () => {
const opt = { singleQuote }

test('plain', () => {
expect(YAML.stringify('foo bar', opt)).toBe('foo bar\n')
})

test('forced', () => {
expect(YAML.stringify('foo: "bar"', opt)).toBe(`'foo: "bar"'\n`)
test('contains double-quote', () => {
expect(YAML.stringify('foo: "bar"', opt)).toBe(
singleQuote === false ? `"foo: \\"bar\\""\n` : `'foo: "bar"'\n`
)
})

test('contains single-quote', () => {
expect(YAML.stringify("foo: 'bar'", opt)).toBe(`"foo: 'bar'"\n`)
})

test('numerical string', () => {
expect(YAML.stringify('123', opt)).toBe('"123"\n')
expect(YAML.stringify('123', opt)).toBe(numeric)
})

test('upgrade from plain', () => {
expect(YAML.stringify('foo #bar', opt)).toBe(exp)
expect(YAML.stringify('foo #bar', opt)).toBe(upgrade)
})

test('parsed node', () => {
const doc = YAML.parseDocument("'foo'")
expect(doc.toString(opt)).toBe(parsed)
})
})
}

for (const { blockQuote, marker, value, parsed } of [
{
blockQuote: false,
marker: '"---"\n',
value: 'foo: "bar\\nfuzz\\n"\n',
parsed: '"foo\\n"\n'
},
{
blockQuote: true,
marker: '|-\n ---\n',
value: 'foo: |\n bar\n fuzz\n',
parsed: '>\nfoo\n'
},
{
blockQuote: 'literal',
marker: '|-\n ---\n',
value: 'foo: |\n bar\n fuzz\n',
parsed: '|\nfoo\n'
},
{
blockQuote: 'folded',
marker: '>-\n ---\n',
value: 'foo: >\n bar\n\n fuzz\n',
parsed: '>\nfoo\n'
}
]) {
describe(`blockQuote: ${blockQuote}`, () => {
test('doc-marker', () => {
expect(YAML.stringify('---', { blockQuote })).toBe(marker)
})

test('map with value', () => {
expect(YAML.stringify({ foo: 'bar\nfuzz\n' }, { blockQuote })).toBe(
value
)
})

test('override parsed type', () => {
const doc = YAML.parseDocument('>\n foo\n')
expect(doc.toString({ blockQuote })).toBe(parsed)
})
})
}
Expand Down