]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/arrow-parens.js
update to 7.1.0 sources
[pve-eslint.git] / eslint / tests / lib / rules / arrow-parens.js
1 /**
2 * @fileoverview Tests for arrow-parens
3 * @author Jxck
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const baseParser = require("../../fixtures/fixture-parser"),
13 rule = require("../../../lib/rules/arrow-parens"),
14 { RuleTester } = require("../../../lib/rule-tester");
15
16 const parser = baseParser.bind(null, "arrow-parens");
17
18 //------------------------------------------------------------------------------
19 // Tests
20 //------------------------------------------------------------------------------
21 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
22
23 const valid = [
24
25 // "always" (by default)
26 "() => {}",
27 "(a) => {}",
28 "(a) => a",
29 "(a) => {\n}",
30 "a.then((foo) => {});",
31 "a.then((foo) => { if (true) {}; });",
32 "const f = (/* */a) => a + a;",
33 "const f = (a/** */) => a + a;",
34 "const f = (a//\n) => a + a;",
35 "const f = (//\na) => a + a;",
36 "const f = (/*\n */a//\n) => a + a;",
37 "const f = (/** @type {number} */a/**hello*/) => a + a;",
38 { code: "a.then(async (foo) => { if (true) {}; });", parserOptions: { ecmaVersion: 8 } },
39
40 // "always" (explicit)
41 { code: "() => {}", options: ["always"] },
42 { code: "(a) => {}", options: ["always"] },
43 { code: "(a) => a", options: ["always"] },
44 { code: "(a) => {\n}", options: ["always"] },
45 { code: "a.then((foo) => {});", options: ["always"] },
46 { code: "a.then((foo) => { if (true) {}; });", options: ["always"] },
47 { code: "a.then(async (foo) => { if (true) {}; });", options: ["always"], parserOptions: { ecmaVersion: 8 } },
48
49 // "as-needed"
50 { code: "() => {}", options: ["as-needed"] },
51 { code: "a => {}", options: ["as-needed"] },
52 { code: "a => a", options: ["as-needed"] },
53 { code: "([a, b]) => {}", options: ["as-needed"] },
54 { code: "({ a, b }) => {}", options: ["as-needed"] },
55 { code: "(a = 10) => {}", options: ["as-needed"] },
56 { code: "(...a) => a[0]", options: ["as-needed"] },
57 { code: "(a, b) => {}", options: ["as-needed"] },
58 { code: "async ([a, b]) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },
59 { code: "async (a, b) => {}", options: ["as-needed"], parserOptions: { ecmaVersion: 8 } },
60 { code: "(a: T) => a", options: ["as-needed"], parser: parser("identifer-type") },
61 { code: "(a): T => a", options: ["as-needed"], parser: parser("return-type") },
62
63 // "as-needed", { "requireForBlockBody": true }
64 { code: "() => {}", options: ["as-needed", { requireForBlockBody: true }] },
65 { code: "a => a", options: ["as-needed", { requireForBlockBody: true }] },
66 { code: "([a, b]) => {}", options: ["as-needed", { requireForBlockBody: true }] },
67 { code: "([a, b]) => a", options: ["as-needed", { requireForBlockBody: true }] },
68 { code: "({ a, b }) => {}", options: ["as-needed", { requireForBlockBody: true }] },
69 { code: "({ a, b }) => a + b", options: ["as-needed", { requireForBlockBody: true }] },
70 { code: "(a = 10) => {}", options: ["as-needed", { requireForBlockBody: true }] },
71 { code: "(...a) => a[0]", options: ["as-needed", { requireForBlockBody: true }] },
72 { code: "(a, b) => {}", options: ["as-needed", { requireForBlockBody: true }] },
73 { code: "a => ({})", options: ["as-needed", { requireForBlockBody: true }] },
74 { code: "async a => ({})", options: ["as-needed", { requireForBlockBody: true }], parserOptions: { ecmaVersion: 8 } },
75 { code: "async a => a", options: ["as-needed", { requireForBlockBody: true }], parserOptions: { ecmaVersion: 8 } },
76 { code: "(a: T) => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("identifer-type") },
77 { code: "(a): T => a", options: ["as-needed", { requireForBlockBody: true }], parser: parser("return-type") },
78 {
79 code: "const f = (/** @type {number} */a/**hello*/) => a + a;",
80 options: ["as-needed"]
81 },
82 {
83 code: "const f = (/* */a) => a + a;",
84 options: ["as-needed"]
85 },
86 {
87 code: "const f = (a/** */) => a + a;",
88 options: ["as-needed"]
89 },
90 {
91 code: "const f = (a//\n) => a + a;",
92 options: ["as-needed"]
93 },
94 {
95 code: "const f = (//\na) => a + a;",
96 options: ["as-needed"]
97 },
98 {
99 code: "const f = (/*\n */a//\n) => a + a;",
100 options: ["as-needed"]
101 },
102 {
103 code: "var foo = (a,/**/) => b;",
104 parserOptions: { ecmaVersion: 2017 },
105 options: ["as-needed"]
106 },
107 {
108 code: "var foo = (a , /**/) => b;",
109 parserOptions: { ecmaVersion: 2017 },
110 options: ["as-needed"]
111 },
112 {
113 code: "var foo = (a\n,\n/**/) => b;",
114 parserOptions: { ecmaVersion: 2017 },
115 options: ["as-needed"]
116 },
117 {
118 code: "var foo = (a,//\n) => b;",
119 parserOptions: { ecmaVersion: 2017 },
120 options: ["as-needed"]
121 },
122 {
123 code: "const i = (a/**/,) => a + a;",
124 parserOptions: { ecmaVersion: 2017 },
125 options: ["as-needed"]
126 },
127 {
128 code: "const i = (a \n /**/,) => a + a;",
129 parserOptions: { ecmaVersion: 2017 },
130 options: ["as-needed"]
131 },
132 {
133 code: "var bar = ({/*comment here*/a}) => a",
134 options: ["as-needed"]
135 },
136 {
137 code: "var bar = (/*comment here*/{a}) => a",
138 options: ["as-needed"]
139 }
140 ];
141
142 const type = "ArrowFunctionExpression";
143
144 const invalid = [
145
146 // "always" (by default)
147 {
148 code: "a => {}",
149 output: "(a) => {}",
150 errors: [{
151 line: 1,
152 column: 1,
153 endColumn: 2,
154 messageId: "expectedParens",
155 type
156 }]
157 },
158 {
159 code: "a => a",
160 output: "(a) => a",
161 errors: [{
162 line: 1,
163 column: 1,
164 endColumn: 2,
165 messageId: "expectedParens",
166 type
167 }]
168 },
169 {
170 code: "a => {\n}",
171 output: "(a) => {\n}",
172 errors: [{
173 line: 1,
174 column: 1,
175 endColumn: 2,
176 messageId: "expectedParens",
177 type
178 }]
179 },
180 {
181 code: "a.then(foo => {});",
182 output: "a.then((foo) => {});",
183 errors: [{
184 line: 1,
185 column: 8,
186 endColumn: 11,
187 messageId: "expectedParens",
188 type
189 }]
190 },
191 {
192 code: "a.then(foo => a);",
193 output: "a.then((foo) => a);",
194 errors: [{
195 line: 1,
196 column: 8,
197 endColumn: 11,
198 messageId: "expectedParens",
199 type
200 }]
201 },
202 {
203 code: "a(foo => { if (true) {}; });",
204 output: "a((foo) => { if (true) {}; });",
205 errors: [{
206 line: 1,
207 column: 3,
208 endColumn: 6,
209 messageId: "expectedParens",
210 type
211 }]
212 },
213 {
214 code: "a(async foo => { if (true) {}; });",
215 output: "a(async (foo) => { if (true) {}; });",
216 parserOptions: { ecmaVersion: 8 },
217 errors: [{
218 line: 1,
219 column: 9,
220 endColumn: 12,
221 messageId: "expectedParens",
222 type
223 }]
224 },
225
226 // "as-needed"
227 {
228 code: "(a) => a",
229 output: "a => a",
230 options: ["as-needed"],
231 errors: [{
232 line: 1,
233 column: 2,
234 endColumn: 3,
235 messageId: "unexpectedParens",
236 type
237 }]
238 },
239 {
240 code: "(a,) => a",
241 output: "a => a",
242 options: ["as-needed"],
243 parserOptions: { ecmaVersion: 8 },
244 errors: [{
245 line: 1,
246 column: 2,
247 endColumn: 3,
248 messageId: "unexpectedParens",
249 type
250 }]
251 },
252 {
253 code: "async (a) => a",
254 output: "async a => a",
255 options: ["as-needed"],
256 parserOptions: { ecmaVersion: 8 },
257 errors: [{
258 line: 1,
259 column: 8,
260 endColumn: 9,
261 messageId: "unexpectedParens",
262 type
263 }]
264 },
265 {
266 code: "async(a) => a",
267 output: "async a => a",
268 options: ["as-needed"],
269 parserOptions: { ecmaVersion: 8 },
270 errors: [{
271 line: 1,
272 column: 7,
273 endColumn: 8,
274 messageId: "unexpectedParens",
275 type
276 }]
277 },
278
279 // "as-needed", { "requireForBlockBody": true }
280 {
281 code: "a => {}",
282 output: "(a) => {}",
283 options: ["as-needed", { requireForBlockBody: true }],
284 errors: [{
285 line: 1,
286 column: 1,
287 endColumn: 2,
288 messageId: "expectedParensBlock",
289 type
290 }]
291 },
292 {
293 code: "(a) => a",
294 output: "a => a",
295 options: ["as-needed", { requireForBlockBody: true }],
296 errors: [{
297 line: 1,
298 column: 2,
299 endColumn: 3,
300 messageId: "unexpectedParensInline",
301 type
302 }]
303 },
304 {
305 code: "async a => {}",
306 output: "async (a) => {}",
307 options: ["as-needed", { requireForBlockBody: true }],
308 parserOptions: { ecmaVersion: 8 },
309 errors: [{
310 line: 1,
311 column: 7,
312 endColumn: 8,
313 messageId: "expectedParensBlock",
314 type
315 }]
316 },
317 {
318 code: "async (a) => a",
319 output: "async a => a",
320 options: ["as-needed", { requireForBlockBody: true }],
321 parserOptions: { ecmaVersion: 8 },
322 errors: [{
323 line: 1,
324 column: 8,
325 endColumn: 9,
326 messageId: "unexpectedParensInline",
327 type
328 }]
329 },
330 {
331 code: "async(a) => a",
332 output: "async a => a",
333 options: ["as-needed", { requireForBlockBody: true }],
334 parserOptions: { ecmaVersion: 8 },
335 errors: [{
336 line: 1,
337 column: 7,
338 endColumn: 8,
339 messageId: "unexpectedParensInline",
340 type
341 }]
342 },
343 {
344 code: "const f = /** @type {number} */(a)/**hello*/ => a + a;",
345 options: ["as-needed"],
346 output: "const f = /** @type {number} */a/**hello*/ => a + a;",
347 errors: [{
348 line: 1,
349 column: 33,
350 type,
351 messageId: "unexpectedParens",
352 endLine: 1,
353 endColumn: 34
354 }]
355 },
356 {
357 code: "const f = //\n(a) => a + a;",
358 output: "const f = //\na => a + a;",
359 options: ["as-needed"],
360 errors: [{
361 line: 2,
362 column: 2,
363 type,
364 messageId: "unexpectedParens",
365 endLine: 2,
366 endColumn: 3
367 }]
368 },
369 {
370 code: "var foo = /**/ a => b;",
371 output: "var foo = /**/ (a) => b;",
372 errors: [{
373 line: 1,
374 column: 16,
375 type: "ArrowFunctionExpression",
376 messageId: "expectedParens",
377 endLine: 1,
378 endColumn: 17
379 }]
380 },
381 {
382 code: "var bar = a /**/ => b;",
383 output: "var bar = (a) /**/ => b;",
384 errors: [{
385 line: 1,
386 column: 11,
387 type: "ArrowFunctionExpression",
388 messageId: "expectedParens",
389 endLine: 1,
390 endColumn: 12
391 }]
392 },
393 {
394 code: `const foo = a => {};
395
396 // comment between 'a' and an unrelated closing paren
397
398 bar();`,
399 output: `const foo = (a) => {};
400
401 // comment between 'a' and an unrelated closing paren
402
403 bar();`,
404 errors: [{
405 line: 1,
406 column: 13,
407 type: "ArrowFunctionExpression",
408 messageId: "expectedParens",
409 endLine: 1,
410 endColumn: 14
411 }]
412 }
413
414 ];
415
416 ruleTester.run("arrow-parens", rule, {
417 valid,
418 invalid
419 });