Fix unstable assignment when a member chain object has a leading comment - #19882
Open
Kjubikstronk wants to merge 3 commits into
Open
Fix unstable assignment when a member chain object has a leading comment#19882Kjubikstronk wants to merge 3 commits into
Kjubikstronk wants to merge 3 commits into
Conversation
A comment on the object of a member chain is printed before the whole chain, so after one format it leads the right-hand side of the assignment. The next format then saw a leading own-line comment, picked break-after-operator, and indented what the first pass had not. The layout now looks down the member chain for that comment, so the first format already produces what the second one would.
β Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
commit: |
β Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #10851.
A comment on the object of a member chain is printed before the whole chain, because the printer adds the parentheses and
printCommentsthen wraps the result:After one format the comment leads the right-hand side, which is a state the original source was not in:
So the second format takes a different branch β
hasLeadingOwnLineCommentis now true,chooseLayoutreturnsbreak-after-operator, and the value is indented. Two formats, two outputs.chooseLayoutnow looks down the member chain for that comment, so the first format already produces what the second one would:This also puts the
@ts-expect-errorback above the line it suppresses, rather than leaving it stranded after the=.The same shape shows up whenever the object needs parentheses β
as,await, and binary expressions all reproduce it, and all three are covered in the fixture.Worth flagging
The one existing snapshot this changes,
typescript/prettier-ignore/issue-14238.ts, was recording the unstable output:Feeding that text back through
maingives a different result, so that snapshot could not survive a second format either. It now records the stable form.I did not touch the parenthesis/comment ordering in
ast-to-doc.js, which is where the comment actually moves. That looked like a much larger change than the bug warranted, and this fix makes the output stable without it β but if you would rather see it solved there, say so and I will look.Checklist
docs/directory).changelog_unreleased/*/XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.