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