6

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!)

Steverino
  • 2,099
  • 6
  • 26
  • 50

2 Answers2

20

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 {
  // ...
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • 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`. – VLAZ Nov 05 '19 at 07:09
  • 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. – Steverino Nov 05 '19 at 07:22
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
  }
Pasindu Perera
  • 489
  • 3
  • 8