]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/switch-colon-spacing.md
b9872c2ad5b20ae55d92404afe0159ff65078cb2
[pve-eslint.git] / eslint / docs / rules / switch-colon-spacing.md
1 # Enforce spacing around colons of switch statements (switch-colon-spacing)
2
3 Spacing around colons improves readability of `case`/`default` clauses.
4
5 ## Rule Details
6
7 This rule controls spacing around colons of `case` and `default` clauses in `switch` statements.
8 This rule does the check only if the consecutive tokens exist on the same line.
9
10 This rule has 2 options that are boolean value.
11
12 ```json
13 {
14 "switch-colon-spacing": ["error", {"after": true, "before": false}]
15 }
16 ```
17
18 - `"after": true` (Default) requires one or more spaces after colons.
19 - `"after": false` disallows spaces after colons.
20 - `"before": true` requires one or more spaces before colons.
21 - `"before": false` (Default) disallows before colons.
22
23 Examples of **incorrect** code for this rule:
24
25 ```js
26 /*eslint switch-colon-spacing: "error"*/
27
28 switch (a) {
29 case 0 :break;
30 default :foo();
31 }
32 ```
33
34 Examples of **correct** code for this rule:
35
36 ```js
37 /*eslint switch-colon-spacing: "error"*/
38
39 switch (a) {
40 case 0: foo(); break;
41 case 1:
42 bar();
43 break;
44 default:
45 baz();
46 break;
47 }
48 ```
49
50 Examples of **incorrect** code for this rule with `{"after": false, "before": true}` option:
51
52 ```js
53 /*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/
54
55 switch (a) {
56 case 0: break;
57 default: foo();
58 }
59 ```
60
61 Examples of **correct** code for this rule with `{"after": false, "before": true}` option:
62
63 ```js
64 /*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/
65
66 switch (a) {
67 case 0 :foo(); break;
68 case 1 :
69 bar();
70 break;
71 default :
72 baz();
73 break;
74 }
75 ```
76
77 ## When Not To Use It
78
79 If you don't want to notify spacing around colons of switch statements, then it's safe to disable this rule.