unicorn/switch-case-braces Style
作用
要求空的 switch 分支省略花括号,而非空分支必须使用花括号。 这可以减少空分支的视觉干扰,并确保非空分支具有正确的作用域。
为什么这不好?
对空分支不必要地使用花括号会增加视觉噪音, 而在非空分支中省略花括号可能导致作用域问题。
示例
以下是此规则的错误代码示例:
javascript
switch (num) {
case 1: {
}
case 2:
console.log("Case 2");
break;
}以下是此规则的正确代码示例:
javascript
switch (num) {
case 1:
case 2: {
console.log("Case 2");
break;
}
}配置示例:
json
"unicorn/switch-case-braces": ["error", "avoid"]配置
此规则接受以下字符串值之一:
"always"
始终要求 case 子句使用花括号(空分支除外)。
"avoid"
仅在需要作用域时允许使用花括号(例如变量或函数声明)。
使用方法
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/switch-case-braces": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/switch-case-braces": "error",
},
});bash
oxlint --deny unicorn/switch-case-braces版本
此规则于 v0.0.15 中添加。
