Skip to content

Tidy Code does not indent nested blocks properly if braces are missing #1815

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

Closed
Hari3 opened this issue Mar 16, 2021 · 3 comments
Closed

Tidy Code does not indent nested blocks properly if braces are missing #1815

Hari3 opened this issue Mar 16, 2021 · 3 comments

Comments

@Hari3
Copy link

Hari3 commented Mar 16, 2021

Nature of issue? Bug

  • Found a bug

Details about the bug:

  • Web browser and version: Chrome 89.0.4389.82
  • Operating System: Windows 10
  • Steps to reproduce this bug:
  1. Create a nested block(branch or loop), inside another branch or loop without using braces anywhere
    a)Check this Sample editor file
    Here are the contents for ease of access
// Indent Issue
// Create a copy and Tidy Code (Edit -> Tidy Code)
// This is already indented properly
do
  if(true)
    console.log("foo")
  else
    console.log("bar")
while(false)
  
// Same is true for while and for loops:

for(i=0;i<10;i++)
  if(true)
    console.log("foo")
  else
    console.log("bar")

while(i<12)
  if(true)
    i++
  else
    i--
  
console.log("foo")

// Note that out put is same in before and after tiding code, "foo" is printed 12 times
  1. Click on Edit > Tidy Code
    a. Expected, this code is already indented properly: if and else lines up. No change should occur
    b. Actual: else gets aligned with previous block(while, do or for)
@welcome
Copy link

welcome bot commented Mar 16, 2021

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.

@Hari3 Hari3 changed the title Tidy Code does not indent nested block if braces are missing Tidy Code does not indent nested blocks properly if braces are missing Mar 16, 2021
@satyasaibhushan
Copy link
Contributor

satyasaibhushan commented Mar 16, 2021

Hey @Hari3,
This will be resolved by the PR #1788. And will be reflected in the website whenever the next release happens.
So, no worries :)

@catarak
Copy link
Member

catarak commented Mar 16, 2021

Thanks for reporting! I just checked if this will indent in your expected way with the merge of #1788, and it still puts them on the same line.

I think this is actually a matter of code style, and both prettier and beautify-js enforce that if/else statements without curly braces should be on one line. I personally agree with this rule and think it should be enforced—I rarely use if/else statements without curly braces because I think it can lead to bugs if you need to update the logic in the future and want to change the body of the if/else statements to have multiple lines.

I found some discussion in the prettier repository about this that seems to be unresolved: prettier/prettier#3049

@catarak catarak closed this as completed Mar 16, 2021
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

No branches or pull requests

3 participants