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

Comments trailing YAML keys are moved #602

Open
DavidWells opened this issue Feb 4, 2025 · 2 comments
Open

Comments trailing YAML keys are moved #602

DavidWells opened this issue Feb 4, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@DavidWells
Copy link

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 DavidWells added the bug Something isn't working label Feb 4, 2025
@DavidWells
Copy link
Author

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

@eemeli
Copy link
Owner

eemeli commented Feb 5, 2025

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.

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