JavaScript: Prefer a trailing comment when comment on between test and consequent on ternary#8554
JavaScript: Prefer a trailing comment when comment on between test and consequent on ternary#8554sosukesuzuki wants to merge 20 commits intoprettier:mainfrom
Conversation
| for (let i = start; i < end; ++i) { | ||
| if (text.charAt(i) === "?") { |
| if (text.charAt(i) === "?") { | ||
| let hasQuestion = true; | ||
| // Ignore "?" in comments | ||
| for (const comment of comments) { |
There was a problem hiding this comment.
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.
I fixed with using getNextNonSpaceNonCommentCharacterIndexWithStartIndex for now. We should replace it with using token in future.
|
Prettier pr-8554 --parser babelInput: 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 babelInput: (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 babelInput: 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 babelInput: (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 babelInput: 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); |
fisker
left a comment
There was a problem hiding this comment.
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 babelInput: 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 babelInput: 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 babelInput: test ?// comment
foo
: bar;Output: test // comment
? foo
: bar; |
|
@thorn0 @evilebottnawi Could you review? |
|
@sosukesuzuki I'm really sorry for being slow with reviewing. I'll get to this ASAP. |
|
My main concerns are Prettier pr-8554 --parser babelInput: 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.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨