π Enforce consistent brace style for case clauses.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated 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:
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;
}
}