0
# Object Formatting
1
2
Rules for consistent object literal formatting, including curly brace positioning, property spacing, and multiline object handling.
3
4
## object-curly-newline
5
6
Enforces consistent line breaks after opening and before closing braces.
7
8
```javascript { .api }
9
const objectCurlyNewline: Rule.RuleModule;
10
11
// Rule options
12
type ObjectCurlyNewlineOptions =
13
| 'always'
14
| 'never'
15
| {
16
ObjectExpression?: 'always' | 'never' | ObjectCurlyNewlineConfig;
17
ObjectPattern?: 'always' | 'never' | ObjectCurlyNewlineConfig;
18
ImportDeclaration?: 'always' | 'never' | ObjectCurlyNewlineConfig;
19
ExportDeclaration?: 'always' | 'never' | ObjectCurlyNewlineConfig;
20
};
21
22
interface ObjectCurlyNewlineConfig {
23
multiline?: boolean;
24
minProperties?: number;
25
consistent?: boolean;
26
}
27
```
28
29
**Usage Examples:**
30
31
```javascript
32
// ✓ Good with "always"
33
const obj = {
34
name: 'John',
35
age: 30
36
};
37
38
// ✓ Good with "never"
39
const obj = { name: 'John', age: 30 };
40
41
// ✓ Good with { minProperties: 2 }
42
const small = { name: 'John' };
43
const large = {
44
name: 'John',
45
age: 30
46
};
47
```
48
49
**Configuration:**
50
51
```javascript
52
rules: {
53
'@stylistic/js/object-curly-newline': ['error', { multiline: true, minProperties: 3 }]
54
}
55
```
56
57
## object-curly-spacing
58
59
Enforces consistent spacing inside braces.
60
61
```javascript { .api }
62
const objectCurlySpacing: Rule.RuleModule;
63
64
// Rule options
65
type ObjectCurlySpacingOptions =
66
| 'always'
67
| 'never'
68
| ['always' | 'never', {
69
arraysInObjects?: boolean;
70
objectsInObjects?: boolean;
71
}];
72
```
73
74
**Usage Examples:**
75
76
```javascript
77
// ✓ Good with "never" (default)
78
const obj = {name: 'John', age: 30};
79
80
// ✓ Good with "always"
81
const obj = { name: 'John', age: 30 };
82
83
// ✓ Good with custom options
84
const obj = {items: ['a', 'b']}; // arraysInObjects: false
85
const nested = {user: {name: 'John'}}; // objectsInObjects: false
86
```
87
88
**Configuration:**
89
90
```javascript
91
rules: {
92
'@stylistic/js/object-curly-spacing': ['error', 'always']
93
}
94
```
95
96
## object-property-newline
97
98
Enforces placing object properties on separate lines.
99
100
```javascript { .api }
101
const objectPropertyNewline: Rule.RuleModule;
102
103
// Rule options
104
type ObjectPropertyNewlineOptions = {
105
allowAllPropertiesOnSameLine?: boolean;
106
allowMultiplePropertiesPerLine?: boolean;
107
};
108
```
109
110
**Usage Examples:**
111
112
```javascript
113
// ✓ Good with default settings
114
const obj = {
115
name: 'John',
116
age: 30,
117
city: 'New York'
118
};
119
120
// ✓ Good with { allowAllPropertiesOnSameLine: true }
121
const obj = { name: 'John', age: 30 }; // all on same line OK
122
const obj = {
123
name: 'John',
124
age: 30
125
}; // or separate lines OK
126
127
// ✗ Bad mixing styles
128
const obj = { name: 'John',
129
age: 30 }; // inconsistent
130
```
131
132
**Configuration:**
133
134
```javascript
135
rules: {
136
'@stylistic/js/object-property-newline': ['error', { allowAllPropertiesOnSameLine: true }]
137
}
138
```
139
140
## computed-property-spacing
141
142
Enforces consistent spacing inside computed property brackets.
143
144
```javascript { .api }
145
const computedPropertySpacing: Rule.RuleModule;
146
147
// Rule options
148
type ComputedPropertySpacingOptions =
149
| 'always'
150
| 'never'
151
| ['always' | 'never', {
152
enforceForClassMembers?: boolean;
153
}];
154
```
155
156
**Usage Examples:**
157
158
```javascript
159
// ✓ Good with "never" (default)
160
const value = obj[key];
161
const obj = { [key]: value };
162
163
// ✓ Good with "always"
164
const value = obj[ key ];
165
const obj = { [ key ]: value };
166
167
// Class members with enforceForClassMembers: true
168
class MyClass {
169
[ key ]() {} // follows same spacing rule
170
}
171
```
172
173
**Configuration:**
174
175
```javascript
176
rules: {
177
'@stylistic/js/computed-property-spacing': ['error', 'never']
178
}
179
```
180
181
## key-spacing
182
183
Enforces consistent spacing between keys and values in object literals.
184
185
```javascript { .api }
186
const keySpacing: Rule.RuleModule;
187
188
// Rule options
189
type KeySpacingOptions = {
190
beforeColon?: boolean;
191
afterColon?: boolean;
192
mode?: 'strict' | 'minimum';
193
align?: 'value' | 'colon' | {
194
beforeColon?: boolean;
195
afterColon?: boolean;
196
on?: 'value' | 'colon';
197
mode?: 'strict' | 'minimum';
198
};
199
singleLine?: KeySpacingConfig;
200
multiLine?: KeySpacingConfig;
201
};
202
```
203
204
**Usage Examples:**
205
206
```javascript
207
// ✓ Good with default settings
208
const obj = {
209
name: 'John',
210
age: 30
211
};
212
213
// ✓ Good with { align: 'value' }
214
const obj = {
215
name : 'John',
216
age : 30,
217
address : '123 Main St'
218
};
219
220
// ✓ Good with { beforeColon: true, afterColon: true }
221
const obj = {
222
name : 'John',
223
age : 30
224
};
225
```
226
227
**Configuration:**
228
229
```javascript
230
rules: {
231
'@stylistic/js/key-spacing': ['error', { beforeColon: false, afterColon: true }]
232
}
233
```
234
235
## Common Object Formatting Combinations
236
237
### Compact Objects
238
```javascript
239
rules: {
240
'@stylistic/js/object-curly-spacing': ['error', 'never'],
241
'@stylistic/js/object-curly-newline': ['error', 'never'],
242
'@stylistic/js/key-spacing': ['error', { beforeColon: false, afterColon: true }]
243
}
244
245
// Result:
246
const obj = {name: 'John', age: 30};
247
```
248
249
### Spaced Objects
250
```javascript
251
rules: {
252
'@stylistic/js/object-curly-spacing': ['error', 'always'],
253
'@stylistic/js/object-curly-newline': ['error', { multiline: true }],
254
'@stylistic/js/object-property-newline': ['error', { allowAllPropertiesOnSameLine: true }]
255
}
256
257
// Result:
258
const obj = { name: 'John', age: 30 };
259
const large = {
260
name: 'John',
261
age: 30,
262
address: '123 Main St'
263
};
264
```
265
266
## Type Definitions
267
268
```typescript { .api }
269
interface ObjectFormattingRules {
270
'object-curly-newline': Rule.RuleModule;
271
'object-curly-spacing': Rule.RuleModule;
272
'object-property-newline': Rule.RuleModule;
273
'computed-property-spacing': Rule.RuleModule;
274
'key-spacing': Rule.RuleModule;
275
}
276
277
interface KeySpacingConfig {
278
beforeColon?: boolean;
279
afterColon?: boolean;
280
mode?: 'strict' | 'minimum';
281
}
282
```