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

Handling of newlines around if/else/if statements #311

Closed
theshoeshiner opened this issue Jul 23, 2013 · 9 comments
Closed

Handling of newlines around if/else/if statements #311

theshoeshiner opened this issue Jul 23, 2013 · 9 comments
Milestone

Comments

@theshoeshiner
Copy link

The handling of an if/else/if block is ambiguous depending on input

With the code on a single line - ie -
if(...){...}else{...}if(...){...}

It will be formatted as such
if (...) {
...
} else {
...
} if (...) {
...
}

Note: No newline between the closure of the else and the second if.
However if there is already a newline after the else{} - ie -
if (...) {...} else {...}
if (...) {...}

Then that newline will be preserved even if preserve newlines is set to false. So the output is -
if (...) {
...
} else {
...
}
if (...) {
...
}

Note: The newline after the else{...}
I dont think this is the expected behavior? With preserve_newlines set to false, the code should always produce the same output (in terms of line breaks), correct?

@bitwiseman
Copy link
Member

@dcwatson84 - "With preserve_newlines set to false, the code should always produce the same output (in terms of line breaks), correct?"

Not quite. preserve_newlines is most accurately described as preserve_extra_newlines. The beautifier may still add newlines.

This is still a bug. The output of the first input should be the same as the second.

The test looks like this:

// input
if(x){a();}else{b();}if(y){c();}

// expected
if (x) {
    a();
} else {
    b();
} 
if (y) {
    c();
}
// actual 
if (x) {
    a();
} else {
    b();
} if (y) {
    c();
}

@bitwiseman bitwiseman added this to the v1.5.2 milestone Sep 29, 2014
@trusktr
Copy link

trusktr commented Jun 14, 2015

@bitwiseman How do we make

if(x){a();}else{b();}

result in

if (x) {
    a();
}
else {
    b();
}

where else is on a new line after the } instead of } else {?

@trusktr
Copy link

trusktr commented Jun 14, 2015

Aha, it seems to be "End braces on own line". Does that correspond to the end-expand brace-style setting? Yes it does.

@trusktr
Copy link

trusktr commented Jun 14, 2015

Confirmed, I just the encountered the bug of this issue, with a if section not being on it's own line.

@bitwiseman
Copy link
Member

So... Bug here or not?

@trusktr
Copy link

trusktr commented Jun 15, 2015

@bitwiseman I'd say it's a bug.

@trusktr
Copy link

trusktr commented Jun 15, 2015

// actual 
if (x) {
    a();
} else {
    b();
} if (y) {
    c();
}

I've never seen anyone code like that intentionally. I would always expect if on a new line, like in both of the following examples:

// brace-style == collapse
if (x) {
    a();
} else {
    b();
}
if (y) {
    c();
}
// brace-style == end-expand
if (x) {
    a();
}
else {
    b();
}
if (y) {
    c();
}

@bitwiseman
Copy link
Member

Okay, I've tried the above and I'm not getting a repro. In all the cases I tried, the if popped to the next line. Could you open a new issue with the specific input/expected/actual and settings? It is likely your is not the same as this one (regression test in place to cover this one).

@trusktr
Copy link

trusktr commented Jun 16, 2015

@bitwiseman I've just tried reproducing, and apparently it doesn't happen all the time. If I encounter it again I'll let you know, but it definitely does happen sometimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants