]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/object-curly-newline.md
302c81c33b5550465c8c006a6ba23fc81208c81e
[pve-eslint.git] / eslint / docs / rules / object-curly-newline.md
1 # enforce consistent line breaks inside braces (object-curly-newline)
2
3 A number of style guides require or disallow line breaks inside of object braces and other tokens.
4
5 ## Rule Details
6
7 This rule enforces consistent line breaks inside braces of object literals or destructuring assignments.
8
9 ## Options
10
11 This rule has either a string option:
12
13 * `"always"` requires line breaks inside braces
14 * `"never"` disallows line breaks inside braces
15
16 Or an object option:
17
18 * `"multiline": true` requires line breaks if there are line breaks inside properties or between properties. Otherwise, it disallows line breaks.
19 * `"minProperties"` requires line breaks if the number of properties is at least the given integer. By default, an error will also be reported if an object contains linebreaks and has fewer properties than the given integer. However, the second behavior is disabled if the `consistent` option is set to `true`
20 * `"consistent": true` (default) requires that either both curly braces, or neither, directly enclose newlines. Note that enabling this option will also change the behavior of the `minProperties` option. (See `minProperties` above for more information)
21
22 You can specify different options for object literals, destructuring assignments, and named imports and exports:
23
24 ```json
25 {
26 "object-curly-newline": ["error", {
27 "ObjectExpression": "always",
28 "ObjectPattern": { "multiline": true },
29 "ImportDeclaration": "never",
30 "ExportDeclaration": { "multiline": true, "minProperties": 3 }
31 }]
32 }
33 ```
34
35 * `"ObjectExpression"` configuration for object literals
36 * `"ObjectPattern"` configuration for object patterns of destructuring assignments
37 * `"ImportDeclaration"` configuration for named imports
38 * `"ExportDeclaration"` configuration for named exports
39
40 ### always
41
42 Examples of **incorrect** code for this rule with the `"always"` option:
43
44 ```js
45 /*eslint object-curly-newline: ["error", "always"]*/
46 /*eslint-env es6*/
47
48 let a = {};
49 let b = {foo: 1};
50 let c = {foo: 1, bar: 2};
51 let d = {foo: 1,
52 bar: 2};
53 let e = {foo() {
54 dosomething();
55 }};
56
57 let {} = obj;
58 let {f} = obj;
59 let {g, h} = obj;
60 let {i,
61 j} = obj;
62 let {k = function() {
63 dosomething();
64 }} = obj;
65 ```
66
67 Examples of **correct** code for this rule with the `"always"` option:
68
69 ```js
70 /*eslint object-curly-newline: ["error", "always"]*/
71 /*eslint-env es6*/
72
73 let a = {
74 };
75 let b = {
76 foo: 1
77 };
78 let c = {
79 foo: 1, bar: 2
80 };
81 let d = {
82 foo: 1,
83 bar: 2
84 };
85 let e = {
86 foo: function() {
87 dosomething();
88 }
89 };
90
91 let {
92 } = obj;
93 let {
94 f
95 } = obj;
96 let {
97 g, h
98 } = obj;
99 let {
100 i,
101 j
102 } = obj;
103 let {
104 k = function() {
105 dosomething();
106 }
107 } = obj;
108 ```
109
110 ### never
111
112 Examples of **incorrect** code for this rule with the `"never"` option:
113
114 ```js
115 /*eslint object-curly-newline: ["error", "never"]*/
116 /*eslint-env es6*/
117
118 let a = {
119 };
120 let b = {
121 foo: 1
122 };
123 let c = {
124 foo: 1, bar: 2
125 };
126 let d = {
127 foo: 1,
128 bar: 2
129 };
130 let e = {
131 foo: function() {
132 dosomething();
133 }
134 };
135
136 let {
137 } = obj;
138 let {
139 f
140 } = obj;
141 let {
142 g, h
143 } = obj;
144 let {
145 i,
146 j
147 } = obj;
148 let {
149 k = function() {
150 dosomething();
151 }
152 } = obj;
153 ```
154
155 Examples of **correct** code for this rule with the `"never"` option:
156
157 ```js
158 /*eslint object-curly-newline: ["error", "never"]*/
159 /*eslint-env es6*/
160
161 let a = {};
162 let b = {foo: 1};
163 let c = {foo: 1, bar: 2};
164 let d = {foo: 1,
165 bar: 2};
166 let e = {foo: function() {
167 dosomething();
168 }};
169
170 let {} = obj;
171 let {f} = obj;
172 let {g, h} = obj;
173 let {i,
174 j} = obj;
175 let {k = function() {
176 dosomething();
177 }} = obj;
178 ```
179
180 ### multiline
181
182 Examples of **incorrect** code for this rule with the `{ "multiline": true }` option:
183
184 ```js
185 /*eslint object-curly-newline: ["error", { "multiline": true }]*/
186 /*eslint-env es6*/
187
188 let a = {
189 };
190 let b = {
191 foo: 1
192 };
193 let c = {
194 foo: 1, bar: 2
195 };
196 let d = {foo: 1,
197 bar: 2};
198 let e = {foo: function() {
199 dosomething();
200 }};
201
202 let {
203 } = obj;
204 let {
205 f
206 } = obj;
207 let {
208 g, h
209 } = obj;
210 let {i,
211 j} = obj;
212 let {k = function() {
213 dosomething();
214 }} = obj;
215 ```
216
217 Examples of **correct** code for this rule with the `{ "multiline": true }` option:
218
219 ```js
220 /*eslint object-curly-newline: ["error", { "multiline": true }]*/
221 /*eslint-env es6*/
222
223 let a = {};
224 let b = {foo: 1};
225 let c = {foo: 1, bar: 2};
226 let d = {
227 foo: 1,
228 bar: 2
229 };
230 let e = {
231 foo: function() {
232 dosomething();
233 }
234 };
235
236 let {} = obj;
237 let {f} = obj;
238 let {g, h} = obj;
239 let {
240 i,
241 j
242 } = obj;
243 let {
244 k = function() {
245 dosomething();
246 }
247 } = obj;
248 ```
249
250 ### minProperties
251
252 Examples of **incorrect** code for this rule with the `{ "minProperties": 2 }` option:
253
254 ```js
255 /*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/
256 /*eslint-env es6*/
257
258 let a = {
259 };
260 let b = {
261 foo: 1
262 };
263 let c = {foo: 1, bar: 2};
264 let d = {foo: 1,
265 bar: 2};
266 let e = {
267 foo: function() {
268 dosomething();
269 }
270 };
271
272 let {
273 } = obj;
274 let {
275 f
276 } = obj;
277 let {g, h} = obj;
278 let {i,
279 j} = obj;
280 let {
281 k = function() {
282 dosomething();
283 }
284 } = obj;
285 ```
286
287 Examples of **correct** code for this rule with the `{ "minProperties": 2 }` option:
288
289 ```js
290 /*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/
291 /*eslint-env es6*/
292
293 let a = {};
294 let b = {foo: 1};
295 let c = {
296 foo: 1, bar: 2
297 };
298 let d = {
299 foo: 1,
300 bar: 2
301 };
302 let e = {foo: function() {
303 dosomething();
304 }};
305
306 let {} = obj;
307 let {f} = obj;
308 let {
309 g, h
310 } = obj;
311 let {
312 i,
313 j
314 } = obj;
315 let {k = function() {
316 dosomething();
317 }} = obj;
318 ```
319
320 ### consistent
321
322 Examples of **incorrect** code for this rule with the default `{ "consistent": true }` option:
323
324 ```js
325 /*eslint object-curly-newline: ["error", { "consistent": true }]*/
326 /*eslint-env es6*/
327
328 let a = {foo: 1
329 };
330 let b = {
331 foo: 1};
332 let c = {foo: 1, bar: 2
333 };
334 let d = {
335 foo: 1, bar: 2};
336 let e = {foo: function() {
337 dosomething();
338 }
339 };
340 let f = {
341 foo: function() {
342 dosomething();}};
343
344 let {g
345 } = obj;
346 let {
347 h} = obj;
348 let {i, j
349 } = obj;
350 let {k, l
351 } = obj;
352 let {
353 m, n} = obj;
354 let {
355 o, p} = obj;
356 let {q = function() {
357 dosomething();
358 }
359 } = obj;
360 let {
361 r = function() {
362 dosomething();
363 }} = obj;
364 ```
365
366 Examples of **correct** code for this rule with the default `{ "consistent": true }` option:
367
368 ```js
369 /*eslint object-curly-newline: ["error", { "consistent": true }]*/
370 /*eslint-env es6*/
371
372
373 let empty1 = {};
374 let empty2 = {
375 };
376 let a = {foo: 1};
377 let b = {
378 foo: 1
379 };
380 let c = {
381 foo: 1, bar: 2
382 };
383 let d = {
384 foo: 1,
385 bar: 2
386 };
387 let e = {foo: function() {dosomething();}};
388 let f = {
389 foo: function() {
390 dosomething();
391 }
392 };
393
394 let {} = obj;
395 let {
396 } = obj;
397 let {g} = obj;
398 let {
399 h
400 } = obj;
401 let {i, j} = obj;
402 let {
403 k, l
404 } = obj;
405 let {m,
406 n} = obj;
407 let {
408 o,
409 p
410 } = obj;
411 let {q = function() {dosomething();}} = obj;
412 let {
413 r = function() {
414 dosomething();
415 }
416 } = obj;
417 ```
418
419 ### ObjectExpression and ObjectPattern
420
421 Examples of **incorrect** code for this rule with the `{ "ObjectExpression": "always", "ObjectPattern": "never" }` options:
422
423 ```js
424 /*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
425 /*eslint-env es6*/
426
427 let a = {};
428 let b = {foo: 1};
429 let c = {foo: 1, bar: 2};
430 let d = {foo: 1,
431 bar: 2};
432 let e = {foo: function() {
433 dosomething();
434 }};
435
436 let {
437 } = obj;
438 let {
439 f
440 } = obj;
441 let {
442 g, h
443 } = obj;
444 let {
445 i,
446 j
447 } = obj;
448 let {
449 k = function() {
450 dosomething();
451 }
452 } = obj;
453 ```
454
455 Examples of **correct** code for this rule with the `{ "ObjectExpression": "always", "ObjectPattern": "never" }` options:
456
457 ```js
458 /*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
459 /*eslint-env es6*/
460
461 let a = {
462 };
463 let b = {
464 foo: 1
465 };
466 let c = {
467 foo: 1, bar: 2
468 };
469 let d = {
470 foo: 1,
471 bar: 2
472 };
473 let e = {
474 foo: function() {
475 dosomething();
476 }
477 };
478
479 let {} = obj;
480 let {f} = obj;
481 let {g, h} = obj;
482 let {i,
483 j} = obj;
484 let {k = function() {
485 dosomething();
486 }} = obj;
487 ```
488
489 ### ImportDeclaration and ExportDeclaration
490
491 Examples of **incorrect** code for this rule with the `{ "ImportDeclaration": "always", "ExportDeclaration": "never" }` options:
492
493 ```js
494 /*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
495 /*eslint-env es6*/
496
497 import {foo, bar} from 'foo-bar';
498 import {foo as f, bar} from 'foo-bar';
499 import {foo,
500 bar} from 'foo-bar';
501
502 export {
503 foo,
504 bar
505 };
506 export {
507 foo as f,
508 bar
509 } from 'foo-bar';
510 ```
511
512 Examples of **correct** code for this rule with the `{ "ImportDeclaration": "always", "ExportDeclaration": "never" }` options:
513
514 ```js
515 /*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
516 /*eslint-env es6*/
517
518 import {
519 foo,
520 bar
521 } from 'foo-bar';
522 import {
523 foo, bar
524 } from 'foo-bar';
525 import {
526 foo as f,
527 bar
528 } from 'foo-bar';
529
530 export { foo, bar } from 'foo-bar';
531 export { foo as f, bar } from 'foo-bar';
532 ```
533
534 ## Compatibility
535
536 * **JSCS**: [requirePaddingNewLinesInObjects](https://jscs-dev.github.io/rule/requirePaddingNewLinesInObjects)
537 * **JSCS**: [disallowPaddingNewLinesInObjects](https://jscs-dev.github.io/rule/disallowPaddingNewLinesInObjects)
538
539 ## When Not To Use It
540
541 If you don't want to enforce consistent line breaks inside braces, then it's safe to disable this rule.
542
543 ## Related Rules
544
545 * [comma-spacing](comma-spacing.md)
546 * [key-spacing](key-spacing.md)
547 * [object-curly-spacing](object-curly-spacing.md)
548 * [object-property-newline](object-property-newline.md)