简体   繁体   English

使用 StandardJS,仅在“else”语句的错误行上出现花括号错误

[英]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).我正在尝试使用 StandardJS 来帮助我进行 linting(也是因为我的主管要求我这样做)。 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.注意,我在这个项目中使用 StandardJS 8.6.0。 The error is being produced both by Standard in project compilation, in in the VS Code IDE with the StandardJS extension installed.该错误是由项目编译中的标准产生的,在安装了 StandardJS 扩展的 VS Code IDE 中。 (And yes, I really made sure all my other curly braces are in the right places!) (是的,我真的确保我所有的其他花括号都在正确的位置!)

Like the error says, you need to put the closing curly brace on the same line as the subsequent block after the else :就像错误所说的那样,您需要将右花括号放在与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:来自标准 linting文档的示例:

Keep else statements on the same line as their curly braces.将 else 语句与花括号放在同一行。

eslint: brace-style eslint:大括号样式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM