Skip to content
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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

sosukesuzuki
Copy link
Member

@sosukesuzuki sosukesuzuki commented Jun 14, 2020

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

--parser babel

Input:

test
  ? first
  // comment
  : second

Output:

test
  ? first
  : // comment
    second;
  • I’ve added tests to confirm my change works.
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@sosukesuzuki sosukesuzuki marked this pull request as ready for review June 21, 2020 10:17
Comment on lines 959 to 960
for (let i = start; i < end; ++i) {
if (text.charAt(i) === "?") {
Copy link
Sponsor Member

@fisker fisker Jun 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text.indexOf('?', start)

if (text.charAt(i) === "?") {
let hasQuestion = true;
// Ignore "?" in comments
for (const comment of comments) {
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use

function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
?

BTW: we really should use token for this kind of stuff #8529

Copy link
Member Author

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!

Copy link
Member Author

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.

@fisker
Copy link
Sponsor Member

fisker commented Jun 21, 2020

Prettier pr-8554
Playground link

--parser babel

Input:

test // comment ?
? 
// comment
foo :  bar;

test // comment 
? 
// comment
foo :  bar;

Output:

test // comment ?
  // comment
  ? foo
  : bar;

test // comment
  ? // comment
    foo
  : bar;

@fisker
Copy link
Sponsor Member

fisker commented Jun 26, 2020

I guess the token you found is )

Prettier pr-8554
Playground link

--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;

@fisker
Copy link
Sponsor Member

fisker commented Jun 26, 2020

Blank line matters?

Prettier pr-8554
Playground link

--parser babel

Input:

test ?
// comment

foo:  bar;

test ?
// comment
foo :  bar;

Output:

test
  ? // comment

    foo
  : bar;

test
  ? // comment
    foo
  : bar;

@fisker
Copy link
Sponsor Member

fisker commented Jun 26, 2020

I think you need keep looking for ?

Prettier pr-8554
Playground link

--parser babel

Input:

(test) ?
// comment
foo :  bar;

test ?
// comment
foo :  bar;

Output:

test
  // comment
  ? foo
  : bar;

test
  ? // comment
    foo
  : bar;

@sosukesuzuki
Copy link
Member Author

@fisker

Blank line matters?

I'm not sure but maybe yes. The behavior also happens on current Prettier and other operator (e.g. BinaryExpression)

Prettier 2.0.5
Playground link

--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;

@sosukesuzuki
Copy link
Member Author

I think you need keep looking for ?

I tried to fix by 776f858 with recursion.

@fisker
Copy link
Sponsor Member

fisker commented Jun 27, 2020

Why not a loop

@lydell
Copy link
Member

lydell commented Jun 28, 2020

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);

Copy link
Sponsor Member

@fisker fisker left a 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 😄

@sosukesuzuki
Copy link
Member Author

@lydell

I might misunderstand #2076, but isn’t the issue about not having trailing comments? I.e. this should stay as-is:

Sorry I cannot understand you said...

(This PR partially fixes the example you showed. I'll also fix the problem about comments before : after this PR is merged.)

Prettier pr-8554
Playground link

--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);

src/language-js/comments.js Outdated Show resolved Hide resolved
@fisker
Copy link
Sponsor Member

fisker commented Jun 29, 2020

@sosukesuzuki Is this expected? I tried your version of that loop, it's the same.

Prettier pr-8554
Playground link

--parser babel

Input:

test ?// comment
  foo
  : bar;

test 
?// comment
  foo
  : bar;

Output:

test // comment
  ? foo
  : bar;

test
  ? // comment
    foo
  : bar;

@sosukesuzuki
Copy link
Member Author

Thank you for your fix.
I think second example is expected, but first example result is expected as:

test
  ? // comment
    foo
  : bar;

I'll look into this.

@sosukesuzuki
Copy link
Member Author

@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?
Prettier 2.0.5
Playground link

--parser babel

Input:

test ?// comment
  foo
  : bar;

Output:

test // comment
  ? foo
  : bar;

@sosukesuzuki
Copy link
Member Author

@thorn0 @evilebottnawi Could you review?

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good

@thorn0
Copy link
Member

thorn0 commented Jul 10, 2020

@sosukesuzuki I'm really sorry for being slow with reviewing. I'll get to this ASAP.

@thorn0
Copy link
Member

thorn0 commented Jul 13, 2020

My main concerns are prettier-ignore comments and that shouldIndentComment and shouldDedentComment don't look reusable/universal enough to be added to the printer API. As for prettier-ignore, here's what I mean:

Prettier pr-8554
Playground link

--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 (willPrintOwnComments and printComments)?

Base automatically changed from master to main January 23, 2021 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comment inside multiline ternary moves after ?
5 participants