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