JS: Fix multiple-comments-on-same-line format#9672
Conversation
|
I just realized that there are similar thing for "RemainingComment" Line 322 in ef6a2c5 |
|
The solution looks good, but I want to test it a bit more. |
|
A known issue Prettier pr-9672 --parser babelInput: a;
/*1*/;/*2*/
/*3*/
b;Output: a; /*2*/
/*1*/ /*3*/
b; |
|
A solution for this known issue might be something like this: if any previous comment for the same |
|
Stable version "remaining comment" can't handle Prettier 2.1.2 --parser babelInput: a;/*1*/ ; /*2*/ b;
a;/*1*/ /*2*/ b;Output: a; /*1*/
/*2*/ b;
a;
/*1*/ /*2*/ b; |
| // Find first comment on the same line | ||
| for (let index = commentIndex - 1; index >= 0; index--) { | ||
| const previousComment = comments[index]; | ||
| if (!previousComment) { |
There was a problem hiding this comment.
Let's add a check here that comment and previousComment have the same precedingNode. Otherwise, slice might return huge strings when the comments are far from each other.
| break; | ||
| } | ||
| if (previous.precedingNode !== precedingNode) { | ||
| continue; |
There was a problem hiding this comment.
| continue; | |
| break; |
Co-authored-by: Georgii Dolzhykov <[email protected]>
|
Prettier pr-9672 --parser babelInput: `${
a
/*1*//*2*/
/*3*/
}`
a
/*1*//*2*/
/*3*/Output: `${
a /*2*/
/*1*/
/*3*/
}`;
a; /*2*/
/*1*/
/*3*/ |
| const { locStart, locEnd } = options; | ||
| let start = locStart(comment); | ||
|
|
||
| if (precedingNode) { |
There was a problem hiding this comment.
I think this condition isn't needed. It's okay if precedingNode is falsy. currentCommentPrecedingNode !== precedingNode would still do what we need (we might want to replace !== with != though).
There was a problem hiding this comment.
#9672 (comment) seems not caused by this part.
a
/*4*//*5*/
/*6*/The trailing comments part print like this
[
{"type":"line-suffix","contents":{"type":"concat","parts":[{"type":"concat","parts":[{"type":"line","hard":true},{"type":"break-parent"}]},"","/*4*/"]}},
{"type":"concat","parts":[" ","/*5*/"]},
{"type":"line-suffix","contents":{"type":"concat","parts":[{"type":"concat","parts":[{"type":"line","hard":true},{"type":"break-parent"}]},"","/*6*/"]}}
]Because of line-suffix, /*1*/ print after /*2*/
There was a problem hiding this comment.
It seems it's a separate issue, I'll try to fix it
There was a problem hiding this comment.
All three comments should be classified as own-line and printed without line-suffix. I thought it didn't happen because of the if (precedingNode) check. Was I wrong?
There was a problem hiding this comment.
They were attached as trailing even without this PR, because there is no followingNode, problem is in printTrailingComment
There was a problem hiding this comment.
I checked. These checks indeed don't make a difference. Those comments are classified as own-line with and without these checks.
Fix #9671
Fix #3532
Fix #7724
docs/directory)changelog_unreleased/*/pr-XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨