]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: object-curly-spacing | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | related_rules: | |
5 | - array-bracket-spacing | |
6 | - comma-spacing | |
7 | - computed-property-spacing | |
8 | - space-in-parens | |
9 | --- | |
10 | ||
11 | ||
eb39fafa DC |
12 | |
13 | While formatting preferences are very personal, a number of style guides require | |
14 | or disallow spaces between curly braces in the following situations: | |
15 | ||
16 | ```js | |
17 | // simple object literals | |
18 | var obj = { foo: "bar" }; | |
19 | ||
20 | // nested object literals | |
21 | var obj = { foo: { zoo: "bar" } }; | |
22 | ||
23 | // destructuring assignment (EcmaScript 6) | |
24 | var { x, y } = y; | |
25 | ||
26 | // import/export declarations (EcmaScript 6) | |
27 | import { foo } from "bar"; | |
28 | export { foo }; | |
29 | ``` | |
30 | ||
31 | ## Rule Details | |
32 | ||
33 | This rule enforces consistent spacing inside braces of object literals, destructuring assignments, and import/export specifiers. | |
34 | ||
35 | ## Options | |
36 | ||
37 | This rule has two options, a string option and an object option. | |
38 | ||
39 | String option: | |
40 | ||
41 | * `"never"` (default) disallows spacing inside of braces | |
42 | * `"always"` requires spacing inside of braces (except `{}`) | |
43 | ||
44 | Object option: | |
45 | ||
46 | * `"arraysInObjects": true` requires spacing inside of braces of objects beginning and/or ending with an array element (applies when the first option is set to `never`) | |
47 | * `"arraysInObjects": false` disallows spacing inside of braces of objects beginning and/or ending with an array element (applies when the first option is set to `always`) | |
48 | * `"objectsInObjects": true` requires spacing inside of braces of objects beginning and/or ending with an object element (applies when the first option is set to `never`) | |
49 | * `"objectsInObjects": false` disallows spacing inside of braces of objects beginning and/or ending with an object element (applies when the first option is set to `always`) | |
50 | ||
51 | ### never | |
52 | ||
53 | Examples of **incorrect** code for this rule with the default `"never"` option: | |
54 | ||
8f9d1d4d DC |
55 | ::: incorrect |
56 | ||
eb39fafa DC |
57 | ```js |
58 | /*eslint object-curly-spacing: ["error", "never"]*/ | |
59 | ||
60 | var obj = { 'foo': 'bar' }; | |
61 | var obj = {'foo': 'bar' }; | |
62 | var obj = { baz: {'foo': 'qux'}, bar}; | |
63 | var obj = {baz: { 'foo': 'qux'}, bar}; | |
64 | var {x } = y; | |
65 | import { foo } from 'bar'; | |
66 | ``` | |
67 | ||
8f9d1d4d DC |
68 | ::: |
69 | ||
eb39fafa DC |
70 | Examples of **correct** code for this rule with the default `"never"` option: |
71 | ||
8f9d1d4d DC |
72 | ::: correct |
73 | ||
eb39fafa DC |
74 | ```js |
75 | /*eslint object-curly-spacing: ["error", "never"]*/ | |
76 | ||
77 | var obj = {'foo': 'bar'}; | |
78 | var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'}; | |
79 | var obj = { | |
80 | 'foo': 'bar' | |
81 | }; | |
82 | var obj = {'foo': 'bar' | |
83 | }; | |
84 | var obj = { | |
85 | 'foo':'bar'}; | |
86 | var obj = {}; | |
87 | var {x} = y; | |
88 | import {foo} from 'bar'; | |
89 | ``` | |
90 | ||
8f9d1d4d DC |
91 | ::: |
92 | ||
eb39fafa DC |
93 | ### always |
94 | ||
95 | Examples of **incorrect** code for this rule with the `"always"` option: | |
96 | ||
8f9d1d4d DC |
97 | ::: incorrect |
98 | ||
eb39fafa DC |
99 | ```js |
100 | /*eslint object-curly-spacing: ["error", "always"]*/ | |
101 | ||
102 | var obj = {'foo': 'bar'}; | |
103 | var obj = {'foo': 'bar' }; | |
104 | var obj = { baz: {'foo': 'qux'}, bar}; | |
105 | var obj = {baz: { 'foo': 'qux' }, bar}; | |
106 | var obj = {'foo': 'bar' | |
107 | }; | |
108 | var obj = { | |
109 | 'foo':'bar'}; | |
110 | var {x} = y; | |
111 | import {foo } from 'bar'; | |
112 | ``` | |
113 | ||
8f9d1d4d DC |
114 | ::: |
115 | ||
eb39fafa DC |
116 | Examples of **correct** code for this rule with the `"always"` option: |
117 | ||
8f9d1d4d DC |
118 | ::: correct |
119 | ||
eb39fafa DC |
120 | ```js |
121 | /*eslint object-curly-spacing: ["error", "always"]*/ | |
122 | ||
123 | var obj = {}; | |
124 | var obj = { 'foo': 'bar' }; | |
125 | var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' }; | |
126 | var obj = { | |
127 | 'foo': 'bar' | |
128 | }; | |
129 | var { x } = y; | |
130 | import { foo } from 'bar'; | |
131 | ``` | |
132 | ||
8f9d1d4d DC |
133 | ::: |
134 | ||
eb39fafa DC |
135 | #### arraysInObjects |
136 | ||
137 | Examples of additional **correct** code for this rule with the `"never", { "arraysInObjects": true }` options: | |
138 | ||
8f9d1d4d DC |
139 | ::: correct |
140 | ||
eb39fafa DC |
141 | ```js |
142 | /*eslint object-curly-spacing: ["error", "never", { "arraysInObjects": true }]*/ | |
143 | ||
144 | var obj = {"foo": [ 1, 2 ] }; | |
145 | var obj = {"foo": [ "baz", "bar" ] }; | |
146 | ``` | |
147 | ||
8f9d1d4d DC |
148 | ::: |
149 | ||
eb39fafa DC |
150 | Examples of additional **correct** code for this rule with the `"always", { "arraysInObjects": false }` options: |
151 | ||
8f9d1d4d DC |
152 | ::: correct |
153 | ||
eb39fafa DC |
154 | ```js |
155 | /*eslint object-curly-spacing: ["error", "always", { "arraysInObjects": false }]*/ | |
156 | ||
157 | var obj = { "foo": [ 1, 2 ]}; | |
158 | var obj = { "foo": [ "baz", "bar" ]}; | |
159 | ``` | |
160 | ||
8f9d1d4d DC |
161 | ::: |
162 | ||
eb39fafa DC |
163 | #### objectsInObjects |
164 | ||
165 | Examples of additional **correct** code for this rule with the `"never", { "objectsInObjects": true }` options: | |
166 | ||
8f9d1d4d DC |
167 | ::: correct |
168 | ||
eb39fafa DC |
169 | ```js |
170 | /*eslint object-curly-spacing: ["error", "never", { "objectsInObjects": true }]*/ | |
171 | ||
172 | var obj = {"foo": {"baz": 1, "bar": 2} }; | |
173 | ``` | |
174 | ||
8f9d1d4d DC |
175 | ::: |
176 | ||
eb39fafa DC |
177 | Examples of additional **correct** code for this rule with the `"always", { "objectsInObjects": false }` options: |
178 | ||
8f9d1d4d DC |
179 | ::: correct |
180 | ||
eb39fafa DC |
181 | ```js |
182 | /*eslint object-curly-spacing: ["error", "always", { "objectsInObjects": false }]*/ | |
183 | ||
184 | var obj = { "foo": { "baz": 1, "bar": 2 }}; | |
185 | ``` | |
186 | ||
8f9d1d4d DC |
187 | ::: |
188 | ||
eb39fafa DC |
189 | ## When Not To Use It |
190 | ||
191 | You can turn this rule off if you are not concerned with the consistency of spacing between curly braces. |