1 # enforce consistent line breaks after opening and before closing braces (object-curly-newline)
3 A number of style guides require or disallow line breaks inside of object braces and other tokens.
7 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.
11 This rule has either a string option:
13 * `"always"` requires line breaks after opening and before closing braces
14 * `"never"` disallows line breaks after opening and before closing braces
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)
22 You can specify different options for object literals, destructuring assignments, and named imports and exports:
26 "object-curly-newline": ["error", {
27 "ObjectExpression": "always",
28 "ObjectPattern": { "multiline": true },
29 "ImportDeclaration": "never",
30 "ExportDeclaration": { "multiline": true, "minProperties": 3 }
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
42 Examples of **incorrect** code for this rule with the `"always"` option:
45 /*eslint object-curly-newline: ["error", "always"]*/
50 let c = {foo: 1, bar: 2};
67 Examples of **correct** code for this rule with the `"always"` option:
70 /*eslint object-curly-newline: ["error", "always"]*/
112 Examples of **incorrect** code for this rule with the `"never"` option:
115 /*eslint object-curly-newline: ["error", "never"]*/
155 Examples of **correct** code for this rule with the `"never"` option:
158 /*eslint object-curly-newline: ["error", "never"]*/
163 let c = {foo: 1, bar: 2};
166 let e = {foo: function() {
175 let {k = function() {
182 Examples of **incorrect** code for this rule with the `{ "multiline": true }` option:
185 /*eslint object-curly-newline: ["error", { "multiline": true }]*/
198 let e = {foo: function() {
212 let {k = function() {
217 Examples of **correct** code for this rule with the `{ "multiline": true }` option:
220 /*eslint object-curly-newline: ["error", { "multiline": true }]*/
225 let c = {foo: 1, bar: 2};
252 Examples of **incorrect** code for this rule with the `{ "minProperties": 2 }` option:
255 /*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/
263 let c = {foo: 1, bar: 2};
287 Examples of **correct** code for this rule with the `{ "minProperties": 2 }` option:
290 /*eslint object-curly-newline: ["error", { "minProperties": 2 }]*/
302 let e = {foo: function() {
315 let {k = function() {
322 Examples of **incorrect** code for this rule with the default `{ "consistent": true }` option:
325 /*eslint object-curly-newline: ["error", { "consistent": true }]*/
332 let c = {foo: 1, bar: 2
336 let e = {foo: function() {
356 let {q = function() {
366 Examples of **correct** code for this rule with the default `{ "consistent": true }` option:
369 /*eslint object-curly-newline: ["error", { "consistent": true }]*/
387 let e = {foo: function() {dosomething();}};
411 let {q = function() {dosomething();}} = obj;
419 ### ObjectExpression and ObjectPattern
421 Examples of **incorrect** code for this rule with the `{ "ObjectExpression": "always", "ObjectPattern": "never" }` options:
424 /*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
429 let c = {foo: 1, bar: 2};
432 let e = {foo: function() {
455 Examples of **correct** code for this rule with the `{ "ObjectExpression": "always", "ObjectPattern": "never" }` options:
458 /*eslint object-curly-newline: ["error", { "ObjectExpression": "always", "ObjectPattern": "never" }]*/
484 let {k = function() {
489 ### ImportDeclaration and ExportDeclaration
491 Examples of **incorrect** code for this rule with the `{ "ImportDeclaration": "always", "ExportDeclaration": "never" }` options:
494 /*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
497 import {foo, bar} from 'foo-bar';
498 import {foo as f, bar} from 'foo-bar';
512 Examples of **correct** code for this rule with the `{ "ImportDeclaration": "always", "ExportDeclaration": "never" }` options:
515 /*eslint object-curly-newline: ["error", { "ImportDeclaration": "always", "ExportDeclaration": "never" }]*/
530 export { foo, bar } from 'foo-bar';
531 export { foo as f, bar } from 'foo-bar';
536 * **JSCS**: [requirePaddingNewLinesInObjects](https://jscs-dev.github.io/rule/requirePaddingNewLinesInObjects)
537 * **JSCS**: [disallowPaddingNewLinesInObjects](https://jscs-dev.github.io/rule/disallowPaddingNewLinesInObjects)
539 ## When Not To Use It
541 If you don't want to enforce consistent line breaks after opening and before closing braces, then it's safe to disable this rule.
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)