Skip to content

fix: quote flow scalars where a colon precedes a flow indicator - #773

Merged
puzrin merged 1 commit into
nodeca:masterfrom
spokodev:fix/flow-colon-quoting
Jul 9, 2026
Merged

puzrin merged 1 commit into
nodeca:masterfrom
spokodev:fix/flow-colon-quoting

Conversation

@spokodev

@spokodev spokodev commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Problem

Flow-style dump produces output the library cannot re-parse when a plain scalar contains a : immediately followed by a flow indicator ({ } [ ] ,):

const yaml = require('js-yaml')
yaml.dump([':{'], { flowLevel: 0 })       // "[:{]\n"
yaml.load(yaml.dump([':{'], { flowLevel: 0 }))
// YAMLException: missed comma between flow collection entries (1:4)

This breaks load(dump(x)) for values such as ':{', ':[', ':,', ':}', ':]', and 'x:{', in both flow sequences and flow mappings. Block style is unaffected — there the scalar stays plain and round-trips fine, which is correct.

Root cause

isPlainSafe in src/ast/presenter.ts decides whether a character may appear unescaped in a plain scalar. Its last clause implements the spec's ns-plain-char rule for a : followed by content:

[130] ns-plain-char(c) ::= ... | ( ":" /* Followed by an ns-plain-safe(c) */ )

The clause was (prev === CHAR_COLON && cIsNsChar), i.e. it treated any ns-char after a colon as safe. But ns-plain-safe(c) is context-dependent:

  • [128] ns-plain-safe-out ::= ns-char (block context)
  • [129] ns-plain-safe-in ::= ns-char - c-flow-indicator (flow context)

In flow context the following character must additionally not be a flow indicator. Because the clause skipped that exclusion, { } [ ] , after a : were wrongly accepted, so the scalar was emitted plain and the {/[/, reopened or closed the surrounding flow collection.

Fix

Add the c-flow-indicator exclusion to the colon clause when not in block context, matching ns-plain-safe-in. The #-after-colon case (:#) and normal :x stay plain; flow indicators after a colon now force quoting.

Verification

Added flowLevel — quotes a colon followed by a flow indicator to test/core/units/dump-options.test.mjs, asserting load(dump(value, { flowLevel: 0 })) === value for the affected strings in flow sequences and mappings.

  • Before: the new test throws missed comma between flow collection entries.
  • After: it passes; full suite green (npm test, 1599 passing, 0 failing). No over-quoting — :a, a:b, :#, http://x remain plain in flow.

dist/ is gitignored, so no build artifacts are committed.

@puzrin
puzrin merged commit bd7ebb2 into nodeca:master Jul 9, 2026
1 check passed
robbevp added a commit to robbevp/website-robbevanpetegem that referenced this pull request Nov 3, 2026
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [js-yaml](https://github.com/nodeca/js-yaml) | dependencies | major | [`^4.1.0` -> `^5.0.0`](https://renovatebot.com/diffs/npm/js-yaml/4.3.2/5.5.0) |

---

### Release Notes

<details>
<summary>nodeca/js-yaml (js-yaml)</summary>

### [`v5.5.0`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#550---2026-10-16)

[Compare Source](nodeca/js-yaml@5.4.3...5.5.0)

##### Changed

- Updated `argparse` to v3.
- Updated development dependencies.

##### Fixed

- Correct percent-encoding of shorthand and verbatim tags.
- Reject `,`, `]`, and `}` at the start of block plain scalars, [#&#8203;801](nodeca/js-yaml#801).
- Report the exact position of non-printable characters in errors, [#&#8203;801](nodeca/js-yaml#801).

### [`v5.4.3`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#543---2026-10-06)

[Compare Source](nodeca/js-yaml@5.4.2...5.4.3)

##### Fixed

- Fixed loading a whitespace-only block scalar followed by a less indented
  line, [#&#8203;802](nodeca/js-yaml#802).

### [`v5.4.2`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#542---2026-09-13)

[Compare Source](nodeca/js-yaml@5.4.1...5.4.2)

##### Fixed

- `forceQuotes` no longer quotes non-string scalars, [#&#8203;798](nodeca/js-yaml#798).

### [`v5.4.1`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#541---2026-08-26)

[Compare Source](nodeca/js-yaml@5.4.0...5.4.1)

##### Changed

- Hard-limit merge sequence size to 100.

##### Security

- Count empty mappings in merge sequences toward `maxTotalMergeKeys` to limit
  CPU usage, [#&#8203;797](nodeca/js-yaml#797).

### [`v5.4.0`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#540---2026-08-25)

[Compare Source](nodeca/js-yaml@5.3.0...5.4.0)

##### Added

- Added the `scalarStyleRules` dumper option to customize string formatting.
  See [Scalar styling](docs/scalar_styling.md) for details.

##### Changed

- \[breaking] Flattened the low-level AST node style representation. Scalar
  and collection nodes now use `SCALAR_STYLE` and `COLLECTION_STYLE` values;
  explicit tags use the separate `tagged` property. Alias nodes now contain
  only `kind` and `anchor`. This only affects code that directly constructs or
  edits AST nodes.
- \[breaking] The `sortKeys` option was rewritten using AST mutation to avoid
  side effects.
- Reworked scalar style selection. This can change formatting without changing
  loaded values; in particular, whitespace-only strings are now double-quoted.

##### Fixed

- Accept a byte order mark at the start of each document in a stream, [#&#8203;791](nodeca/js-yaml#791).
- Produce valid flow mappings with `quoteFlowKeys` and `flowSkipColonSpace`,
  including alias and property-only keys, [#&#8203;786](nodeca/js-yaml#786).
- Preserve empty scalar items when converting block sequences to flow style.
- Do not apply the 1024-character simple-key limit to flow mapping keys.
- Count Unicode code points, rather than UTF-16 code units, for the
  1024-character simple-key limit.
- Add an explicit document-end marker after keep-chomped block scalars when
  needed to preserve trailing newlines.

### [`v5.3.0`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#530---2026-08-14)

[Compare Source](nodeca/js-yaml@5.2.3...5.3.0)

This release focuses on reworking the documentation and making small
architectural improvements before moving forward.

##### Added

- Added completely new documentation.
- Exported `DUMP_SCHEMA`, the default schema used by the dumper.
- Added `YAMLException.throwAt()` for throwing an error at a source position.

##### Changed

- Changed flat constant exports to grouped exports: `EVENT_ID`, `SCALAR_STYLE`,
  `COLLECTION_STYLE`, and `CHOMPING_MODE`, along with their value types. The old
  exports are still preserved, but deprecated.
- Made `identify` mandatory for custom tag definitions. Use
  `identify: () => false` for load-only tags.

##### Deprecated

- Deprecated flat constant exports. Use grouped ones instead.

##### Removed

- Removed the `MERGE_KEY` export (not used anymore after last fixes).

##### Fixed

- Validate `<<` sequence items at merge time, so aliased merge sources are
  checked too.
- Resolve `<<` outside of a mapping key as the plain string `'<<'`, matching
  v4, instead of leaking an internal symbol into the result.

### [`v5.2.3`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#523---2026-08-01)

[Compare Source](nodeca/js-yaml@5.2.2...5.2.3)

##### Fixed

- Prevent prototype fallback when resolving tags and mapping entries, [#&#8203;782](nodeca/js-yaml#782).
- Resolve `!!timestamp` years 0000-0099 correctly, [#&#8203;775](nodeca/js-yaml#775).
- Preserve implicit null mapping values before document markers and reject
  unpaired mapping event streams, [#&#8203;784](nodeca/js-yaml#784).
- Preserve folded scalar values with tab-indented lines when round-tripping a
  parsed AST through `present()`; `dump()` and loading are unaffected, [#&#8203;780](nodeca/js-yaml#780).

### [`v5.2.2`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#522---2026-07-24)

[Compare Source](nodeca/js-yaml@5.2.1...5.2.2)

##### Fixed

- Quote flow scalars where a colon precedes a flow indicator, [#&#8203;773](nodeca/js-yaml#773).

##### Security

- Avoid exponential parsing time for nested flow sequence pairs.

### [`v5.2.1`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#521---2026-07-02)

[Compare Source](nodeca/js-yaml@5.2.0...5.2.1)

##### Fixed

- Add `Map` support to !!omap (should work when `realMapTag` used)

##### Security

- Remove quadratic complexity from !!omap `addItem`. Regression from v5
  (usually not critical, because YAML11\_SCHEMA is not default anymore).

### [`v5.2.0`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#520---2026-06-26)

[Compare Source](nodeca/js-yaml@5.1.0...5.2.0)

##### Added

- Added `maxTotalMergeKeys` (10000) loader option to limit the total number of
  keys processed by YAML merge (`<<`) across one `load()` / `loadAll()` call.
- Added `maxAliases` (-1) loader option to limit the number of YAML aliases per
  document.

##### Removed

- `maxMergeSeqLength` replaced with `maxTotalMergeKeys` for limiting YAML merge
  processing.

##### Fixed

- Round-trip of integers with exponential form (>= `1e21`)

### [`v5.1.0`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#510---2026-06-23)

[Compare Source](nodeca/js-yaml@5.0.0...5.1.0)

##### Added

- Collection tags can finalize an incrementally populated carrier into a
  different result value.

##### Changed

- \[breaking] `quoteStyle` now selects the preferred quote style; use the
  restored `forceQuotes` option to force quoting non-key strings.

### [`v5.0.0`](https://github.com/nodeca/js-yaml/blob/HEAD/CHANGELOG.md#500---2026-06-20)

[Compare Source](nodeca/js-yaml@4.3.2...5.0.0)

##### Added

- Added named exports for schemas, tags, parser events and AST utilities.
- Reworked `JSON_SCHEMA` and `CORE_SCHEMA` with spec-compliant scalar resolution
  rules, and added `YAML11_SCHEMA`.
- Added `realMapTag` for lossless mappings with non-string and complex keys.
  Object-based mappings now reject complex keys instead of stringifying them.
- Added `dump()` `transform` option for changing the generated AST before
  rendering.
- Added `dump()` options `seqInlineFirst`, `flowBracketPadding`,
  `flowSkipCommaSpace`, `flowSkipColonSpace`, `quoteFlowKeys`, `quoteStyle` and
  `tagBeforeAnchor`.
- Added formal data layers (events and AST) for modular data pipelines.
  - Added low-level parser (to events), presenter and visitor APIs.
- Added the [YAML Test Suite](https://github.com/yaml/yaml-test-suite) to the
  test set.

##### Changed

- See the [migration guide](docs/migrate_v4_to_v5.md) for upgrade notes.
- Rewritten in TypeScript and reorganized the public API around flat named
  exports.
- Reduced the set of exported schemas:
  - YAML 1.2 schemas: `CORE_SCHEMA` (loader default), `JSON_SCHEMA`,
    `FAILSAFE_SCHEMA`.
  - `YAML11_SCHEMA`, a combination of all YAML 1.1 tags (YAML 1.1 does not
    specify a schema, only "types").
- `load`/`dump` default behaviour is now specified exactly via schemas:
  - `load` uses `CORE_SCHEMA`, without `!!merge` by default.
  - `dump` uses `YAML11_SCHEMA` + `CORE_SCHEMA` for the quoting check, to
    guarantee backward compatibility by default.
- `!!set` is now loaded as a JavaScript `Set`.
- Replaced the `Type` API with a tags API. Similar, but more precise and
  simpler. See examples for details. Tags can be defined via
  `defineScalarTag()`, `defineSequenceTag()` and `defineMappingTag()`, or as a
  spread + override of an existing tag.
- Renamed `Schema.extend()` to `Schema.withTags()`.
- Expanded YAML 1.2 conformance and improved handling of directives, document
  markers, block keys, multiline scalars, tag syntax and other things.
- `load()` now throws on empty input instead of returning `undefined`.
- Moved browser builds to the `js-yaml/browser` export.
- Deprecated the `loadAll` signature with an iterator (still works, but is a
  candidate for removal).

##### Removed

- Removed deprecated `safeLoad()`, `safeLoadAll()` and `safeDump()` exports.
- Removed `DEFAULT_SCHEMA` and the nested `types` export.
- Removed loader options `onWarning`, `legacy` and `listener`.
- Removed dumper options `styles`, `replacer`, `noCompatMode`, `condenseFlow`,
  `quotingType` and `forceQuotes`. Renamed `noArrayIndent` to `seqNoIndent`.
  Formatting and representation are now configured through presenter options,
  schemas and tag definitions. See migration guide on how to replace.
- Removed support for importing internal files from `lib/`.

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS42MS4wIiwidXBkYXRlZEluVmVyIjoiNDEuNjEuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: Robbe Van Petegem <git@robbevp.be>
Reviewed-on: https://git.robbevp.be/robbevp/website-robbevanpetegem/pulls/516
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants