Skip to content

Latest commit

Β 

History

History
97 lines (79 loc) Β· 1.7 KB

File metadata and controls

97 lines (79 loc) Β· 1.7 KB

switch-case-braces

πŸ“ 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.

  1. Forbid braces for empty clauses.
  2. Enforce braces for non-empty clauses.

Examples

// ❌
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;
	}
}

Options

Type: string
Default: 'always'

  • 'always' (default)
    • Always report when clause is not a BlockStatement.
  • '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;
	}
}