yaml icon indicating copy to clipboard operation
yaml copied to clipboard

Comments trailing YAML keys are moved

Open DavidWells opened this issue 10 months ago • 2 comments

Describe the bug

Hello there. First off great library! Thank you for making it.

I'm running into an issue with comments and I'm wondering if there is an option to turn it off.

The issue: It looks like inline comments on keys are being moved

Input yaml

tutorial: #nesting level 1
  - yaml: #nesting level 2 (2 spaces used for indentation)
      name: YAML Ain't Markup Language #string [literal] #nesting level 3 (4 spaces used for indentation)
      type: awesome #string [literal]
      born: 2001 #number [literal]

Produces

tutorial:
  #nesting level 1
  - yaml:
      #nesting level 2 (2 spaces used for indentation)
      name: YAML Ain't Markup Language #string [literal] #nesting level 3 (4 spaces used for indentation)
      type: awesome #string [literal]
      born: 2001 #number [literal]

#nesting level 1 and #nesting level 2 (2 spaces used for indentation) have moved

here's the diff

- tutorial: #nesting level 1
+ tutorial:
+   #nesting level 1
- - yaml: #nesting level 2 (2 spaces used for indentation)
+ - yaml:
+     #nesting level 2 (2 spaces used for indentation)
      name: YAML Ain't Markup Language #string [literal] #nesting level 3 (4 spaces used for indentation)
      type: awesome #string [literal]
      born: 2001 #number [literal]

To Reproduce Steps to reproduce the behaviour.

const originalString = `
tutorial: #nesting level 1
  - yaml: #nesting level 2 (2 spaces used for indentation)
      name: YAML Ain't Markup Language #string [literal] #nesting level 3 (4 spaces used for indentation)
      type: awesome #string [literal]
      born: 2001 #number [literal]`
const originalYamlDoc = yaml.parseDocument(originalString.trim());
const outputTest = new yaml.Document(originalYamlDoc)
const outputTestStr = outputTest.toString()
console.log(outputTestStr) // string with comments shifted

Expected behaviour

keep comments on the same line where the key is.

I think I can work around this with some custom mapping but I was wondering if there was an option to toggle to avoid this in the first place.

Thank you again for the great lib ❤️

DavidWells avatar Feb 04 '25 21:02 DavidWells

For some additional context, I was attempting to upgrade my util lib for this from to "yaml": "^1.10.2" to "yaml": "2.7.0"

V1 kept comments in the same spot https://github.com/DavidWells/components/blob/master/packages/util-yaml/src/index.test.js

DavidWells avatar Feb 04 '25 22:02 DavidWells

The challenge here is that the parsed Document representation does not retain the exact source positions of each comment, but only their attachment either on or before a value. In other words, there's no option to toggle this behaviour.

On the other hand, a single-line comment before a block collection within another block collection could well have its comment on the key line, as in your inputs, and I'd be quite willing to consider a PR changing the behaviour. The change would probably need to be applied somewhere around here.

eemeli avatar Feb 05 '25 11:02 eemeli