Skip to content

forceQuotes quotes non-string scalars and emits explicit type tags in v5 #798

Closed

Description

@okaryo

Description

In js-yaml 5.4.1, enabling forceQuotes applies the quoted style not only to JavaScript strings, but also to booleans, numbers, and null.

As a result, explicit tags such as !!bool, !!int, !!float, and !!null are emitted to preserve their types.

The documentation describes forceQuotes as quoting “all non-key strings,” and v4 only quoted string values. Is this behavior intentional, or is it a regression from v4?

Reproduction

import { dump } from 'js-yaml'

console.log(dump(
  {
    bool: true,
    integer: 42,
    float: 3.14,
    nil: null,
    text: 'true'
  },
  {
    forceQuotes: true,
    quoteStyle: 'double'
  }
))

Environment:

  • js-yaml: 5.4.1
  • Node.js: 24.8.0

Actual output

bool: !!bool "true"
integer: !!int "42"
float: !!float "3.14"
nil: !!null "null"
text: "true"

Expected output

Based on the documented behavior and js-yaml v4, I expected only JavaScript string values to be quoted:

bool: true
integer: 42
float: 3.14
nil: null
text: "true"

Additional context

The emitted YAML is valid, and dump() followed by load() preserves the original types. The issue is not the use of explicit tags itself, but that forceQuotes is being applied to non-string scalar values.

The forceQuotes documentation says:

Quotes all non-key strings using quoteStyle.

The v5.1.0 changelog also describes the restored option as forcing quotes around non-key strings.

However, applyForceQuotesOption() currently checks only whether the scalar is plain and is not a key. It does not check the scalar’s semantic tag before changing its style.

Once a non-string scalar is quoted, the presenter correctly emits its explicit tag to preserve its type:

https://github.com/nodeca/js-yaml/blob/5.4.1/src/ast/presenter.ts#L408-L417

Activity

  1. puzrin commented on Sep 13, 2026

    @puzrin
    Member

    Thank you!

  2. puzrin commented on Sep 13, 2026

    @puzrin
    Member

    Fixed in 5.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions