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

Expected { after 'if' condition even with single lines #718

Closed
jonaswindey opened this issue Dec 13, 2016 · 5 comments

Comments

@jonaswindey
Copy link

commented Dec 13, 2016

Using standard@8.6.0, the following code fails:

const test = false
if (test)
  console.log(false)
else
  console.log(true)

Error:

  /Users/jonas/Development/sandbox/test/test.js:2:1: Expected { after 'if' condition.
  /Users/jonas/Development/sandbox/test/test.js:4:1: Expected { after 'else'.

whereas this one works:

const test = false
if (test) console.log(false)
else console.log(true)

Could be related to #664 , but the issue has been closed and this issue still exists

This makes it difficult to use single-line if blocks when they are too long to fit on the same line.

@LinusU

This comment has been minimized.

Copy link
Member

commented Dec 13, 2016

I think this is intentional, I at least prefer this behaviour

@jonaswindey

This comment has been minimized.

Copy link
Author

commented Dec 13, 2016

The error says Expected { after 'if' condition.

If I add the curly brackets like this:

const test = false
if (test) {
  console.log(false)
}
else {
  console.log(true)
}

It says there's an unnecessary { after if (due to being a single line)

This is confusing to say the least.

What do you do then when you have a single line after an if condition that is too long and you want to put it on the next line?

@LinusU

This comment has been minimized.

Copy link
Member

commented Dec 13, 2016

I think that it's like so,

single line:

if (thisIsAll) onOneLine()

multiple lines:

if (thisIsActually)
  twoLine()

What do you do then when you have a single line after an if condition that is too long and you want to put it on the next line?

You put brackets around it

@dcousens

This comment has been minimized.

Copy link
Member

commented Dec 13, 2016

@jonaswindey

Single line

const test = false
if (test) console.log(false)
else console.log(true)

Multi line

const test = false
if (test) {
  console.log(false)
} else {
  console.log(true)
}

As you pointed out, other [non-standard] formatting of these statements will throw respective errors.

@dcousens dcousens closed this Dec 13, 2016

@dcousens dcousens added the question label Dec 13, 2016

@jonaswindey

This comment has been minimized.

Copy link
Author

commented Dec 14, 2016

Ok, I guess that rule changed than? I remember that

const test = false
if (test) {
  console.log(false)
} else {
  console.log(true)
}

Gave Unnecessary { after 'if' condition. because it only contained one line (or other ifs)

Thanks for clarifying

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.