Skip to content

Latest commit

 

History

History
87 lines (70 loc) · 1.44 KB

switch-case-braces.md

File metadata and controls

87 lines (70 loc) · 1.44 KB

Enforce consistent brace style for case clauses

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

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

Fail

switch (foo) {
	case 1: {
	}
	case 2: {
		doSomething();
		break;
	}
}
switch (foo) {
	case 1:
		doSomething();
		break;
}

Pass

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