0
# Array Formatting
1
2
Rules that control the formatting and styling of JavaScript arrays, including bracket spacing, newlines, and element positioning.
3
4
## array-bracket-newline
5
6
Enforces linebreaks after opening and before closing array brackets.
7
8
```javascript { .api }
9
const arrayBracketNewline: Rule.RuleModule;
10
11
// Rule options
12
type ArrayBracketNewlineOptions =
13
| 'always'
14
| 'never'
15
| 'consistent'
16
| {
17
multiline?: boolean;
18
minItems?: number | null;
19
};
20
```
21
22
**Usage Examples:**
23
24
```javascript
25
// ✓ Good with "always"
26
const items = [
27
'apple',
28
'banana'
29
];
30
31
// ✓ Good with "never"
32
const items = ['apple', 'banana'];
33
34
// ✓ Good with { multiline: true }
35
const items = ['apple', 'banana']; // single line ok
36
const items = [
37
'apple',
38
'banana'
39
]; // multiline requires brackets on new lines
40
```
41
42
**Configuration:**
43
44
```javascript
45
rules: {
46
'@stylistic/js/array-bracket-newline': ['error', 'consistent']
47
}
48
```
49
50
## array-bracket-spacing
51
52
Enforces consistent spacing inside array brackets.
53
54
```javascript { .api }
55
const arrayBracketSpacing: Rule.RuleModule;
56
57
// Rule options
58
type ArrayBracketSpacingOptions =
59
| 'always'
60
| 'never'
61
| ['always' | 'never', {
62
singleValue?: boolean;
63
objectsInArrays?: boolean;
64
arraysInArrays?: boolean;
65
}];
66
```
67
68
**Usage Examples:**
69
70
```javascript
71
// ✓ Good with "never" (default)
72
const items = ['apple', 'banana'];
73
74
// ✓ Good with "always"
75
const items = [ 'apple', 'banana' ];
76
77
// ✓ Good with custom options
78
const items = ['apple', 'banana']; // singleValue: false
79
const nested = [[ 'nested' ]]; // arraysInArrays: false
80
```
81
82
**Configuration:**
83
84
```javascript
85
rules: {
86
'@stylistic/js/array-bracket-spacing': ['error', 'never']
87
}
88
```
89
90
## array-element-newline
91
92
Enforces line breaks after each array element.
93
94
```javascript { .api }
95
const arrayElementNewline: Rule.RuleModule;
96
97
// Rule options
98
type ArrayElementNewlineOptions =
99
| 'always'
100
| 'never'
101
| 'consistent'
102
| {
103
multiline?: boolean;
104
minItems?: number | null;
105
};
106
```
107
108
**Usage Examples:**
109
110
```javascript
111
// ✓ Good with "always"
112
const items = [
113
'apple',
114
'banana',
115
'cherry'
116
];
117
118
// ✓ Good with "never"
119
const items = ['apple', 'banana', 'cherry'];
120
121
// ✓ Good with { minItems: 3 }
122
const small = ['a', 'b']; // under threshold, no newlines required
123
const large = [
124
'apple',
125
'banana',
126
'cherry'
127
]; // meets threshold, newlines required
128
```
129
130
**Configuration:**
131
132
```javascript
133
rules: {
134
'@stylistic/js/array-element-newline': ['error', { minItems: 3 }]
135
}
136
```
137
138
## Common Array Formatting Combinations
139
140
Here are some popular combinations for array formatting:
141
142
### Compact Arrays
143
```javascript
144
rules: {
145
'@stylistic/js/array-bracket-spacing': ['error', 'never'],
146
'@stylistic/js/array-bracket-newline': ['error', 'never'],
147
'@stylistic/js/array-element-newline': ['error', 'never']
148
}
149
150
// Result:
151
const items = ['apple', 'banana', 'cherry'];
152
```
153
154
### Multiline Arrays
155
```javascript
156
rules: {
157
'@stylistic/js/array-bracket-spacing': ['error', 'never'],
158
'@stylistic/js/array-bracket-newline': ['error', { multiline: true }],
159
'@stylistic/js/array-element-newline': ['error', { minItems: 3 }]
160
}
161
162
// Result:
163
const short = ['a', 'b'];
164
const long = [
165
'apple',
166
'banana',
167
'cherry'
168
];
169
```
170
171
## Type Definitions
172
173
```typescript { .api }
174
// Complete type definitions for array rules
175
interface ArrayBracketNewlineRule {
176
meta: {
177
type: 'layout';
178
docs: { description: string };
179
fixable: 'whitespace';
180
schema: JSONSchema4[];
181
};
182
create(context: Rule.RuleContext): Rule.RuleListener;
183
}
184
185
interface ArrayFormattingRules {
186
'array-bracket-newline': ArrayBracketNewlineRule;
187
'array-bracket-spacing': Rule.RuleModule;
188
'array-element-newline': Rule.RuleModule;
189
}
190
```