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

Improve printing of if/while/for whose body isn't a block #8785

Open
thorn0 opened this issue Jul 17, 2020 · 4 comments
Open

Improve printing of if/while/for whose body isn't a block #8785

thorn0 opened this issue Jul 17, 2020 · 4 comments
Labels
lang:javascript Issues affecting JS status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:enhancement A potential new feature to be added, or an improvement to how we print something

Comments

@thorn0
Copy link
Member

thorn0 commented Jul 17, 2020

Prior discussion: #4665, #3049

Prettier 2.0.5
Playground link

--parser babel

Input:

if (askTrovenaBeenaDependsRowans) return

if (bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans) return

if (bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans && 
glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl) return

Output:

if (askTrovenaBeenaDependsRowans) return;

if (bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans)
  return;

if (
  bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans &&
  glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl
)
  return;

Proposed:

if (askTrovenaBeenaDependsRowans) return;

if (
  bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans
) return;

if (
  bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans &&
  glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl
) return;
@thorn0 thorn0 added lang:javascript Issues affecting JS type:enhancement A potential new feature to be added, or an improvement to how we print something status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels Jul 17, 2020
@fisker
Copy link
Member

fisker commented Jul 18, 2020

I prefer current one.

@thorn0
Copy link
Member Author

thorn0 commented Jul 18, 2020

Dangling indented statements are bad. Of course, Prettier protects us from the "goto fail bug" by reformatting on save, but what if somebody for some reason edits code without using Prettier? The proposed formatting will help in that case.

@pvdz
Copy link

pvdz commented Jul 18, 2020

if (bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans && 
glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl)  /* hello world */
  callStuff();

And then there were two indented lines. Looks like a block

if (
  bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans &&
  glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl
)
  /* hello world */
  callStuff();

And then somebody wants to clarfiy a few things

if (
  bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans &&
  glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl
)
  /* hello world */
  // The above is supposed to do a bunch of things
  // Also it's not supposed to do this or that
  // TODO: clean this up
  repeat();

And here comes your coworker, six months later, needing to do add some functionality

if (
  bifornCringerMoshedPerplexSawder(true) > askTrovenaBeenaDependsRowans &&
  glimseGlyphsHazardNoopsTieTie() === averredBathersBoxroomBuggyNurl
)
  /* helllo world */
  // The above is supposed to do a bunch of things
  // Also it's not supposed to do this or that
  functionlity.added();
  repeat();

I think @thorn0 's proposal is an elegant solution and highly preferred over an indented sub-statement that is not in a block.

I'd love for this to be an option, if not the default.

@thorn0
Copy link
Member Author

thorn0 commented Jul 18, 2020

@pvdz But where will those comments end up if this proposal gets implemented? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:javascript Issues affecting JS status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:enhancement A potential new feature to be added, or an improvement to how we print something
Projects
None yet
Development

No branches or pull requests

3 participants