Skip to content
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
9 changes: 8 additions & 1 deletion src/ast/presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,14 @@ function isPlainSafe (c: number, prev: number, inblock: boolean) {
!(prev === CHAR_COLON && !cIsNsChar)
) || // false on ': '
(isNsCharOrWhitespace(prev) && !isWhitespace(prev) && c === CHAR_SHARP) || // change to true on '[^ ]#'
(prev === CHAR_COLON && cIsNsChar) // change to true on ':[^ ]'
(prev === CHAR_COLON && cIsNsChar &&
// outside block context a following c-flow-indicator is not ns-plain-safe
(inblock ||
(c !== CHAR_COMMA &&
c !== CHAR_LEFT_SQUARE_BRACKET &&
c !== CHAR_RIGHT_SQUARE_BRACKET &&
c !== CHAR_LEFT_CURLY_BRACKET &&
c !== CHAR_RIGHT_CURLY_BRACKET))) // change to true on ':[^ ]'
}

// Simplified test for values allowed as the first character in plain style.
Expand Down
12 changes: 11 additions & 1 deletion test/core/units/dump-options.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { dump, JSON_SCHEMA, CORE_SCHEMA, defineMappingTag, realMapTag, YAMLException } from 'js-yaml'
import { dump, load, JSON_SCHEMA, CORE_SCHEMA, defineMappingTag, realMapTag, YAMLException } from 'js-yaml'

describe('dump options', () => {
it('schema — decides which plain scalars need quoting', () => {
Expand Down Expand Up @@ -96,6 +96,16 @@ describe('dump options', () => {
assert.equal(dump({ a: [1, 2] }, { flowLevel: 1 }), 'a: [1, 2]\n')
})

it('flowLevel — quotes a colon followed by a flow indicator', () => {
// In flow context a `:` followed by a flow indicator ({ } [ ] ,) is not
// plain-safe (ns-plain-safe-in excludes c-flow-indicator), so the value
// must be quoted for the output to re-parse.
for (const value of [':{', ':[', ':,', ':}', ':]', 'x:{']) {
assert.deepStrictEqual(load(dump([value], { flowLevel: 0 })), [value])
assert.deepStrictEqual(load(dump({ k: value }, { flowLevel: 0 })), { k: value })
}
})

it('transform — mutates the generated documents before presentation', () => {
const output = dump({ values: [1, 2] }, {
flowLevel: 1,
Expand Down