我想强制执行没有大括号的一行 if 语句。例如:
// incorrect
if(foo) {
bar()
}
// correct
if(foo) bar()
Run Code Online (Sandbox Code Playgroud)
但如果我有 else/else if,我仍然想保留大括号:
// incorrect
if(foo) bar()
else(baz) qux()
// correct
if(foo) {
bar()
} else(baz) {
qux()
}
Run Code Online (Sandbox Code Playgroud)
我能够创建一个像这样的插件:
module.exports = {
meta: {
type: "layout",
fixable: "whitespace",
messages: {
noSingleLineIfsAsBlock: "Don't use braces with one line if statements",
useBlocksWithElseCases: "Use braces when there are else ifs and/or elses"
}
},
create(context) {
return {
IfStatement(node) {
if(node.alternate === null && node.parent.type !== "IfStatement") {
if(node.consequent.type …Run Code Online (Sandbox Code Playgroud)