]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/array-bracket-newline.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / array-bracket-newline.md
CommitLineData
8f9d1d4d
DC
1---
2title: array-bracket-newline
8f9d1d4d
DC
3rule_type: layout
4related_rules:
5- array-bracket-spacing
6---
7
8
eb39fafa
DC
9
10A number of style guides require or disallow line breaks inside of array brackets.
11
12## Rule Details
13
14This rule enforces line breaks after opening and before closing array brackets.
15
16## Options
17
18This 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
24Or 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
31Examples 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
38var a = [];
39var b = [1];
40var c = [1, 2];
41var d = [1,
42 2];
43var e = [function foo() {
44 dosomething();
45}];
46```
47
8f9d1d4d
DC
48:::
49
eb39fafa
DC
50Examples 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
57var a = [
58];
59var b = [
60 1
61];
62var c = [
63 1, 2
64];
65var d = [
66 1,
67 2
68];
69var e = [
70 function foo() {
71 dosomething();
72 }
73];
74```
75
8f9d1d4d
DC
76:::
77
eb39fafa
DC
78### never
79
80Examples 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
87var a = [
88];
89var b = [
90 1
91];
92var c = [
93 1, 2
94];
95var d = [
96 1,
97 2
98];
99var e = [
100 function foo() {
101 dosomething();
102 }
103];
104```
105
8f9d1d4d
DC
106:::
107
eb39fafa
DC
108Examples 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
115var a = [];
116var b = [1];
117var c = [1, 2];
118var d = [1,
119 2];
120var e = [function foo() {
121 dosomething();
122}];
123```
124
8f9d1d4d
DC
125:::
126
eb39fafa
DC
127### consistent
128
129Examples 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
136var a = [1
137];
138var b = [
139 1];
140var c = [function foo() {
141 dosomething();
142}
143]
144var d = [
145 function foo() {
146 dosomething();
147 }]
148```
149
8f9d1d4d
DC
150:::
151
eb39fafa
DC
152Examples 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
159var a = [];
160var b = [
161];
162var c = [1];
163var d = [
164 1
165];
166var e = [function foo() {
167 dosomething();
168}];
169var f = [
170 function foo() {
171 dosomething();
172 }
173];
174```
175
8f9d1d4d
DC
176:::
177
eb39fafa
DC
178### multiline
179
180Examples 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
187var a = [
188];
189var b = [
190 1
191];
192var c = [
193 1, 2
194];
195var d = [1,
196 2];
197var e = [function foo() {
198 dosomething();
199}];
200```
201
8f9d1d4d
DC
202:::
203
eb39fafa
DC
204Examples 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
211var a = [];
212var b = [1];
213var c = [1, 2];
214var d = [
215 1,
216 2
217];
218var e = [
219 function foo() {
220 dosomething();
221 }
222];
223```
224
8f9d1d4d
DC
225:::
226
eb39fafa
DC
227### minItems
228
229Examples 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
236var a = [
237];
238var b = [
239 1
240];
241var c = [1, 2];
242var d = [1,
243 2];
244var e = [
245 function foo() {
246 dosomething();
247 }
248];
249```
250
8f9d1d4d
DC
251:::
252
eb39fafa
DC
253Examples 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
260var a = [];
261var b = [1];
262var c = [
263 1, 2
264];
265var d = [
266 1,
267 2
268];
269var e = [function foo() {
270 dosomething();
271}];
272```
273
8f9d1d4d
DC
274:::
275
eb39fafa
DC
276### multiline and minItems
277
278Examples 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
285var a = [
286];
287var b = [
288 1
289];
290var c = [1, 2];
291var d = [1,
292 2];
293var e = [function foo() {
294 dosomething();
295}];
296```
297
8f9d1d4d
DC
298:::
299
eb39fafa
DC
300Examples 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
307var a = [];
308var b = [1];
309var c = [
310 1, 2
311];
312var d = [
313 1,
314 2
315];
316var e = [
317 function foo() {
318 dosomething();
319 }
320];
321```
322
8f9d1d4d
DC
323:::
324
eb39fafa
DC
325## When Not To Use It
326
327If 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)