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

With CST parser newline is ignored/skipped after comment #525

Closed
tiithansen opened this issue Feb 26, 2024 · 2 comments
Closed

With CST parser newline is ignored/skipped after comment #525

tiithansen opened this issue Feb 26, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@tiithansen
Copy link

Describe the bug

Newline which should exist after comment is not serialized and start of array element is serialized directly after comment. This bug does not occur when there is indent before array element. - name: a #Comment a 3 instead of - name: a #Comment a 3.

Input:

version: 1.0.0
#
# Test
services: # Comment 1
# Comment 2
- name: a #Comment a 3
  extraLabels:
    k8s-app: some-app
  # Comment 4
  prop: 1

Output:

version: 1.0.0
#
# Test
services: # Comment 1
# Comment 2- name: a #Comment a 3
  extraLabels:
    k8s-app: some-app
  # Comment 4
  prop: 1

JSON dump of services map key where it can be seen that last element of sep is not newline.

"start": [
  {
    "type": "comment",
    "offset": 15,
    "indent": 0,
    "source": "#"
  },
  {
    "type": "newline",
    "offset": 16,
    "indent": 0,
    "source": "\n"
  },
  {
    "type": "comment",
    "offset": 17,
    "indent": 0,
    "source": "# Test"
  },
  {
    "type": "newline",
    "offset": 23,
    "indent": 0,
    "source": "\n"
  }
],
"key": {
  "type": "scalar",
  "offset": 24,
  "indent": 0,
  "source": "services"
},
"sep": [
  {
    "type": "map-value-ind",
    "offset": 32,
    "indent": 0,
    "source": ":"
  },
  {
    "type": "space",
    "offset": 33,
    "indent": 0,
    "source": " "
  },
  {
    "type": "comment",
    "offset": 34,
    "indent": 0,
    "source": "# Comment 1"
  },
  {
    "type": "newline",
    "offset": 45,
    "indent": 0,
    "source": "\n"
  },
  {
    "type": "comment",
    "offset": 46,
    "indent": 0,
    "source": "# Comment 2"
  }
]

Expected behaviour
Should keep exact same output.

Versions (please complete the following information):

  • Environment: Node v20.5.0
  • yaml: 2.3.4

Additional context

Method used to load:

constructor(file: string) {
    const contents = fs.readFileSync(file).toString()

    let foundDocument = undefined
    for (const token of new Parser().parse(contents)) {
      this.content.push(token)
      if (token.type === 'document') {
        foundDocument = token
      }
    }

    if (foundDocument) {
      this.document = foundDocument
    } else {
      console.error('Document is empty')
      process.exit(1)
    }
  }

Method to save

private stringifyContent(input: Array<CST.Token>) {
    let string = ''
    for (const idx in input) {
      string += CST.stringify(input[idx])
    }
    return string
  }
@tiithansen tiithansen added the bug Something isn't working label Feb 26, 2024
@eemeli
Copy link
Owner

eemeli commented Feb 26, 2024

Yup, confirming that's a bug. This looks like the minimal case for triggering this:

let src = 'a:\n#\n- b'
let [doc] = Array.from(new YAML.Parser().parse(src))
YAML.CST.stringify(doc)
// 'a:\n#- b'

@eemeli eemeli closed this as completed in 40903db Mar 6, 2024
@eemeli
Copy link
Owner

eemeli commented Mar 6, 2024

The newline was getting dropped because the - sequence indicator's treatment as whitespace wasn't covered by a detector for comments on empty values in block mappings, because their assignment needs to be adjusted during the parse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants