💼 This rule is enabled in the ✅ recommended
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
- Forbid braces for empty clauses.
- Enforce braces for non-empty clauses.
switch (foo) {
case 1: {
}
case 2: {
doSomething();
break;
}
}
switch (foo) {
case 1:
doSomething();
break;
}
switch (foo) {
case 1: {
doSomething();
break;
}
}
Type: string
Default: 'always'
'always'
(default)- Always report when clause is not a
BlockStatement
.
- Always report when clause is not a
'avoid'
- Only allow braces when there are variable declaration or function declaration which requires a scope.
The following cases are considered valid:
// eslint unicorn/switch-case-braces: ["error", "avoid"]
switch (foo) {
case 1:
doSomething();
break;
}
// eslint unicorn/switch-case-braces: ["error", "avoid"]
switch (foo) {
case 1: {
const bar = 2;
doSomething(bar);
break;
}
}
The following case is considered invalid:
// eslint unicorn/switch-case-braces: ["error", "avoid"]
switch (foo) {
case 1: {
doSomething();
break;
}
}