Skip to content

Preserve Inline comments#629

Open
DavidWells wants to merge 2 commits intoeemeli:mainfrom
DavidWells:inline-comment-support
Open

Preserve Inline comments#629
DavidWells wants to merge 2 commits intoeemeli:mainfrom
DavidWells:inline-comment-support

Conversation

@DavidWells
Copy link

Fix for inline comments getting removed #602

It's possible there is an easier way to do this, but this appears to work!

There are a couple of skipped cases that do not pass in tests/trailing-key-comments.test.ts

Copy link
Owner

@eemeli eemeli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for not getting to this sooner -- I'd somehow managed to miss the notification about the PR.

I didn't look into the implementation details too much, as I have concerns about the new fields that are being proposed for Node and SourceToken.

My own initial sense is that in something like

key: # comment
  value

we ought to parse the comment as the .comment value of the key, rather than any new field. I'm also very doubtful that this really requires any new fields being added to the CST.

Then when serialising, whether or not the comment contains more than one line of content could be used to pick different behaviour.

Is there a reason why that approach wouldn't work?

Comment on lines +57 to +59
/** A comment after key: */
declare commentAfterKey?: string | null

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new field really necessary? Couldn't the comment value of the key be used instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc it was because there was conflict with comments before the line and on the key. Both were slotting into 'comment' and conflicting

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments before the key ought to go into commentBefore. There is a possibility of comments right after an explicit key but before the ::

? - key
  # comment
: value

but that's really quite rare.

As it happens, assigning a comment on a key appears to already produce the desired output:

let doc = parseDocument('key: value')
doc.contents.items[0].key.comment = 'ccc'
doc.toString()

results in

key: #ccc
  value

So perhaps the problem to fix here is that re-parsing the above result puts the comment as the commentBefore of the value, rather than attaching it as the key comment?

Comment on lines +34 to +38
commentAfterKey?: boolean
commentOnParentKey?: string
parentComment?: string
commentIsAfterKey?: boolean
context?: any
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needing to add this many fields here is almost certainly a mistake. A SourceToken is meant to be a YAML syntax character, and not a vessel for carrying a lot of state.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but I don't remember why I implemented this way now lol. I needed the references downstream and wasn't sure where the best place to do this would be

import { prettyToken, tokenType } from './cst.ts'
import { Lexer } from './lexer.ts'

const DEBUG = false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to be removed before merging, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah artifact of trying to debug the lib to implement this feature

Comment on lines -1151 to +1153
const src = `a: b #c\n#d\n`
const src = source`
a: b #c

#d
`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous test had a single newline before #d, why are there now two?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember 😅

Something about the trailing comment after all key value pairs is adding 2 newlines

@eemeli
Copy link
Owner

eemeli commented Feb 13, 2026

@DavidWells Are you still interested in working on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants