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