javascriptnode.jslintstandardjs

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


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


Solution

  • 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 {
      // ...
    }