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