question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keep comments that are near an `if`’s condition closer to that condition

See original GitHub issue
The original Prettier 1.13.6 Playground link

Prettier 1.13.6 Playground link

--parser babylon
--trailing-comma false

Prettier 2.1.2 Playground link

--parser babel

Input:

if (foo) { // foo may not exist
  doThing(foo);
}

if (foo)
// foo may not exist
{
  doThing(foo);
}

if (foo) /* foo may not exist */ {
  doThing(foo);
}

Output:

if (foo) {
  // foo may not exist
  doThing(foo);
}

if (foo) {
  // foo may not exist
  doThing(foo);
}

if (foo) {
  /* foo may not exist */ doThing(foo);
}

In the current output, the comments are moved into the block after the if.

As for the desired output, I’m not sure in each of the three cases where the comments should go. Either they shouldn’t be moved:

if (foo) { // foo may not exist
  doThing(foo);
}

if (foo)
// foo may not exist
{
  doThing(foo);
}

if (foo) /* foo may not exist */ {
  doThing(foo);
}

Or they should be moved before the if, rather than into the block after the if:

// foo may not exist
if (foo) {
  doThing(foo);
}

// foo may not exist
if (foo) {
  doThing(foo);
}

/* foo may not exist */ if (foo) {
  doThing(foo);
}

I think the second case has a higher likelihood of deserving to be moved than the first and third cases.

Related issue

Pull request #672 “Stabilize comments inside of if/then/else before {” made a change related to the last of the three cases, but it simply made the current output stable, rather than changing what that output was.

Similar cases with acceptable output

For completeness, these similar cases are formatted in a way I am okay with, though it’s possible others would want the comment placement to always be preserved:

The original Prettier 1.13.6 Playground link

Prettier 1.13.6 Playground link

--parser babylon
--trailing-comma false

Prettier 2.1.2 Playground link

--parser babel

Input:

if (foo) // foo may not exist
{
  doThing(foo);
}

if /* foo may not exist */ (foo) {
  doThing(foo);
}

/* foo may not exist */ if (foo) {
  doThing(foo);
}

Output:

if (foo) {
  // foo may not exist
  doThing(foo);
}

if (/* foo may not exist */ foo) {
  doThing(foo);
}

/* foo may not exist */ if (foo) {
  doThing(foo);
}

Scope of this issue

All of these examples also apply to constructs other than if, such as while and for, that can have comments next to their conditions.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:25
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
wagerfieldcommented, Apr 5, 2017

Not sure if this has been discussed elsewhere, but when adding comments above else if and else statements, they are indented to the same level as the previous block rather than to where the closing bracket is:

let result

// Comment 1
if (something) {
  result = 1

// Comment 2
} else if (somethingElse) {
  result = 2

// Comment 3
} else {
  result = 3
}

…becomes

let result

// Comment 1
if (something) {
  result = 1

  // Comment 2
} else if (somethingElse) {
  result = 2

  // Comment 3
} else {
  result = 3
}

I feel like // Comment 2 and // Comment 3 should be at the same indentation level as // Comment 1

2reactions
MarkKahncommented, Aug 10, 2022

Is August, 2022, still looking for a solution…

Read more comments on GitHub >

github_iconTop Results From Across the Web

IFS function - Microsoft Support
The IFS function checks whether one or more conditions are met, and returns a value that corresponds to the first TRUE condition. IFS...
Read more >
Use the new Excel IFS function instead of nested IF
The IFS function in Excel shows whether one or more conditions are observed and returns a value that meets the first TRUE condition....
Read more >
How To Use the IFS Function in Excel (With Examples) - Indeed
This function can evaluate multiple conditions and return a specified value when it determines a true condition. IFS functions are more ...
Read more >
IFS function - Google Docs Editors Help
Evaluates multiple conditions and returns a value that corresponds to the first true condition. Sample Usage IFS(A1>90, "A", A1>80, "B", A1>70, ...
Read more >
Excel IFS Function – How To Use
The IFS function checks the first condition – if 93<60, which turns out to be FALSE. When this condition is found to be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found