]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: array-bracket-newline | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | related_rules: | |
5 | - array-bracket-spacing | |
6 | --- | |
7 | ||
8 | ||
eb39fafa DC |
9 | |
10 | A number of style guides require or disallow line breaks inside of array brackets. | |
11 | ||
12 | ## Rule Details | |
13 | ||
14 | This rule enforces line breaks after opening and before closing array brackets. | |
15 | ||
16 | ## Options | |
17 | ||
18 | This rule has either a string option: | |
19 | ||
20 | * `"always"` requires line breaks inside brackets | |
21 | * `"never"` disallows line breaks inside brackets | |
22 | * `"consistent"` requires consistent usage of linebreaks for each pair of brackets. It reports an error if one bracket in the pair has a linebreak inside it and the other bracket does not. | |
23 | ||
24 | Or an object option (Requires line breaks if any of properties is satisfied. Otherwise, disallows line breaks): | |
25 | ||
26 | * `"multiline": true` (default) requires line breaks if there are line breaks inside elements or between elements. If this is false, this condition is disabled. | |
27 | * `"minItems": null` (default) requires line breaks if the number of elements is at least the given integer. If this is 0, this condition will act the same as the option `"always"`. If this is `null` (the default), this condition is disabled. | |
28 | ||
29 | ### always | |
30 | ||
31 | Examples of **incorrect** code for this rule with the `"always"` option: | |
32 | ||
8f9d1d4d DC |
33 | :::incorrect |
34 | ||
eb39fafa DC |
35 | ```js |
36 | /*eslint array-bracket-newline: ["error", "always"]*/ | |
37 | ||
38 | var a = []; | |
39 | var b = [1]; | |
40 | var c = [1, 2]; | |
41 | var d = [1, | |
42 | 2]; | |
43 | var e = [function foo() { | |
44 | dosomething(); | |
45 | }]; | |
46 | ``` | |
47 | ||
8f9d1d4d DC |
48 | ::: |
49 | ||
eb39fafa DC |
50 | Examples of **correct** code for this rule with the `"always"` option: |
51 | ||
8f9d1d4d DC |
52 | :::correct |
53 | ||
eb39fafa DC |
54 | ```js |
55 | /*eslint array-bracket-newline: ["error", "always"]*/ | |
56 | ||
57 | var a = [ | |
58 | ]; | |
59 | var b = [ | |
60 | 1 | |
61 | ]; | |
62 | var c = [ | |
63 | 1, 2 | |
64 | ]; | |
65 | var d = [ | |
66 | 1, | |
67 | 2 | |
68 | ]; | |
69 | var e = [ | |
70 | function foo() { | |
71 | dosomething(); | |
72 | } | |
73 | ]; | |
74 | ``` | |
75 | ||
8f9d1d4d DC |
76 | ::: |
77 | ||
eb39fafa DC |
78 | ### never |
79 | ||
80 | Examples of **incorrect** code for this rule with the `"never"` option: | |
81 | ||
8f9d1d4d DC |
82 | :::incorrect |
83 | ||
eb39fafa DC |
84 | ```js |
85 | /*eslint array-bracket-newline: ["error", "never"]*/ | |
86 | ||
87 | var a = [ | |
88 | ]; | |
89 | var b = [ | |
90 | 1 | |
91 | ]; | |
92 | var c = [ | |
93 | 1, 2 | |
94 | ]; | |
95 | var d = [ | |
96 | 1, | |
97 | 2 | |
98 | ]; | |
99 | var e = [ | |
100 | function foo() { | |
101 | dosomething(); | |
102 | } | |
103 | ]; | |
104 | ``` | |
105 | ||
8f9d1d4d DC |
106 | ::: |
107 | ||
eb39fafa DC |
108 | Examples of **correct** code for this rule with the `"never"` option: |
109 | ||
8f9d1d4d DC |
110 | :::correct |
111 | ||
eb39fafa DC |
112 | ```js |
113 | /*eslint array-bracket-newline: ["error", "never"]*/ | |
114 | ||
115 | var a = []; | |
116 | var b = [1]; | |
117 | var c = [1, 2]; | |
118 | var d = [1, | |
119 | 2]; | |
120 | var e = [function foo() { | |
121 | dosomething(); | |
122 | }]; | |
123 | ``` | |
124 | ||
8f9d1d4d DC |
125 | ::: |
126 | ||
eb39fafa DC |
127 | ### consistent |
128 | ||
129 | Examples of **incorrect** code for this rule with the `"consistent"` option: | |
130 | ||
8f9d1d4d DC |
131 | :::incorrect |
132 | ||
eb39fafa DC |
133 | ```js |
134 | /*eslint array-bracket-newline: ["error", "consistent"]*/ | |
135 | ||
136 | var a = [1 | |
137 | ]; | |
138 | var b = [ | |
139 | 1]; | |
140 | var c = [function foo() { | |
141 | dosomething(); | |
142 | } | |
143 | ] | |
144 | var d = [ | |
145 | function foo() { | |
146 | dosomething(); | |
147 | }] | |
148 | ``` | |
149 | ||
8f9d1d4d DC |
150 | ::: |
151 | ||
eb39fafa DC |
152 | Examples of **correct** code for this rule with the `"consistent"` option: |
153 | ||
8f9d1d4d DC |
154 | :::correct |
155 | ||
eb39fafa DC |
156 | ```js |
157 | /*eslint array-bracket-newline: ["error", "consistent"]*/ | |
158 | ||
159 | var a = []; | |
160 | var b = [ | |
161 | ]; | |
162 | var c = [1]; | |
163 | var d = [ | |
164 | 1 | |
165 | ]; | |
166 | var e = [function foo() { | |
167 | dosomething(); | |
168 | }]; | |
169 | var f = [ | |
170 | function foo() { | |
171 | dosomething(); | |
172 | } | |
173 | ]; | |
174 | ``` | |
175 | ||
8f9d1d4d DC |
176 | ::: |
177 | ||
eb39fafa DC |
178 | ### multiline |
179 | ||
180 | Examples of **incorrect** code for this rule with the default `{ "multiline": true }` option: | |
181 | ||
8f9d1d4d DC |
182 | :::incorrect |
183 | ||
eb39fafa DC |
184 | ```js |
185 | /*eslint array-bracket-newline: ["error", { "multiline": true }]*/ | |
186 | ||
187 | var a = [ | |
188 | ]; | |
189 | var b = [ | |
190 | 1 | |
191 | ]; | |
192 | var c = [ | |
193 | 1, 2 | |
194 | ]; | |
195 | var d = [1, | |
196 | 2]; | |
197 | var e = [function foo() { | |
198 | dosomething(); | |
199 | }]; | |
200 | ``` | |
201 | ||
8f9d1d4d DC |
202 | ::: |
203 | ||
eb39fafa DC |
204 | Examples of **correct** code for this rule with the default `{ "multiline": true }` option: |
205 | ||
8f9d1d4d DC |
206 | :::correct |
207 | ||
eb39fafa DC |
208 | ```js |
209 | /*eslint array-bracket-newline: ["error", { "multiline": true }]*/ | |
210 | ||
211 | var a = []; | |
212 | var b = [1]; | |
213 | var c = [1, 2]; | |
214 | var d = [ | |
215 | 1, | |
216 | 2 | |
217 | ]; | |
218 | var e = [ | |
219 | function foo() { | |
220 | dosomething(); | |
221 | } | |
222 | ]; | |
223 | ``` | |
224 | ||
8f9d1d4d DC |
225 | ::: |
226 | ||
eb39fafa DC |
227 | ### minItems |
228 | ||
229 | Examples of **incorrect** code for this rule with the `{ "minItems": 2 }` option: | |
230 | ||
8f9d1d4d DC |
231 | :::incorrect |
232 | ||
eb39fafa DC |
233 | ```js |
234 | /*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/ | |
235 | ||
236 | var a = [ | |
237 | ]; | |
238 | var b = [ | |
239 | 1 | |
240 | ]; | |
241 | var c = [1, 2]; | |
242 | var d = [1, | |
243 | 2]; | |
244 | var e = [ | |
245 | function foo() { | |
246 | dosomething(); | |
247 | } | |
248 | ]; | |
249 | ``` | |
250 | ||
8f9d1d4d DC |
251 | ::: |
252 | ||
eb39fafa DC |
253 | Examples of **correct** code for this rule with the `{ "minItems": 2 }` option: |
254 | ||
8f9d1d4d DC |
255 | :::correct |
256 | ||
eb39fafa DC |
257 | ```js |
258 | /*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/ | |
259 | ||
260 | var a = []; | |
261 | var b = [1]; | |
262 | var c = [ | |
263 | 1, 2 | |
264 | ]; | |
265 | var d = [ | |
266 | 1, | |
267 | 2 | |
268 | ]; | |
269 | var e = [function foo() { | |
270 | dosomething(); | |
271 | }]; | |
272 | ``` | |
273 | ||
8f9d1d4d DC |
274 | ::: |
275 | ||
eb39fafa DC |
276 | ### multiline and minItems |
277 | ||
278 | Examples of **incorrect** code for this rule with the `{ "multiline": true, "minItems": 2 }` options: | |
279 | ||
8f9d1d4d DC |
280 | :::incorrect |
281 | ||
eb39fafa DC |
282 | ```js |
283 | /*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/ | |
284 | ||
285 | var a = [ | |
286 | ]; | |
287 | var b = [ | |
288 | 1 | |
289 | ]; | |
290 | var c = [1, 2]; | |
291 | var d = [1, | |
292 | 2]; | |
293 | var e = [function foo() { | |
294 | dosomething(); | |
295 | }]; | |
296 | ``` | |
297 | ||
8f9d1d4d DC |
298 | ::: |
299 | ||
eb39fafa DC |
300 | Examples of **correct** code for this rule with the `{ "multiline": true, "minItems": 2 }` options: |
301 | ||
8f9d1d4d DC |
302 | :::correct |
303 | ||
eb39fafa DC |
304 | ```js |
305 | /*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/ | |
306 | ||
307 | var a = []; | |
308 | var b = [1]; | |
309 | var c = [ | |
310 | 1, 2 | |
311 | ]; | |
312 | var d = [ | |
313 | 1, | |
314 | 2 | |
315 | ]; | |
316 | var e = [ | |
317 | function foo() { | |
318 | dosomething(); | |
319 | } | |
320 | ]; | |
321 | ``` | |
322 | ||
8f9d1d4d DC |
323 | ::: |
324 | ||
eb39fafa DC |
325 | ## When Not To Use It |
326 | ||
327 | If you don't want to enforce line breaks after opening and before closing array brackets, don't enable this rule. | |
328 | ||
329 | ## Compatibility | |
330 | ||
331 | * **JSCS:** [validateNewlineAfterArrayElements](https://jscs-dev.github.io/rule/validateNewlineAfterArrayElements) |