]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/object-curly-spacing.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / object-curly-spacing.md
CommitLineData
8f9d1d4d
DC
1---
2title: object-curly-spacing
8f9d1d4d
DC
3rule_type: layout
4related_rules:
5- array-bracket-spacing
6- comma-spacing
7- computed-property-spacing
8- space-in-parens
9---
10
11
eb39fafa
DC
12
13While formatting preferences are very personal, a number of style guides require
14or disallow spaces between curly braces in the following situations:
15
16```js
17// simple object literals
18var obj = { foo: "bar" };
19
20// nested object literals
21var obj = { foo: { zoo: "bar" } };
22
23// destructuring assignment (EcmaScript 6)
24var { x, y } = y;
25
26// import/export declarations (EcmaScript 6)
27import { foo } from "bar";
28export { foo };
29```
30
31## Rule Details
32
33This rule enforces consistent spacing inside braces of object literals, destructuring assignments, and import/export specifiers.
34
35## Options
36
37This rule has two options, a string option and an object option.
38
39String option:
40
41* `"never"` (default) disallows spacing inside of braces
42* `"always"` requires spacing inside of braces (except `{}`)
43
44Object 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
53Examples 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
60var obj = { 'foo': 'bar' };
61var obj = {'foo': 'bar' };
62var obj = { baz: {'foo': 'qux'}, bar};
63var obj = {baz: { 'foo': 'qux'}, bar};
64var {x } = y;
65import { foo } from 'bar';
66```
67
8f9d1d4d
DC
68:::
69
eb39fafa
DC
70Examples 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
77var obj = {'foo': 'bar'};
78var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'};
79var obj = {
80 'foo': 'bar'
81};
82var obj = {'foo': 'bar'
83};
84var obj = {
85 'foo':'bar'};
86var obj = {};
87var {x} = y;
88import {foo} from 'bar';
89```
90
8f9d1d4d
DC
91:::
92
eb39fafa
DC
93### always
94
95Examples 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
102var obj = {'foo': 'bar'};
103var obj = {'foo': 'bar' };
104var obj = { baz: {'foo': 'qux'}, bar};
105var obj = {baz: { 'foo': 'qux' }, bar};
106var obj = {'foo': 'bar'
107};
108var obj = {
109 'foo':'bar'};
110var {x} = y;
111import {foo } from 'bar';
112```
113
8f9d1d4d
DC
114:::
115
eb39fafa
DC
116Examples 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
123var obj = {};
124var obj = { 'foo': 'bar' };
125var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' };
126var obj = {
127 'foo': 'bar'
128};
129var { x } = y;
130import { foo } from 'bar';
131```
132
8f9d1d4d
DC
133:::
134
eb39fafa
DC
135#### arraysInObjects
136
137Examples 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
144var obj = {"foo": [ 1, 2 ] };
145var obj = {"foo": [ "baz", "bar" ] };
146```
147
8f9d1d4d
DC
148:::
149
eb39fafa
DC
150Examples 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
157var obj = { "foo": [ 1, 2 ]};
158var obj = { "foo": [ "baz", "bar" ]};
159```
160
8f9d1d4d
DC
161:::
162
eb39fafa
DC
163#### objectsInObjects
164
165Examples 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
172var obj = {"foo": {"baz": 1, "bar": 2} };
173```
174
8f9d1d4d
DC
175:::
176
eb39fafa
DC
177Examples 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
184var obj = { "foo": { "baz": 1, "bar": 2 }};
185```
186
8f9d1d4d
DC
187:::
188
eb39fafa
DC
189## When Not To Use It
190
191You can turn this rule off if you are not concerned with the consistency of spacing between curly braces.