Javascript 使用StandardJS,仅在“else”语句的错误行上获取大括号错误
我正试图使用StandardJS来帮助我的线头,也是因为我的主管让我这么做的。它在很大程度上起到了很好的作用。然而,今天我开始发现以前从未见过的错误,报告如下:Javascript 使用StandardJS,仅在“else”语句的错误行上获取大括号错误,javascript,node.js,lint,standardjs,Javascript,Node.js,Lint,Standardjs,我正试图使用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({ st
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。该错误是由安装了StandardJS扩展的VS代码IDE中的标准项目编译中产生的。是的,我真的确定了我所有其他的花括号都在正确的位置 如错误所述,您需要将右大括号放在与else后面的后续块相同的行上:
如错误所述,您需要将右大括号放在与else后面的后续块相同的行上:
这是正确的。事实上,我在他们的在线演示中尝试过,错误出现在前一行——if的closing}。我认为这意味着else的closing花括号,因为else块的closing花括号本身就是IDE中突出显示的。他们的错误消息/突出显示可能更具描述性。这是正确的。事实上,我在他们的在线演示中尝试过,错误出现在前一行——if的closing}。我认为这意味着else的closing花括号,因为else块的closing花括号本身就是IDE中突出显示的。他们的错误消息/突出显示可能更具描述性。
if (err.status === 'not found') {
cb({ statusCode: 404 })
return
} else { // <--- now } is on same line as {
cb({ statusCode: 500 })
return
}
// ✓ ok
if (condition) {
// ...
} else {
// ...
}
// ✗ avoid
if (condition) {
// ...
}
else {
// ...
}