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

我正在尝试使用StandardJS来帮助我的棉绒(也是因为我的上司要求我这样做)。在大多数情况下,它的性能都很好。但是,今天我开始遇到以前从未见过的错误,并报告:

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

这是导致错误的代码:

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

为什么会出现此错误,如何解决?

注意,我在此项目上使用StandardJS 8.6.0。该错误是由Standard在项目编译中以及在安装了StandardJS扩展的VS Code IDE中产生的。 (是的,我确实确保所有其他花括号都在正确的位置!)

gillettcool 回答:使用StandardJS,仅在“ else”语句上的大括号处出现错误

就像错误提示一样,您需要将右大括号放在else之后的后续代码行的同一行:

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

来自the docs中有关标准棉绒的示例:

  

将else语句与其大括号保持在同一行。

     

夹板:大括号样式

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

// ✗ avoid
if (condition) {
  // ...
}
else {
  // ...
}
本文链接:https://www.f2er.com/3161680.html

大家都在问