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