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
JavaScript: Prefer a trailing comment when comment on between test and consequent on ternary #8554
base: main
Are you sure you want to change the base?
Conversation
src/language-js/comments.js
Outdated
for (let i = start; i < end; ++i) { | ||
if (text.charAt(i) === "?") { |
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.
text.indexOf('?', start)
src/language-js/comments.js
Outdated
if (text.charAt(i) === "?") { | ||
let hasQuestion = true; | ||
// Ignore "?" in comments | ||
for (const comment of comments) { |
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.
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'll consider it.
BTW: we really should use token for this kind of stuff #8529
Using token
sounds good!
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 fixed with using getNextNonSpaceNonCommentCharacterIndexWithStartIndex
for now. We should replace it with using token
in future.
Prettier pr-8554 --parser babel Input: test // comment ?
?
// comment
foo : bar;
test // comment
?
// comment
foo : bar; Output: test // comment ?
// comment
? foo
: bar;
test // comment
? // comment
foo
: bar; |
I guess the token you found is Prettier pr-8554 --parser babel Input: (await test)
// comment
?
foo : bar;
(test)
// comment
?
foo : bar;
test
// comment
?
foo : bar; Output: (await test)
? // comment
foo
: bar;
test
? // comment
foo
: bar;
test
// comment
? foo
: bar; |
Blank line matters? Prettier pr-8554 --parser babel Input: test ?
// comment
foo: bar;
test ?
// comment
foo : bar; Output: test
? // comment
foo
: bar;
test
? // comment
foo
: bar; |
I think you need keep looking for Prettier pr-8554 --parser babel Input: (test) ?
// comment
foo : bar;
test ?
// comment
foo : bar; Output: test
// comment
? foo
: bar;
test
? // comment
foo
: bar; |
I'm not sure but maybe yes. The behavior also happens on current Prettier and other operator (e.g. Prettier 2.0.5 --parser babel Input: test ?
// comment
foo: bar;
test ?
// comment
foo : bar;
a +
// comment
a;
a +
// comment
a; Output: test
? // comment
foo
: bar;
test
? // comment
foo
: bar;
a +
// comment
a;
a +
// comment
a; |
I tried to fix by 776f858 with recursion. |
Why not a loop |
I might misunderstand #2076, but isn’t the issue about not having trailing comments? I.e. this should stay as-is: this.chromeInstances = CONSTANTS.paralleliseChrome
// I am running in paralel, add a chromeLauncher.launch() command for each required
// instance of Chrome to run in paralell
? await Promise.all([
chromeLauncher.launch(chromeLauncherOptions),
chromeLauncher.launch(chromeLauncherOptions),
chromeLauncher.launch(chromeLauncherOptions)
])
// Every test run will be in the same instance of Chrome
: await chromeLauncher.launch(chromeLauncherOptions); |
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.
Looks good, but I don't like adding options.printer.shouldIndentComment
and I don't have a better idea 😄
Sorry I cannot understand you said... (This PR partially fixes the example you showed. I'll also fix the problem about comments before Prettier pr-8554 --parser babel Input: this.chromeInstances = CONSTANTS.paralleliseChrome
// I am running in paralel, add a chromeLauncher.launch() command for each required
// instance of Chrome to run in paralell
? await Promise.all([
chromeLauncher.launch(chromeLauncherOptions),
chromeLauncher.launch(chromeLauncherOptions),
chromeLauncher.launch(chromeLauncherOptions)
])
// Every test run will be in the same instance of Chrome
: await chromeLauncher.launch(chromeLauncherOptions); Output: this.chromeInstances = CONSTANTS.paralleliseChrome
// I am running in paralel, add a chromeLauncher.launch() command for each required
// instance of Chrome to run in paralell
? await Promise.all([
chromeLauncher.launch(chromeLauncherOptions),
chromeLauncher.launch(chromeLauncherOptions),
chromeLauncher.launch(chromeLauncherOptions),
])
: // Every test run will be in the same instance of Chrome
await chromeLauncher.launch(chromeLauncherOptions); |
@sosukesuzuki Is this expected? I tried your version of that loop, it's the same. Prettier pr-8554 --parser babel Input: test ?// comment
foo
: bar;
test
?// comment
foo
: bar; Output: test // comment
? foo
: bar;
test
? // comment
foo
: bar; |
Thank you for your fix. test
? // comment
foo
: bar; I'll look into this. |
@fisker Stable Prettier also formats like you showed. I want to keep PRs small as possible as so can I fix this problem in other PR? --parser babel Input: test ?// comment
foo
: bar; Output: test // comment
? foo
: bar; |
@thorn0 @evilebottnawi Could you review? |
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.
Looks very good
@sosukesuzuki I'm really sorry for being slow with reviewing. I'll get to this ASAP. |
My main concerns are Prettier pr-8554 --parser babel Input: a=foo&& bar
// prettier-ignore
? baz +quux : 20; Output: a =
foo&& bar
// prettier-ignore
? baz + quux
: 20; We had a similar problem (#7798) in union types where comments are attached as trailing too, for the same reasons. Can we probably reuse the way comments are printed in union types ( |
Fixes #2076. this is re-implementation of #6418.
The below case is out of this PR scope, I'll work on it after this PR is merged(#8709):
Prettier 2.0.5
Playground link
Input:
Output:
changelog_unreleased/*/pr-XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨