]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/lines-around-directive.md
first commit
[pve-eslint.git] / eslint / docs / rules / lines-around-directive.md
1 # require or disallow newlines around directives (lines-around-directive)
2
3 This rule was **deprecated** in ESLint v4.0.0 and replaced by the [padding-line-between-statements](padding-line-between-statements.md) rule.
4
5 Directives are used in JavaScript to indicate to the execution environment that a script would like to opt into a feature such as `"strict mode"`. Directives are grouped together in a [directive prologue](https://www.ecma-international.org/ecma-262/7.0/#directive-prologue) at the top of either a file or function block and are applied to the scope in which they occur.
6
7 ```js
8 // Strict mode is invoked for the entire script
9 "use strict";
10
11 var foo;
12
13 function bar() {
14 var baz;
15 }
16 ```
17
18 ```js
19 var foo;
20
21 function bar() {
22 // Strict mode is only invoked within this function
23 "use strict";
24
25 var baz;
26 }
27 ```
28
29 ## Rule Details
30
31 This rule requires or disallows blank newlines around directive prologues. This rule does not enforce any conventions about blank newlines between the individual directives. In addition, it does not require blank newlines before directive prologues unless they are preceded by a comment. Please use the [padded-blocks](padded-blocks.md) rule if this is a style you would like to enforce.
32
33 ## Options
34
35 This rule has one option. It can either be a string or an object:
36
37 * `"always"` (default) enforces blank newlines around directives.
38 * `"never"` disallows blank newlines around directives.
39
40 or
41
42 ```js
43 {
44 "before": "always" or "never"
45 "after": "always" or "never",
46 }
47 ```
48
49 ### always
50
51 This is the default option.
52
53 Examples of **incorrect** code for this rule with the `"always"` option:
54
55 ```js
56 /* eslint lines-around-directive: ["error", "always"] */
57
58 /* Top of file */
59 "use strict";
60 var foo;
61
62 /* Top of file */
63 // comment
64 "use strict";
65 "use asm";
66 var foo;
67
68 function foo() {
69 "use strict";
70 "use asm";
71 var bar;
72 }
73
74 function foo() {
75 // comment
76 "use strict";
77 var bar;
78 }
79 ```
80
81 Examples of **correct** code for this rule with the `"always"` option:
82
83 ```js
84 /* eslint lines-around-directive: ["error", "always"] */
85
86 /* Top of file */
87 "use strict";
88
89 var foo;
90
91 /* Top of file */
92 // comment
93
94 "use strict";
95 "use asm";
96
97 var foo;
98
99 function foo() {
100 "use strict";
101 "use asm";
102
103 var bar;
104 }
105
106 function foo() {
107 // comment
108
109 "use strict";
110
111 var bar;
112 }
113 ```
114
115 ### never
116
117 Examples of **incorrect** code for this rule with the `"never"` option:
118
119 ```js
120 /* eslint lines-around-directive: ["error", "never"] */
121
122 /* Top of file */
123
124 "use strict";
125
126 var foo;
127
128
129 /* Top of file */
130 // comment
131
132 "use strict";
133 "use asm";
134
135 var foo;
136
137
138 function foo() {
139 "use strict";
140 "use asm";
141
142 var bar;
143 }
144
145
146 function foo() {
147 // comment
148
149 "use strict";
150
151 var bar;
152 }
153 ```
154
155 Examples of **correct** code for this rule with the `"never"` option:
156
157 ```js
158 /* eslint lines-around-directive: ["error", "never"] */
159
160 /* Top of file */
161 "use strict";
162 var foo;
163
164 /* Top of file */
165 // comment
166 "use strict";
167 "use asm";
168 var foo;
169
170 function foo() {
171 "use strict";
172 "use asm";
173 var bar;
174 }
175
176 function foo() {
177 // comment
178 "use strict";
179 var bar;
180 }
181 ```
182
183 ### before & after
184
185 Examples of **incorrect** code for this rule with the `{ "before": "never", "after": "always" }` option:
186
187 ```js
188 /* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
189
190 /* Top of file */
191
192 "use strict";
193 var foo;
194
195 /* Top of file */
196 // comment
197
198 "use strict";
199 "use asm";
200 var foo;
201
202 function foo() {
203 "use strict";
204 "use asm";
205 var bar;
206 }
207
208 function foo() {
209 // comment
210
211 "use strict";
212 var bar;
213 }
214 ```
215
216 Examples of **correct** code for this rule with the `{ "before": "never", "after": "always" }` option:
217
218 ```js
219 /* eslint lines-around-directive: ["error", { "before": "never", "after": "always" }] */
220
221 /* Top of file */
222 "use strict";
223
224 var foo;
225
226 /* Top of file */
227 // comment
228 "use strict";
229 "use asm";
230
231 var foo;
232
233 function foo() {
234 "use strict";
235 "use asm";
236
237 var bar;
238 }
239
240 function foo() {
241 // comment
242 "use strict";
243
244 var bar;
245 }
246 ```
247
248 Examples of **incorrect** code for this rule with the `{ "before": "always", "after": "never" }` option:
249
250 ```js
251 /* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
252
253 /* Top of file */
254 "use strict";
255
256 var foo;
257
258 /* Top of file */
259 // comment
260 "use strict";
261 "use asm";
262
263 var foo;
264
265 function foo() {
266 "use strict";
267 "use asm";
268
269 var bar;
270 }
271
272 function foo() {
273 // comment
274 "use strict";
275
276 var bar;
277 }
278 ```
279
280 Examples of **correct** code for this rule with the `{ "before": "always", "after": "never" }` option:
281
282 ```js
283 /* eslint lines-around-directive: ["error", { "before": "always", "after": "never" }] */
284
285 /* Top of file */
286 "use strict";
287 var foo;
288
289 /* Top of file */
290 // comment
291
292 "use strict";
293 "use asm";
294 var foo;
295
296 function foo() {
297 "use strict";
298 "use asm";
299 var bar;
300 }
301
302 function foo() {
303 // comment
304
305 "use strict";
306 var bar;
307 }
308 ```
309
310 ## When Not To Use It
311
312 You can safely disable this rule if you do not have any strict conventions about whether or not directive prologues should have blank newlines before or after them.
313
314 ## Related Rules
315
316 * [lines-around-comment](lines-around-comment.md)
317 * [padded-blocks](padded-blocks.md)
318
319 ## Compatibility
320
321 * **JSCS**: [requirePaddingNewLinesAfterUseStrict](https://jscs-dev.github.io/rule/requirePaddingNewLinesAfterUseStrict)
322 * **JSCS**: [disallowPaddingNewLinesAfterUseStrict](https://jscs-dev.github.io/rule/disallowPaddingNewLinesAfterUseStrict)