Using StandardJS, getting error for curly brace on wrong line only for 'else' statements
Asked Answered
S

2

8

I'm trying to use StandardJS to help with my linting (also because my supervisor asked me to). It is working great for the most part. However, today I started getting errors I haven't seen before, reporting that:

Closing curly brace does not appear on the same line as the subsequent block. (brace-style)
standard(brace-style)

This is the code causing the error:

if (err.status === 'not found') {
  cb({ statusCode: 404 })
  return
}
else {  // THIS LINE causes the error
  cb({ statusCode: 500 })
  return
}

Why might I be getting this error, and how do I prevent it?

Note, I'm using StandardJS 8.6.0 on this project. The error is being produced both by Standard in project compilation, in in the VS Code IDE with the StandardJS extension installed. (And yes, I really made sure all my other curly braces are in the right places!)

Syracuse answered 5/11, 2019 at 7:4 Comment(0)
W
24

Like the error says, you need to put the closing curly brace on the same line as the subsequent block after the else:

if (err.status === 'not found') {
  cb({ statusCode: 404 })
  return
} else {   // <--- now } is on same line as {
  cb({ statusCode: 500 })
  return
}

From an example from the docs on Standard linting:

Keep else statements on the same line as their curly braces.

eslint: brace-style

// ✓ ok
if (condition) {
  // ...
} else {
  // ...
}

// ✗ avoid
if (condition) {
  // ...
}
else {
  // ...
}
Wootan answered 5/11, 2019 at 7:5 Comment(2)
This is correct. In fact, I tried it in their online online demo and the error was on the previous line - the closing } for the if.Averroism
I thought it meant the closing curly brace of the else (since the opening curly brace of the else block itself is what was highlighted in the IDE). Their error messaging/highlighting could be more descriptive.Syracuse
M
0

Use below format when you face above error in typescript eslint.

if (Logic1) {
  //content1
  } else if (Logic2) {
  //content2
  } else if (Logic3) {
  //content3
  } else {
   //content4
  }
Martita answered 17/5, 2022 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.