Skip to content

Latest commit

Β 

History

History
138 lines (114 loc) Β· 2.64 KB

switch-case-braces.md

File metadata and controls

138 lines (114 loc) Β· 2.64 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.

Braces give each non-empty case its own explicit scope and keep case-body structure consistent.

Cases with lexical declarations that are used outside the case are reported without a fix, because adding braces would hide the bindings from the other cases.

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)
    • Require braces for non-empty clauses, and forbid braces for empty clauses.
  • 'avoid'
    • Only allow braces when the block contains a top-level declaration.
  • 'single-statement'
    • Require each case clause to have a single statement. Multiple statements must be wrapped in a block. A block with one statement is allowed only when that statement needs its own 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;
	}
}

The following cases are considered valid:

/* eslint unicorn/switch-case-braces: ["error", "single-statement"] */
function unicorn(foo) {
	switch (foo) {
		case 1:
			return foo;
		case 2: {
			doSomething();
			break;
		}
		case 3: {
			const bar = 2;
		}
	}
}

The following cases are considered invalid:

/* eslint unicorn/switch-case-braces: ["error", "single-statement"] */
function unicorn(foo) {
	switch (foo) {
		case 1:
			doSomething();
			break;
		case 2: {
			return foo;
		}
	}
}