eemeli / yaml

YAML parser and stringifier for JavaScript
https://eemeli.org/yaml
ISC License
1.32k stars 116 forks source link

Array whitespace is changed between parse and stringify #570

Closed pksunkara closed 1 month ago

pksunkara commented 3 months ago

Describe the bug When I am parsing a YAML and stringifying it afterward, the whitespace in an array changes.

To Reproduce

Before:

on: [pull_request]

After:

on: [ pull_request ]

Expected behaviour

The after file should be the same as before

Versions (please complete the following information):

eemeli commented 3 months ago

It's retained in the document that you get from parseDocument; the formatting is not retained in the JS representation.

pksunkara commented 3 months ago

Oh, that makes sense. Let me see if I can use parseDocument and fix it.

pksunkara commented 3 months ago

I haven't fully confirmed because I am still revamping my other logics, but it looks like the "After" version is adding spaces though.

on: [ pull_request ]
pksunkara commented 3 months ago

Yup, I can confirm that the spaces choice between in the square brackets in array is not being preserved.

eemeli commented 3 months ago

Normalising whitespace is done intentionally; information about it is not retained in the parsed document.

pksunkara commented 3 months ago

In that case,

  1. Do you plan to add support for it? (I might be willing to contribute here to this case.)
  2. Is this the same cause for #572 too?
eemeli commented 3 months ago
  1. No; if you need to retain whitespace, you should be applying your changes to the CST and serialising that: https://eemeli.org/yaml/#working-with-cst-tokens
  2. Same root cause, but details differ. I'll comment on that issue separately.
eemeli commented 1 month ago

Closing, as there's nothing actionable here and the current behaviour is intentional.