Skip to content

Comma is inserted after comment when serializing flow map #528

@WhiteAbeLincoln

Description

@WhiteAbeLincoln

Describe the bug
When serializing a flow mapping, the separating comma is appended to a trailing comment instead of after a value.
This results in an invalid yaml document (despite no changes being made to the document) because of a missing comma between flow map items.

This behavior does not occur with a flow sequence, just a flow map.

To Reproduce

const yaml = require('yaml')
const assert = require('node:assert')

const textBefore =
`test: {
  items: {
    first: { value: true }, # comment
    second: { value: true }
  },
  array: [
    first, # comment
    second
  ]
}`

const textAfter =
`test:
  {
    items:
      {
        first: { value: true } # comment,
        second: { value: true }
      },
    array:
      [
        first, # comment
        second
      ]
  }`

const doc = yaml.parseDocument(textBefore)

const docText = doc.toString()

// no error
assert.equal(docText.trim(), textAfter)

const docAfter = yaml.parseDocument(docText)

// throws error since expected is an array of one error:
/*
YAMLParseError: Missing , between flow map items at line 6, column 9:

            first: { value: true } # comment,
            second: { value: true }
            ^
*/
assert.deepStrictEqual( docAfter.errors, [])

Expected behaviour
Comma should be in the correct position so that the document still parses after being stringified

Versions (please complete the following information):

  • Environment: Node 20.11.1, Firefox 123.0, Edge 122.0.2365.66
  • yaml: 2.4.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions