]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/object-curly-newline.md
import eslint 7.28.0
[pve-eslint.git] / eslint / docs / rules / object-curly-newline.md
CommitLineData
5422a9cc 1# enforce consistent line breaks after opening and before closing braces (object-curly-newline)
eb39fafa
DC
2
3A number of style guides require or disallow line breaks inside of object braces and other tokens.
4
5## Rule Details
6
5422a9cc 7This 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
8
9## Options
10
11This rule has either a string option:
12
5422a9cc
TL
13* `"always"` requires line breaks after opening and before closing braces
14* `"never"` disallows line breaks after opening and before closing braces
eb39fafa
DC
15
16Or 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
22You 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
42Examples 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
48let a = {};
49let b = {foo: 1};
50let c = {foo: 1, bar: 2};
51let d = {foo: 1,
52 bar: 2};
53let e = {foo() {
54 dosomething();
55}};
56
57let {} = obj;
58let {f} = obj;
59let {g, h} = obj;
60let {i,
61 j} = obj;
62let {k = function() {
63 dosomething();
64}} = obj;
65```
66
67Examples 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
73let a = {
74};
75let b = {
76 foo: 1
77};
78let c = {
79 foo: 1, bar: 2
80};
81let d = {
82 foo: 1,
83 bar: 2
84};
85let e = {
86 foo: function() {
87 dosomething();
88 }
89};
90
91let {
92} = obj;
93let {
94 f
95} = obj;
96let {
97 g, h
98} = obj;
99let {
100 i,
101 j
102} = obj;
103let {
104 k = function() {
105 dosomething();
106 }
107} = obj;
108```
109
110### never
111
112Examples 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
118let a = {
119};
120let b = {
121 foo: 1
122};
123let c = {
124 foo: 1, bar: 2
125};
126let d = {
127 foo: 1,
128 bar: 2
129};
130let e = {
131 foo: function() {
132 dosomething();
133 }
134};
135
136let {
137} = obj;
138let {
139 f
140} = obj;
141let {
142 g, h
143} = obj;
144let {
145 i,
146 j
147} = obj;
148let {
149 k = function() {
150 dosomething();
151 }
152} = obj;
153```
154
155Examples 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
161let a = {};
162let b = {foo: 1};
163let c = {foo: 1, bar: 2};
164let d = {foo: 1,
165 bar: 2};
166let e = {foo: function() {
167 dosomething();
168}};
169
170let {} = obj;
171let {f} = obj;
172let {g, h} = obj;
173let {i,
174 j} = obj;
175let {k = function() {
176 dosomething();
177}} = obj;
178```
179
180### multiline
181
182Examples 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
188let a = {
189};
190let b = {
191 foo: 1
192};
193let c = {
194 foo: 1, bar: 2
195};
196let d = {foo: 1,
197 bar: 2};
198let e = {foo: function() {
199 dosomething();
200}};
201
202let {
203} = obj;
204let {
205 f
206} = obj;
207let {
208 g, h
209} = obj;
210let {i,
211 j} = obj;
212let {k = function() {
213 dosomething();
214}} = obj;
215```
216
217Examples 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
223let a = {};
224let b = {foo: 1};
225let c = {foo: 1, bar: 2};
226let d = {
227 foo: 1,
228 bar: 2
229};
230let e = {
231 foo: function() {
232 dosomething();
233 }
234};
235
236let {} = obj;
237let {f} = obj;
238let {g, h} = obj;
239let {
240 i,
241 j
242} = obj;
243let {
244 k = function() {
245 dosomething();
246 }
247} = obj;
248```
249
250### minProperties
251
252Examples 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
258let a = {
259};
260let b = {
261 foo: 1
262};
263let c = {foo: 1, bar: 2};
264let d = {foo: 1,
265 bar: 2};
266let e = {
267 foo: function() {
268 dosomething();
269 }
270};
271
272let {
273} = obj;
274let {
275 f
276} = obj;
277let {g, h} = obj;
278let {i,
279 j} = obj;
280let {
281 k = function() {
282 dosomething();
283 }
284} = obj;
285```
286
287Examples 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
293let a = {};
294let b = {foo: 1};
295let c = {
296 foo: 1, bar: 2
297};
298let d = {
299 foo: 1,
300 bar: 2
301};
302let e = {foo: function() {
303 dosomething();
304}};
305
306let {} = obj;
307let {f} = obj;
308let {
309 g, h
310} = obj;
311let {
312 i,
313 j
314} = obj;
315let {k = function() {
316 dosomething();
317}} = obj;
318```
319
320### consistent
321
322Examples 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
328let a = {foo: 1
329};
330let b = {
331 foo: 1};
332let c = {foo: 1, bar: 2
333};
334let d = {
335 foo: 1, bar: 2};
336let e = {foo: function() {
337 dosomething();
6f036462
TL
338 }
339};
340let f = {
341 foo: function() {
342 dosomething();}};
eb39fafa 343
6f036462 344let {g
eb39fafa
DC
345} = obj;
346let {
6f036462
TL
347 h} = obj;
348let {i, j
eb39fafa 349} = obj;
6f036462
TL
350let {k, l
351} = obj;
352let {
353 m, n} = obj;
eb39fafa 354let {
6f036462
TL
355 o, p} = obj;
356let {q = function() {
eb39fafa 357 dosomething();
6f036462
TL
358 }
359} = obj;
360let {
361 r = function() {
362 dosomething();
363 }} = obj;
eb39fafa
DC
364```
365
366Examples 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
6f036462
TL
372
373let empty1 = {};
374let empty2 = {
375};
376let a = {foo: 1};
377let b = {
eb39fafa
DC
378 foo: 1
379};
6f036462 380let c = {
eb39fafa
DC
381 foo: 1, bar: 2
382};
6f036462 383let d = {
eb39fafa
DC
384 foo: 1,
385 bar: 2
386};
6f036462
TL
387let e = {foo: function() {dosomething();}};
388let f = {
eb39fafa
DC
389 foo: function() {
390 dosomething();
391 }
392};
393
394let {} = obj;
6f036462
TL
395let {
396} = obj;
397let {g} = obj;
398let {
399 h
400} = obj;
eb39fafa
DC
401let {i, j} = obj;
402let {
403 k, l
404} = obj;
405let {m,
406 n} = obj;
407let {
408 o,
409 p
410} = obj;
411let {q = function() {dosomething();}} = obj;
412let {
413 r = function() {
414 dosomething();
415 }
416} = obj;
417```
418
419### ObjectExpression and ObjectPattern
420
421Examples 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
427let a = {};
428let b = {foo: 1};
429let c = {foo: 1, bar: 2};
430let d = {foo: 1,
431 bar: 2};
432let e = {foo: function() {
433 dosomething();
434}};
435
436let {
437} = obj;
438let {
439 f
440} = obj;
441let {
442 g, h
443} = obj;
444let {
445 i,
446 j
447} = obj;
448let {
449 k = function() {
450 dosomething();
451 }
452} = obj;
453```
454
455Examples 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
461let a = {
462};
463let b = {
464 foo: 1
465};
466let c = {
467 foo: 1, bar: 2
468};
469let d = {
470 foo: 1,
471 bar: 2
472};
473let e = {
474 foo: function() {
475 dosomething();
476 }
477};
478
479let {} = obj;
480let {f} = obj;
481let {g, h} = obj;
482let {i,
483 j} = obj;
484let {k = function() {
485 dosomething();
486}} = obj;
487```
488
489### ImportDeclaration and ExportDeclaration
490
491Examples 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
497import {foo, bar} from 'foo-bar';
498import {foo as f, bar} from 'foo-bar';
499import {foo,
500 bar} from 'foo-bar';
501
502export {
503 foo,
504 bar
505};
506export {
507 foo as f,
508 bar
509} from 'foo-bar';
510```
511
512Examples 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
518import {
519 foo,
520 bar
521} from 'foo-bar';
56c4a2cb
DC
522import {
523 foo, bar
524} from 'foo-bar';
eb39fafa
DC
525import {
526 foo as f,
527 bar
528} from 'foo-bar';
529
530export { foo, bar } from 'foo-bar';
531export { 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
5422a9cc 541If you don't want to enforce consistent line breaks after opening and before closing braces, then it's safe to disable this rule.
eb39fafa
DC
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)