eemeli / yaml

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

Array style choice is not preserved between parse and stringify #570

Open pksunkara opened 3 weeks ago

pksunkara commented 3 weeks ago

Describe the bug When I am parsing a YAML and stringifying it afterward, the style of 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 weeks ago

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

pksunkara commented 3 weeks ago

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

pksunkara commented 3 weeks 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 2 weeks ago

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

eemeli commented 2 weeks ago

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

pksunkara commented 2 weeks 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 2 weeks 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.