]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/switch-colon-spacing.md
bd469ea779100d1a4c7a4845f8576082232abe61
[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
24 Examples of **incorrect** code for this rule:
25
26 ```js
27 /*eslint switch-colon-spacing: "error"*/
28
29 switch (a) {
30 case 0 :break;
31 default :foo();
32 }
33 ```
34
35 Examples of **correct** code for this rule:
36
37 ```js
38 /*eslint switch-colon-spacing: "error"*/
39
40 switch (a) {
41 case 0: foo(); break;
42 case 1:
43 bar();
44 break;
45 default:
46 baz();
47 break;
48 }
49 ```
50
51 Examples of **incorrect** code for this rule with `{"after": false, "before": true}` option:
52
53 ```js
54 /*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/
55
56 switch (a) {
57 case 0: break;
58 default: foo();
59 }
60 ```
61
62 Examples of **correct** code for this rule with `{"after": false, "before": true}` option:
63
64 ```js
65 /*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/
66
67 switch (a) {
68 case 0 :foo(); break;
69 case 1 :
70 bar();
71 break;
72 default :
73 baz();
74 break;
75 }
76 ```
77
78 ## When Not To Use It
79
80 If you don't want to notify spacing around colons of switch statements, then it's safe to disable this rule.