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

Stop including end comments in collections if next content is not sufficiently indented #54

Merged
merged 1 commit into from
Dec 1, 2018

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Nov 28, 2018

Fixes #41

Effectively, when encountering a comment on a new line while parsing a collection, the parser now peeks ahead to see if the next non-comment content is indented at least to the level of this collection, or if there's a subsequent comment that's sufficiently indented. This means that e.g. parsing and re-stringifying this document:

#c0
- #c1
  k1: v1
  #c2
  k2: v2 #c3
#c4
  k3: v3
#c5

Results in this representation:

#c0
#c1
- k1: v1
  #c2
  k2: v2 #c3
  #c4
  k3: v3
#c5

Previously, the #c5 comment would have gained a spurious level of indentation due to being incorrectly attached to the k* map rather than the root sequence.

@ikatyang Did you have some fixes in yaml-unist-parser for this following prettier/prettier#4917 that would need to take into account these changes?

@ikatyang
Copy link
Contributor

Thanks for the notice. Basically, yaml-unist-parser re-attaches every comment (with a few exceptions) so the result should be the same as long as all the comments are collected correctly. I just tried this branch in yaml-unist-parser, all the tests are passed.

I think this kind of comment attaching can be hard for a parser since it needs to be fixed one-by-one, so I didn't raise this sort of issues here. Instead, I apply a postprocessing for comment attaching in yaml-unist-parser, which I just need to define general rules (something like "when A comment is in front of B node, attach A to B") and it'll work for all the cases.

@eemeli eemeli merged commit c5b900b into master Dec 1, 2018
@eemeli eemeli deleted the unindent-end-comments branch December 1, 2018 13:06
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.

Indentation level is not taken into account for trailing comments
2 participants