millermedeiros / esformatter

ECMAScript code beautifier/formatter
MIT License
970 stars 91 forks source link

Standalone Block opening/closing brace needs Linebreak and block statements needs indentation #430

Closed Vasanthi-S closed 8 years ago

Vasanthi-S commented 8 years ago

Hi,

Summary of issue: Standalone block not formatted properly, missing line-breaks and indentations, Creates a erroneous code.

When I have a standalone block with a set of statements, It would be great if it formatted with a line-break after/before block opening/closing brace. Also the statements inside the block needs one-level indentation for readability. Because of the missing line-break, formatter creates a erroneous code output.

Input:

function a() { var arr2 = [] }
var x = 1
{
var x = 2
console.log(x) }

Actual output: - formatter creates errorenous code

function a() {
  var arr2 = []
}
var x = 1 {        
var x = 2
console.log(x)   }

After I set the property indent.BlockStatement=1,I get the indentation working (as shown below) With block indentation working:

function a() {
    var arr2 = []
}
var x = 1 {
  var x = 2                  // indented correctly
  console.log(x)   }         // indented correctly

So in order to get the line-break working, I tried setting lineBreak.before.BlockStatement=1 and lineBreak.after.BlockStatement=1. But do not see linebreaks appearing for the standalone blocks.

EDIT: @millermedeiros inlined the gist to make it easier to understand.

millermedeiros commented 8 years ago

we currently don't handle BlockStatement properly since it ends up being part of many other constructs (like FunctionDeclaration.body):

function foo() {
  // this is also a BlockStatement!
}

To handle it properly we would need to check if the parent node is not a function/do/while/arrowFunction/for/forOf before we add the indent or line breaks.

It is doable (should not be that hard) and is actually something that we need to address, thanks for reporting it!

Vasanthi-S commented 8 years ago

Thanks for the update. Would be great to know an approximate timeline when this could be fixed.