]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/padding-line-between-statements.md
2f2f67ee07cece632f315204626fbe2aee083010
[pve-eslint.git] / eslint / docs / rules / padding-line-between-statements.md
1 # Require or disallow padding lines between statements (padding-line-between-statements)
2
3 This rule requires or disallows blank lines between the given 2 kinds of statements.
4 Properly blank lines help developers to understand the code.
5
6 For example, the following configuration requires a blank line between a variable declaration and a `return` statement.
7
8 ```js
9 /*eslint padding-line-between-statements: [
10 "error",
11 { blankLine: "always", prev: "var", next: "return" }
12 ]*/
13
14 function foo() {
15 var a = 1;
16
17 return a;
18 }
19 ```
20
21 ## Rule Details
22
23 This rule does nothing if no configurations are provided.
24
25 A configuration is an object which has 3 properties; `blankLine`, `prev` and `next`. For example, `{ blankLine: "always", prev: "var", next: "return" }` means "one or more blank lines are required between a variable declaration and a `return` statement."
26 You can supply any number of configurations. If a statement pair matches multiple configurations, the last matched configuration will be used.
27
28 ```json
29 {
30 "padding-line-between-statements": [
31 "error",
32 { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
33 { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
34 { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
35 { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
36 ...
37 ]
38 }
39 ```
40
41 - `LINEBREAK_TYPE` is one of the following.
42 - `"any"` just ignores the statement pair.
43 - `"never"` disallows blank lines.
44 - `"always"` requires one or more blank lines. Note it does not count lines that comments exist as blank lines.
45
46 - `STATEMENT_TYPE` is one of the following, or an array of the following.
47 - `"*"` is wildcard. This matches any statements.
48 - `"block"` is lonely blocks.
49 - `"block-like"` is block like statements. This matches statements that the last token is the closing brace of blocks; e.g. `{ }`, `if (a) { }`, and `while (a) { }`. Also matches immediately invoked function expression statements.
50 - `"break"` is `break` statements.
51 - `"case"` is `case` clauses in `switch` statements.
52 - `"cjs-export"` is `export` statements of CommonJS; e.g. `module.exports = 0`, `module.exports.foo = 1`, and `exports.foo = 2`. This is a special case of assignment.
53 - `"cjs-import"` is `import` statements of CommonJS; e.g. `const foo = require("foo")`. This is a special case of variable declarations.
54 - `"class"` is `class` declarations.
55 - `"const"` is `const` variable declarations, both single-line and multiline.
56 - `"continue"` is `continue` statements.
57 - `"debugger"` is `debugger` statements.
58 - `"default"` is `default` clauses in `switch` statements.
59 - `"directive"` is directive prologues. This matches directives; e.g. `"use strict"`.
60 - `"do"` is `do-while` statements. This matches all statements that the first token is `do` keyword.
61 - `"empty"` is empty statements.
62 - `"export"` is `export` declarations.
63 - `"expression"` is expression statements.
64 - `"for"` is `for` loop families. This matches all statements that the first token is `for` keyword.
65 - `"function"` is function declarations.
66 - `"if"` is `if` statements.
67 - `"iife"` is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator.
68 - `"import"` is `import` declarations.
69 - `"let"` is `let` variable declarations, both single-line and multiline.
70 - `"multiline-block-like"` is block like statements. This is the same as `block-like` type, but only if the block is multiline.
71 - `"multiline-const"` is multiline `const` variable declarations.
72 - `"multiline-expression"` is expression statements. This is the same as `expression` type, but only if the statement is multiline.
73 - `"multiline-let"` is multiline `let` variable declarations.
74 - `"multiline-var"` is multiline `var` variable declarations.
75 - `"return"` is `return` statements.
76 - `"singleline-const"` is single-line `const` variable declarations.
77 - `"singleline-let"` is single-line `let` variable declarations.
78 - `"singleline-var"` is single-line `var` variable declarations.
79 - `"switch"` is `switch` statements.
80 - `"throw"` is `throw` statements.
81 - `"try"` is `try` statements.
82 - `"var"` is `var` variable declarations, both single-line and multiline.
83 - `"while"` is `while` loop statements.
84 - `"with"` is `with` statements.
85
86 ## Examples
87
88 This configuration would require blank lines before all `return` statements, like the [newline-before-return] rule.
89
90 Examples of **incorrect** code for the `[{ blankLine: "always", prev: "*", next: "return" }]` configuration:
91
92 ```js
93 /*eslint padding-line-between-statements: [
94 "error",
95 { blankLine: "always", prev: "*", next: "return" }
96 ]*/
97
98 function foo() {
99 bar();
100 return;
101 }
102 ```
103
104 Examples of **correct** code for the `[{ blankLine: "always", prev: "*", next: "return" }]` configuration:
105
106 ```js
107 /*eslint padding-line-between-statements: [
108 "error",
109 { blankLine: "always", prev: "*", next: "return" }
110 ]*/
111
112 function foo() {
113 bar();
114
115 return;
116 }
117
118 function foo() {
119 return;
120 }
121 ```
122
123 ----
124
125 This configuration would require blank lines after every sequence of variable declarations, like the [newline-after-var] rule.
126
127 Examples of **incorrect** code for the `[{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}]` configuration:
128
129 ```js
130 /*eslint padding-line-between-statements: [
131 "error",
132 { blankLine: "always", prev: ["const", "let", "var"], next: "*"},
133 { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}
134 ]*/
135
136 function foo() {
137 var a = 0;
138 bar();
139 }
140
141 function foo() {
142 let a = 0;
143 bar();
144 }
145
146 function foo() {
147 const a = 0;
148 bar();
149 }
150 ```
151
152 Examples of **correct** code for the `[{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}]` configuration:
153
154 ```js
155 /*eslint padding-line-between-statements: [
156 "error",
157 { blankLine: "always", prev: ["const", "let", "var"], next: "*"},
158 { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}
159 ]*/
160
161 function foo() {
162 var a = 0;
163 var b = 0;
164
165 bar();
166 }
167
168 function foo() {
169 let a = 0;
170 const b = 0;
171
172 bar();
173 }
174
175 function foo() {
176 const a = 0;
177 const b = 0;
178
179 bar();
180 }
181 ```
182
183 ----
184
185 This configuration would require blank lines after all directive prologues, like the [lines-around-directive] rule.
186
187 Examples of **incorrect** code for the `[{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }]` configuration:
188
189 ```js
190 /*eslint padding-line-between-statements: [
191 "error",
192 { blankLine: "always", prev: "directive", next: "*" },
193 { blankLine: "any", prev: "directive", next: "directive" }
194 ]*/
195
196 "use strict";
197 foo();
198 ```
199
200 Examples of **correct** code for the `[{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }]` configuration:
201
202 ```js
203 /*eslint padding-line-between-statements: [
204 "error",
205 { blankLine: "always", prev: "directive", next: "*" },
206 { blankLine: "any", prev: "directive", next: "directive" }
207 ]*/
208
209 "use strict";
210 "use asm";
211
212 foo();
213 ```
214
215 ----
216
217 This configuration would require blank lines between clauses in `switch` statements.
218
219 Examples of **incorrect** code for the `[{ blankLine: "always", prev: ["case", "default"], next: "*" }]` configuration:
220
221 ```js
222 /*eslint padding-line-between-statements: [
223 "error",
224 { blankLine: "always", prev: ["case", "default"], next: "*" }
225 ]*/
226
227 switch (foo) {
228 case 1:
229 bar();
230 break;
231 case 2:
232 case 3:
233 baz();
234 break;
235 default:
236 quux();
237 }
238 ```
239
240 Examples of **correct** code for the `[{ blankLine: "always", prev: ["case", "default"], next: "*" }]` configuration:
241
242 ```js
243 /*eslint padding-line-between-statements: [
244 "error",
245 { blankLine: "always", prev: ["case", "default"], next: "*" }
246 ]*/
247
248 switch (foo) {
249 case 1:
250 bar();
251 break;
252
253 case 2:
254
255 case 3:
256 baz();
257 break;
258
259 default:
260 quux();
261 }
262 ```
263
264 ## Compatibility
265
266 - **JSCS:** [requirePaddingNewLineAfterVariableDeclaration]
267 - **JSCS:** [requirePaddingNewLinesAfterBlocks]
268 - **JSCS:** [disallowPaddingNewLinesAfterBlocks]
269 - **JSCS:** [requirePaddingNewLinesAfterUseStrict]
270 - **JSCS:** [disallowPaddingNewLinesAfterUseStrict]
271 - **JSCS:** [requirePaddingNewLinesBeforeExport]
272 - **JSCS:** [disallowPaddingNewLinesBeforeExport]
273 - **JSCS:** [requirePaddingNewlinesBeforeKeywords]
274 - **JSCS:** [disallowPaddingNewlinesBeforeKeywords]
275
276 ## When Not To Use It
277
278 If you don't want to notify warnings about linebreaks, then it's safe to disable this rule.
279
280 [lines-around-directive]: https://eslint.org/docs/rules/lines-around-directive
281 [newline-after-var]: https://eslint.org/docs/rules/newline-after-var
282 [newline-before-return]: https://eslint.org/docs/rules/newline-before-return
283 [requirePaddingNewLineAfterVariableDeclaration]: https://jscs-dev.github.io/rule/requirePaddingNewLineAfterVariableDeclaration
284 [requirePaddingNewLinesAfterBlocks]: https://jscs-dev.github.io/rule/requirePaddingNewLinesAfterBlocks
285 [disallowPaddingNewLinesAfterBlocks]: https://jscs-dev.github.io/rule/disallowPaddingNewLinesAfterBlocks
286 [requirePaddingNewLinesAfterUseStrict]: https://jscs-dev.github.io/rule/requirePaddingNewLinesAfterUseStrict
287 [disallowPaddingNewLinesAfterUseStrict]: https://jscs-dev.github.io/rule/disallowPaddingNewLinesAfterUseStrict
288 [requirePaddingNewLinesBeforeExport]: https://jscs-dev.github.io/rule/requirePaddingNewLinesBeforeExport
289 [disallowPaddingNewLinesBeforeExport]: https://jscs-dev.github.io/rule/disallowPaddingNewLinesBeforeExport
290 [requirePaddingNewlinesBeforeKeywords]: https://jscs-dev.github.io/rule/requirePaddingNewlinesBeforeKeywords
291 [disallowPaddingNewlinesBeforeKeywords]: https://jscs-dev.github.io/rule/disallowPaddingNewlinesBeforeKeywords