-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
JS: Fix multiple-comments-on-same-line format #9672
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
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 babel Input: 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 babel Input: a;/*1*/ ; /*2*/ b;
a;/*1*/ /*2*/ b; Output: a; /*1*/
/*2*/ b;
a;
/*1*/ /*2*/ b; |
src/main/comments.js
Outdated
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
src/main/comments.js
Outdated
break; | ||
} | ||
if (previous.precedingNode !== precedingNode) { | ||
continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continue; | |
break; |
Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
Prettier pr-9672 --parser babel Input: `${
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems it's a separate issue, I'll try to fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨