yaml icon indicating copy to clipboard operation
yaml copied to clipboard

Multi line values formatting is not preserved when parsing and stringifying documents

Open pksunkara opened this issue 1 year ago • 1 comments

Running parseDocument(content).toString() on the following:

run: aws cloudfront create-invalidation
  --distribution-id ABCDEF
  --paths '/index.html'

is changing it to:

run: aws cloudfront create-invalidation --distribution-id ABCDEF --paths
  '/index.html'

Expected behaviour

The original value should be preserved exactly as it is.

Versions (please complete the following information):

yaml and node latest

pksunkara avatar Sep 02 '24 03:09 pksunkara

The newlines in the plain scalar are collapsed into spaces, so they're lost in the parsing. They are of course in the CST, but retaining them for Document stringification as well could be nice, though tricky to do without impacting users who don't care about them.

Preserving multi-line values "exactly as it is" is also rather tricky, as the structure around the node can change, and it can become more or less indented as a result.

There could be something in the stringifier that might check the srcToken value (the node's CST token, retained if keepSourceTokens: true is set), re-parse that to see if it matches the current value, and if so, determine a common indent to remove from each line past the first, and then use that in the serialization. There probably are a bunch of corner cases here that I'm not thinking of right away, but I'd be happy for someone to look into this.

eemeli avatar Sep 02 '24 13:09 eemeli