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