]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/semi-spacing.md
01b3c3ffb0d50bddd5967db0fcecde3c20e8573e
[pve-eslint.git] / eslint / docs / src / rules / semi-spacing.md
1 ---
2 title: semi-spacing
3 layout: doc
4 rule_type: layout
5 related_rules:
6 - semi
7 - no-extra-semi
8 - comma-spacing
9 - block-spacing
10 - space-in-parens
11 ---
12
13
14
15 JavaScript allows you to place unnecessary spaces before or after a semicolon.
16
17 Disallowing or enforcing space around a semicolon can improve the readability of your program.
18
19 ```js
20 var a = "b" ;
21
22 var c = "d";var e = "f";
23 ```
24
25 ## Rule Details
26
27 This rule aims to enforce spacing around a semicolon. This rule prevents the use of spaces before a semicolon in expressions.
28
29 This rule doesn't check spacing in the following cases:
30
31 * The spacing after the semicolon if it is the first token in the line.
32
33 * The spacing before the semicolon if it is after an opening parenthesis (`(` or `{`), or the spacing after the semicolon if it is before a closing parenthesis (`)` or `}`). That spacing is checked by `space-in-parens` or `block-spacing`.
34
35 * The spacing around the semicolon in a for loop with an empty condition (`for(;;)`).
36
37 ## Options
38
39 The rule takes one option, an object, which has two keys `before` and `after` having boolean values `true` or `false`.
40 If `before` is `true`, space is enforced before semicolons and if it's `false`, space is disallowed before semicolons.
41 If `after` is `true`, space is enforced after semicolons and if it's `false`, space is disallowed after semicolons.
42 The `after` option will be only applied if a semicolon is not at the end of line.
43
44 The default is `{"before": false, "after": true}`.
45
46 ```json
47 "semi-spacing": ["error", {"before": false, "after": true}]
48 ```
49
50 ### `{"before": false, "after": true}`
51
52 This is the default option. It enforces spacing after semicolons and disallows spacing before semicolons.
53
54 Examples of **incorrect** code for this rule:
55
56 ::: incorrect
57
58 ```js
59 /*eslint semi-spacing: "error"*/
60
61 var foo ;
62 var foo;var bar;
63 throw new Error("error") ;
64 while (a) { break ; }
65 for (i = 0 ; i < 10 ; i++) {}
66 for (i = 0;i < 10;i++) {}
67 ```
68
69 :::
70
71 Examples of **correct** code for this rule:
72
73 ::: correct
74
75 ```js
76 /*eslint semi-spacing: "error"*/
77
78 var foo;
79 var foo; var bar;
80 throw new Error("error");
81 while (a) { break; }
82 for (i = 0; i < 10; i++) {}
83 for (;;) {}
84 if (true) {;}
85 ;foo();
86 ```
87
88 :::
89
90 ### `{"before": true, "after": false}`
91
92 This option enforces spacing before semicolons and disallows spacing after semicolons.
93
94 Examples of **incorrect** code for this rule with the `{"before": true, "after": false}` option:
95
96 ::: incorrect
97
98 ```js
99 /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
100
101 var foo;
102 var foo ; var bar;
103 throw new Error("error");
104 while (a) { break; }
105 for (i = 0;i < 10;i++) {}
106 for (i = 0; i < 10; i++) {}
107 ```
108
109 :::
110
111 Examples of **correct** code for this rule with the `{"before": true, "after": false}` option:
112
113 ::: correct
114
115 ```js
116 /*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
117
118 var foo ;
119 var foo ;var bar ;
120 throw new Error("error") ;
121 while (a) {break ;}
122 for (i = 0 ;i < 10 ;i++) {}
123 ```
124
125 :::
126
127 ## When Not To Use It
128
129 You can turn this rule off if you are not concerned with the consistency of spacing before or after semicolons.