]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-extra-parens.js
upgrade to v7.0.0
[pve-eslint.git] / eslint / tests / lib / rules / no-extra-parens.js
1 /**
2 * @fileoverview Disallow parenthesesisng higher precedence subexpressions.
3 * @author Michael Ficarra
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-extra-parens"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 /**
16 * Create error message object for failure cases
17 * @param {string} code source code
18 * @param {string} output fixed source code
19 * @param {string} type node type
20 * @param {int} line line number
21 * @param {Object} config rule configuration
22 * @returns {Object} result object
23 * @private
24 */
25 function invalid(code, output, type, line, config) {
26 const result = {
27 code,
28 output,
29 parserOptions: config && config.parserOptions || {},
30 errors: [
31 {
32 messageId: "unexpected",
33 type
34 }
35 ],
36 options: config && config.options || []
37 };
38
39 if (line) {
40 result.errors[0].line = line;
41 }
42 return result;
43 }
44
45 const ruleTester = new RuleTester({
46 parserOptions: {
47 ecmaVersion: 2020,
48 ecmaFeatures: {
49 jsx: true
50 }
51 }
52 });
53
54 ruleTester.run("no-extra-parens", rule, {
55 valid: [
56
57 // all precedence boundaries
58 "foo",
59 "a = b, c = d",
60 "a = b ? c : d",
61 "a = (b, c)",
62 "a || b ? c = d : e = f",
63 "(a = b) ? (c, d) : (e, f)",
64 "a && b || c && d",
65 "(a ? b : c) || (d ? e : f)",
66 "a | b && c | d",
67 "(a || b) && (c || d)",
68 "a ^ b | c ^ d",
69 "(a && b) | (c && d)",
70 "a & b ^ c & d",
71 "(a | b) ^ (c | d)",
72 "a == b & c != d",
73 "(a ^ b) & (c ^ d)",
74 "a < b === c in d",
75 "(a & b) !== (c & d)",
76 "a << b >= c >>> d",
77 "(a == b) instanceof (c != d)",
78 "a + b << c - d",
79 "(a <= b) >> (c > d)",
80 "a * b + c / d",
81 "(a << b) - (c >> d)",
82 "+a % !b",
83 "(a + b) * (c - d)",
84 "-void+delete~typeof!a",
85 "!(a * b); typeof (a / b); +(a % b); delete (a * b); ~(a / b); void (a % b); -(a * b)",
86 "a(b = c, (d, e))",
87 "(++a)(b); (c++)(d);",
88 "new (A())",
89 "new (foo.Baz().foo)",
90 "new (foo.baz.bar().foo.baz)",
91 "new ({}.baz.bar.foo().baz)",
92 "new (doSomething().baz.bar().foo)",
93 "new ([][0].baz.foo().bar.foo)",
94 "new (foo\n.baz\n.bar()\n.foo.baz)",
95 "new A()()",
96 "(new A)()",
97 "(new (Foo || Bar))()",
98 "(new new foo())()",
99 "new (new new foo())(bar)",
100 "(new foo).bar",
101 "(new foo)[bar]",
102 "(new foo).bar.baz",
103 "(new foo.bar).baz",
104 "(new foo).bar()",
105 "(new foo.bar).baz()",
106 "new (new foo).bar",
107 "new (new foo.bar).baz",
108 "(new new foo()).baz",
109 "(2 + 3) ** 4",
110 "2 ** (2 + 3)",
111 "new (import(source))",
112 "import((s,t))",
113
114 // same precedence
115 "a, b, c",
116 "a = b = c",
117 "a ? b ? c : d : e",
118 "a ? b : c ? d : e",
119 "a || b || c",
120 "a || (b || c)",
121 "a && b && c",
122 "a && (b && c)",
123 "a | b | c",
124 "a | (b | c)",
125 "a ^ b ^ c",
126 "a ^ (b ^ c)",
127 "a & b & c",
128 "a & (b & c)",
129 "a == b == c",
130 "a == (b == c)",
131 "a < b < c",
132 "a < (b < c)",
133 "a << b << c",
134 "a << (b << c)",
135 "a + b + c",
136 "a + (b + c)",
137 "a * b * c",
138 "a * (b * c)",
139 "!!a; typeof +b; void -c; ~delete d;",
140 "a(b)",
141 "a(b)(c)",
142 "a((b, c))",
143 "new new A",
144 "2 ** 3 ** 4",
145 "(2 ** 3) ** 4",
146
147 // constructs that contain expressions
148 "if(a);",
149 "with(a){}",
150 "switch(a){ case 0: break; }",
151 "function a(){ return b; }",
152 "var a = () => { return b; }",
153 "throw a;",
154 "while(a);",
155 "do; while(a);",
156 "for(;;);",
157 "for(a in b);",
158 "for(a in b, c);",
159 "for(a of b);",
160 "for (a of (b, c));",
161 "var a = (b, c);",
162 "[]",
163 "[a, b]",
164 "!{a}",
165 "!{a: 0, b: 1}",
166 "!{[a]:0}",
167 "!{[(a, b)]:0}",
168 "!{a, ...b}",
169 "const {a} = {}",
170 "const {a:b} = {}",
171 "const {a:b=1} = {}",
172 "const {[a]:b} = {}",
173 "const {[a]:b=1} = {}",
174 "const {[(a, b)]:c} = {}",
175 "const {a, ...b} = {}",
176 "class foo {}",
177 "class foo { constructor(){} a(){} get b(){} set b(bar){} get c(){} set d(baz){} static e(){} }",
178 "class foo { [a](){} get [b](){} set [b](bar){} get [c](){} set [d](baz){} static [e](){} }",
179 "class foo { [(a,b)](){} }",
180 "class foo { a(){} [b](){} c(){} [(d,e)](){} }",
181 "class foo { [(a,b)](){} c(){} [d](){} e(){} }",
182 "const foo = class { constructor(){} a(){} get b(){} set b(bar){} get c(){} set d(baz){} static e(){} }",
183
184 // ExpressionStatement restricted productions
185 "({});",
186 "(function(){});",
187 "(let[a] = b);",
188 "(function*(){});",
189 "(class{});",
190
191 // special cases
192 "(0).a",
193 "(function(){ }())",
194 "({a: function(){}}.a());",
195 "({a:0}.a ? b : c)",
196
197 // RegExp literal is allowed to have parens (#1589)
198 "var isA = (/^a$/).test('a');",
199 "var regex = (/^a$/);",
200 "function a(){ return (/^a$/); }",
201 "function a(){ return (/^a$/).test('a'); }",
202 "var isA = ((/^a$/)).test('a');",
203
204 // IIFE is allowed to have parens in any position (#655)
205 "var foo = (function() { return bar(); }())",
206 "var o = { foo: (function() { return bar(); }()) };",
207 "o.foo = (function(){ return bar(); }());",
208 "(function(){ return bar(); }()), (function(){ return bar(); }())",
209
210 // IIFE is allowed to have outer parens (#1004)
211 "var foo = (function() { return bar(); })()",
212 "var o = { foo: (function() { return bar(); })() };",
213 "o.foo = (function(){ return bar(); })();",
214 "(function(){ return bar(); })(), (function(){ return bar(); })()",
215 "function foo() { return (function(){}()); }",
216
217 // parens are required around yield
218 "var foo = (function*() { if ((yield foo()) + 1) { return; } }())",
219
220 // arrow functions have the precedence of an assignment expression
221 "(() => 0)()",
222 "(_ => 0)()",
223 "_ => 0, _ => 1",
224 "a = () => b = 0",
225 "0 ? _ => 0 : _ => 0",
226 "(_ => 0) || (_ => 0)",
227
228 // Object literals as arrow function bodies need parentheses
229 "x => ({foo: 1})",
230
231
232 // Exponentiation operator `**`
233 "1 + 2 ** 3",
234 "1 - 2 ** 3",
235 "2 ** -3",
236 "(-2) ** 3",
237 "(+2) ** 3",
238 "+ (2 ** 3)",
239
240 // https://github.com/eslint/eslint/issues/5789
241 "a => ({b: c}[d])",
242 "a => ({b: c}.d())",
243 "a => ({b: c}.d.e)",
244
245 // "functions" enables reports for function nodes only
246 { code: "(0)", options: ["functions"] },
247 { code: "((0))", options: ["functions"] },
248 { code: "a + (b * c)", options: ["functions"] },
249 { code: "a + ((b * c))", options: ["functions"] },
250 { code: "(a)(b)", options: ["functions"] },
251 { code: "((a))(b)", options: ["functions"] },
252 { code: "a, (b = c)", options: ["functions"] },
253 { code: "a, ((b = c))", options: ["functions"] },
254 { code: "for(a in (0));", options: ["functions"] },
255 { code: "for(a in ((0)));", options: ["functions"] },
256 { code: "var a = (b = c)", options: ["functions"] },
257 { code: "var a = ((b = c))", options: ["functions"] },
258 { code: "_ => (a = 0)", options: ["functions"] },
259 { code: "_ => ((a = 0))", options: ["functions"] },
260
261 // ["all", { conditionalAssign: false }] enables extra parens around conditional assignments
262 { code: "while ((foo = bar())) {}", options: ["all", { conditionalAssign: false }] },
263 { code: "if ((foo = bar())) {}", options: ["all", { conditionalAssign: false }] },
264 { code: "do; while ((foo = bar()))", options: ["all", { conditionalAssign: false }] },
265 { code: "for (;(a = b););", options: ["all", { conditionalAssign: false }] },
266 { code: "var a = ((b = c)) ? foo : bar;", options: ["all", { conditionalAssign: false }] },
267 { code: "while (((foo = bar()))) {}", options: ["all", { conditionalAssign: false }] },
268 { code: "var a = (((b = c))) ? foo : bar;", options: ["all", { conditionalAssign: false }] },
269
270 // ["all", { nestedBinaryExpressions: false }] enables extra parens around conditional assignments
271 { code: "a + (b * c)", options: ["all", { nestedBinaryExpressions: false }] },
272 { code: "(a * b) + c", options: ["all", { nestedBinaryExpressions: false }] },
273 { code: "(a * b) / c", options: ["all", { nestedBinaryExpressions: false }] },
274 { code: "a || (b && c)", options: ["all", { nestedBinaryExpressions: false }] },
275 { code: "a + ((b * c))", options: ["all", { nestedBinaryExpressions: false }] },
276 { code: "((a * b)) + c", options: ["all", { nestedBinaryExpressions: false }] },
277 { code: "((a * b)) / c", options: ["all", { nestedBinaryExpressions: false }] },
278 { code: "a || ((b && c))", options: ["all", { nestedBinaryExpressions: false }] },
279
280 // ["all", { returnAssign: false }] enables extra parens around expressions returned by return statements
281 { code: "function a(b) { return b || c; }", options: ["all", { returnAssign: false }] },
282 { code: "function a(b) { return; }", options: ["all", { returnAssign: false }] },
283 { code: "function a(b) { return (b = 1); }", options: ["all", { returnAssign: false }] },
284 { code: "function a(b) { return (b = c) || (b = d); }", options: ["all", { returnAssign: false }] },
285 { code: "function a(b) { return c ? (d = b) : (e = b); }", options: ["all", { returnAssign: false }] },
286 { code: "b => b || c;", options: ["all", { returnAssign: false }] },
287 { code: "b => (b = 1);", options: ["all", { returnAssign: false }] },
288 { code: "b => (b = c) || (b = d);", options: ["all", { returnAssign: false }] },
289 { code: "b => c ? (d = b) : (e = b);", options: ["all", { returnAssign: false }] },
290 { code: "b => { return b || c };", options: ["all", { returnAssign: false }] },
291 { code: "b => { return (b = 1) };", options: ["all", { returnAssign: false }] },
292 { code: "b => { return (b = c) || (b = d) };", options: ["all", { returnAssign: false }] },
293 { code: "b => { return c ? (d = b) : (e = b) };", options: ["all", { returnAssign: false }] },
294 { code: "function a(b) { return ((b = 1)); }", options: ["all", { returnAssign: false }] },
295 { code: "b => ((b = 1));", options: ["all", { returnAssign: false }] },
296
297 // https://github.com/eslint/eslint/issues/3653
298 "(function(){}).foo(), 1, 2;",
299 "(function(){}).foo++;",
300 "(function(){}).foo() || bar;",
301 "(function(){}).foo() + 1;",
302 "(function(){}).foo() ? bar : baz;",
303 "(function(){}).foo.bar();",
304 "(function(){}.foo());",
305 "(function(){}.foo.bar);",
306
307 "(class{}).foo(), 1, 2;",
308 "(class{}).foo++;",
309 "(class{}).foo() || bar;",
310 "(class{}).foo() + 1;",
311 "(class{}).foo() ? bar : baz;",
312 "(class{}).foo.bar();",
313 "(class{}.foo());",
314 "(class{}.foo.bar);",
315
316 // https://github.com/eslint/eslint/issues/4608
317 "function *a() { yield b; }",
318 "function *a() { yield yield; }",
319 "function *a() { yield b, c; }",
320 "function *a() { yield (b, c); }",
321 "function *a() { yield b + c; }",
322 "function *a() { (yield b) + c; }",
323
324 // https://github.com/eslint/eslint/issues/4229
325 [
326 "function a() {",
327 " return (",
328 " b",
329 " );",
330 "}"
331 ].join("\n"),
332 [
333 "function a() {",
334 " return (",
335 " <JSX />",
336 " );",
337 "}"
338 ].join("\n"),
339 [
340 "function a() {",
341 " return (",
342 " <></>",
343 " );",
344 "}"
345 ].join("\n"),
346 [
347 "throw (",
348 " a",
349 ");"
350 ].join("\n"),
351 [
352 "function *a() {",
353 " yield (",
354 " b",
355 " );",
356 "}"
357 ].join("\n"),
358
359 // async/await
360 "async function a() { await (a + b) }",
361 "async function a() { await (a + await b) }",
362 "async function a() { (await a)() }",
363 "async function a() { new (await a) }",
364 { code: "(foo instanceof bar) instanceof baz", options: ["all", { nestedBinaryExpressions: false }] },
365 { code: "(foo in bar) in baz", options: ["all", { nestedBinaryExpressions: false }] },
366 { code: "(foo + bar) + baz", options: ["all", { nestedBinaryExpressions: false }] },
367 { code: "(foo && bar) && baz", options: ["all", { nestedBinaryExpressions: false }] },
368 { code: "foo instanceof (bar instanceof baz)", options: ["all", { nestedBinaryExpressions: false }] },
369 { code: "foo in (bar in baz)", options: ["all", { nestedBinaryExpressions: false }] },
370 { code: "foo + (bar + baz)", options: ["all", { nestedBinaryExpressions: false }] },
371 { code: "foo && (bar && baz)", options: ["all", { nestedBinaryExpressions: false }] },
372 { code: "((foo instanceof bar)) instanceof baz", options: ["all", { nestedBinaryExpressions: false }] },
373 { code: "((foo in bar)) in baz", options: ["all", { nestedBinaryExpressions: false }] },
374
375 // https://github.com/eslint/eslint/issues/9019
376 "(async function() {});",
377 "(async function () { }());",
378
379 // ["all", { ignoreJSX: "all" }]
380 { code: "const Component = (<div />)", options: ["all", { ignoreJSX: "all" }] },
381 { code: "const Component = ((<div />))", options: ["all", { ignoreJSX: "all" }] },
382 {
383 code: [
384 "const Component = (<>",
385 " <p />",
386 "</>);"
387 ].join("\n"),
388 options: ["all", { ignoreJSX: "all" }]
389 },
390 {
391 code: [
392 "const Component = ((<>",
393 " <p />",
394 "</>));"
395 ].join("\n"),
396 options: ["all", { ignoreJSX: "all" }]
397 },
398 {
399 code: [
400 "const Component = (<div>",
401 " <p />",
402 "</div>);"
403 ].join("\n"),
404 options: ["all", { ignoreJSX: "all" }]
405 },
406 {
407 code: [
408 "const Component = (",
409 " <div />",
410 ");"
411 ].join("\n"),
412 options: ["all", { ignoreJSX: "all" }]
413 },
414 {
415 code: [
416 "const Component =",
417 " (<div />)"
418 ].join("\n"),
419 options: ["all", { ignoreJSX: "all" }]
420 },
421
422 // ["all", { ignoreJSX: "single-line" }]
423 { code: "const Component = (<div />);", options: ["all", { ignoreJSX: "single-line" }] },
424 { code: "const Component = ((<div />));", options: ["all", { ignoreJSX: "single-line" }] },
425 {
426 code: [
427 "const Component = (",
428 " <div />",
429 ");"
430 ].join("\n"),
431 options: ["all", { ignoreJSX: "single-line" }]
432 },
433 {
434 code: [
435 "const Component =",
436 "(<div />)"
437 ].join("\n"),
438 options: ["all", { ignoreJSX: "single-line" }]
439 },
440
441 // ["all", { ignoreJSX: "multi-line" }]
442 {
443 code: [
444 "const Component = (",
445 "<div>",
446 " <p />",
447 "</div>",
448 ");"
449 ].join("\n"),
450 options: ["all", { ignoreJSX: "multi-line" }]
451 },
452 {
453 code: [
454 "const Component = ((",
455 "<div>",
456 " <p />",
457 "</div>",
458 "));"
459 ].join("\n"),
460 options: ["all", { ignoreJSX: "multi-line" }]
461 },
462 {
463 code: [
464 "const Component = (<div>",
465 " <p />",
466 "</div>);"
467 ].join("\n"),
468 options: ["all", { ignoreJSX: "multi-line" }]
469 },
470 {
471 code: [
472 "const Component =",
473 "(<div>",
474 " <p />",
475 "</div>);"
476 ].join("\n"),
477 options: ["all", { ignoreJSX: "multi-line" }]
478 },
479 {
480 code: [
481 "const Component = (<div",
482 " prop={true}",
483 "/>)"
484 ].join("\n"),
485 options: ["all", { ignoreJSX: "multi-line" }]
486 },
487
488 // ["all", { enforceForArrowConditionals: false }]
489 { code: "var a = b => 1 ? 2 : 3", options: ["all", { enforceForArrowConditionals: false }] },
490 { code: "var a = (b) => (1 ? 2 : 3)", options: ["all", { enforceForArrowConditionals: false }] },
491 { code: "var a = (b) => ((1 ? 2 : 3))", options: ["all", { enforceForArrowConditionals: false }] },
492
493 // ["all", { enforceForSequenceExpressions: false }]
494 { code: "(a, b)", options: ["all", { enforceForSequenceExpressions: false }] },
495 { code: "((a, b))", options: ["all", { enforceForSequenceExpressions: false }] },
496 { code: "(foo(), bar());", options: ["all", { enforceForSequenceExpressions: false }] },
497 { code: "((foo(), bar()));", options: ["all", { enforceForSequenceExpressions: false }] },
498 { code: "if((a, b)){}", options: ["all", { enforceForSequenceExpressions: false }] },
499 { code: "if(((a, b))){}", options: ["all", { enforceForSequenceExpressions: false }] },
500 { code: "while ((val = foo(), val < 10));", options: ["all", { enforceForSequenceExpressions: false }] },
501
502 // ["all", { enforceForNewInMemberExpressions: false }]
503 { code: "(new foo()).bar", options: ["all", { enforceForNewInMemberExpressions: false }] },
504 { code: "(new foo())[bar]", options: ["all", { enforceForNewInMemberExpressions: false }] },
505 { code: "(new foo()).bar()", options: ["all", { enforceForNewInMemberExpressions: false }] },
506 { code: "(new foo(bar)).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
507 { code: "(new foo.bar()).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
508 { code: "(new foo.bar()).baz()", options: ["all", { enforceForNewInMemberExpressions: false }] },
509 { code: "((new foo.bar())).baz()", options: ["all", { enforceForNewInMemberExpressions: false }] },
510
511 "let a = [ ...b ]",
512 "let a = { ...b }",
513 {
514 code: "let a = { ...b }",
515 parserOptions: { ecmaVersion: 2018 }
516 },
517 "let a = [ ...(b, c) ]",
518 "let a = { ...(b, c) }",
519 {
520 code: "let a = { ...(b, c) }",
521 parserOptions: { ecmaVersion: 2018 }
522 },
523 "var [x = (1, foo)] = bar",
524 "class A extends B {}",
525 "const A = class extends B {}",
526 "class A extends (B=C) {}",
527 "const A = class extends (B=C) {}",
528 "class A extends (++foo) {}",
529 "() => ({ foo: 1 })",
530 "() => ({ foo: 1 }).foo",
531 "() => ({ foo: 1 }.foo().bar).baz.qux()",
532 "() => ({ foo: 1 }.foo().bar + baz)",
533 {
534 code: "export default (a, b)",
535 parserOptions: { sourceType: "module" }
536 },
537 {
538 code: "export default (function(){}).foo",
539 parserOptions: { sourceType: "module" }
540 },
541 {
542 code: "export default (class{}).foo",
543 parserOptions: { sourceType: "module" }
544 },
545 "({}).hasOwnProperty.call(foo, bar)",
546 "({}) ? foo() : bar()",
547 "({}) + foo",
548 "(function(){}) + foo",
549 "(let)\nfoo",
550 "(let[foo]) = 1", // setting the 'foo' property of the 'let' variable to 1
551 {
552 code: "((function(){}).foo.bar)();",
553 options: ["functions"]
554 },
555 {
556 code: "((function(){}).foo)();",
557 options: ["functions"]
558 },
559 "(let)[foo]",
560 "for ((let) in foo);",
561 "for ((let[foo]) in bar);",
562 "for ((let)[foo] in bar);",
563 "for ((let[foo].bar) in baz);",
564
565 // https://github.com/eslint/eslint/issues/11706 (also in invalid[])
566 "for (let a = (b in c); ;);",
567 "for (let a = (b && c in d); ;);",
568 "for (let a = (b in c && d); ;);",
569 "for (let a = (b => b in c); ;);",
570 "for (let a = b => (b in c); ;);",
571 "for (let a = (b in c in d); ;);",
572 "for (let a = (b in c), d = (e in f); ;);",
573 "for (let a = (b => c => b in c); ;);",
574 "for (let a = (b && c && d in e); ;);",
575 "for (let a = b && (c in d); ;);",
576 "for (let a = (b in c) && (d in e); ;);",
577 "for ((a in b); ;);",
578 "for (a = (b in c); ;);",
579 "for ((a in b && c in d && e in f); ;);",
580 "for (let a = [] && (b in c); ;);",
581 "for (let a = (b in [c]); ;);",
582 "for (let a = b => (c in d); ;);",
583 "for (let a = (b in c) ? d : e; ;);",
584 "for (let a = (b in c ? d : e); ;);",
585 "for (let a = b ? c : (d in e); ;);",
586 "for (let a = (b in c), d = () => { for ((e in f);;); for ((g in h);;); }; ;); for((i in j); ;);",
587
588 // https://github.com/eslint/eslint/issues/11706 regression tests (also in invalid[])
589 "for (let a = b; a; a); a; a;",
590 "for (a; a; a); a; a;",
591 "for (; a; a); a; a;",
592 "for (let a = (b && c) === d; ;);"
593 ],
594
595 invalid: [
596 invalid("(0)", "0", "Literal"),
597 invalid("( 0 )", " 0 ", "Literal"),
598 invalid("if((0));", "if(0);", "Literal"),
599 invalid("if(( 0 ));", "if( 0 );", "Literal"),
600 invalid("with((0)){}", "with(0){}", "Literal"),
601 invalid("switch((0)){}", "switch(0){}", "Literal"),
602 invalid("switch(0){ case (1): break; }", "switch(0){ case 1: break; }", "Literal"),
603 invalid("for((0);;);", "for(0;;);", "Literal"),
604 invalid("for(;(0););", "for(;0;);", "Literal"),
605 invalid("for(;;(0));", "for(;;0);", "Literal"),
606 invalid("throw(0)", "throw 0", "Literal"),
607 invalid("while((0));", "while(0);", "Literal"),
608 invalid("do; while((0))", "do; while(0)", "Literal"),
609 invalid("for(a in (0));", "for(a in 0);", "Literal"),
610 invalid("for(a of (0));", "for(a of 0);", "Literal", 1),
611 invalid("const foo = {[(a)]:1}", "const foo = {[a]:1}", "Identifier", 1),
612 invalid("const foo = {[(a=b)]:1}", "const foo = {[a=b]:1}", "AssignmentExpression", 1),
613 invalid("const foo = {*[(Symbol.iterator)]() {}}", "const foo = {*[Symbol.iterator]() {}}", "MemberExpression", 1),
614 invalid("const foo = { get [(a)]() {}}", "const foo = { get [a]() {}}", "Identifier", 1),
615 invalid("const foo = {[(a+b)]:c, d}", "const foo = {[a+b]:c, d}", "BinaryExpression", 1),
616 invalid("const foo = {a, [(b+c)]:d, e}", "const foo = {a, [b+c]:d, e}", "BinaryExpression", 1),
617 invalid("const foo = {[(a+b)]:c, d:e}", "const foo = {[a+b]:c, d:e}", "BinaryExpression", 1),
618 invalid("const foo = {a:b, [(c+d)]:e, f:g}", "const foo = {a:b, [c+d]:e, f:g}", "BinaryExpression", 1),
619 invalid("const foo = {[(a+b)]:c, [d]:e}", "const foo = {[a+b]:c, [d]:e}", "BinaryExpression", 1),
620 invalid("const foo = {[a]:b, [(c+d)]:e, [f]:g}", "const foo = {[a]:b, [c+d]:e, [f]:g}", "BinaryExpression", 1),
621 invalid("const foo = {[(a+b)]:c, [(d,e)]:f}", "const foo = {[a+b]:c, [(d,e)]:f}", "BinaryExpression", 1),
622 invalid("const foo = {[(a,b)]:c, [(d+e)]:f, [(g,h)]:e}", "const foo = {[(a,b)]:c, [d+e]:f, [(g,h)]:e}", "BinaryExpression", 1),
623 invalid("const foo = {a, b:c, [(d+e)]:f, [(g,h)]:i, [j]:k}", "const foo = {a, b:c, [d+e]:f, [(g,h)]:i, [j]:k}", "BinaryExpression", 1),
624 invalid("const foo = {[a+(b*c)]:d}", "const foo = {[a+b*c]:d}", "BinaryExpression", 1),
625 invalid("const foo = {[(a, (b+c))]:d}", "const foo = {[(a, b+c)]:d}", "BinaryExpression", 1),
626 invalid("const {[(a)]:b} = {}", "const {[a]:b} = {}", "Identifier", 1),
627 invalid("const {[(a=b)]:c=1} = {}", "const {[a=b]:c=1} = {}", "AssignmentExpression", 1),
628 invalid("const {[(a+b)]:c, d} = {}", "const {[a+b]:c, d} = {}", "BinaryExpression", 1),
629 invalid("const {a, [(b+c)]:d, e} = {}", "const {a, [b+c]:d, e} = {}", "BinaryExpression", 1),
630 invalid("const {[(a+b)]:c, d:e} = {}", "const {[a+b]:c, d:e} = {}", "BinaryExpression", 1),
631 invalid("const {a:b, [(c+d)]:e, f:g} = {}", "const {a:b, [c+d]:e, f:g} = {}", "BinaryExpression", 1),
632 invalid("const {[(a+b)]:c, [d]:e} = {}", "const {[a+b]:c, [d]:e} = {}", "BinaryExpression", 1),
633 invalid("const {[a]:b, [(c+d)]:e, [f]:g} = {}", "const {[a]:b, [c+d]:e, [f]:g} = {}", "BinaryExpression", 1),
634 invalid("const {[(a+b)]:c, [(d,e)]:f} = {}", "const {[a+b]:c, [(d,e)]:f} = {}", "BinaryExpression", 1),
635 invalid("const {[(a,b)]:c, [(d+e)]:f, [(g,h)]:e} = {}", "const {[(a,b)]:c, [d+e]:f, [(g,h)]:e} = {}", "BinaryExpression", 1),
636 invalid("const {a, b:c, [(d+e)]:f, [(g,h)]:i, [j]:k} = {}", "const {a, b:c, [d+e]:f, [(g,h)]:i, [j]:k} = {}", "BinaryExpression", 1),
637 invalid("const {[a+(b*c)]:d} = {}", "const {[a+b*c]:d} = {}", "BinaryExpression", 1),
638 invalid("const {[(a, (b+c))]:d} = {}", "const {[(a, b+c)]:d} = {}", "BinaryExpression", 1),
639 invalid("class foo { [(a)](){} }", "class foo { [a](){} }", "Identifier"),
640 invalid("class foo {*[(Symbol.iterator)]() {}}", "class foo {*[Symbol.iterator]() {}}", "MemberExpression"),
641 invalid("class foo { get [(a)](){} }", "class foo { get [a](){} }", "Identifier"),
642 invalid("class foo { set [(a)](bar){} }", "class foo { set [a](bar){} }", "Identifier"),
643 invalid("class foo { static [(a)](bar){} }", "class foo { static [a](bar){} }", "Identifier"),
644 invalid("class foo { [(a=b)](){} }", "class foo { [a=b](){} }", "AssignmentExpression"),
645 invalid("class foo { constructor (){} [(a+b)](){} }", "class foo { constructor (){} [a+b](){} }", "BinaryExpression"),
646 invalid("class foo { [(a+b)](){} constructor (){} }", "class foo { [a+b](){} constructor (){} }", "BinaryExpression"),
647 invalid("class foo { [(a+b)](){} c(){} }", "class foo { [a+b](){} c(){} }", "BinaryExpression"),
648 invalid("class foo { a(){} [(b+c)](){} d(){} }", "class foo { a(){} [b+c](){} d(){} }", "BinaryExpression"),
649 invalid("class foo { [(a+b)](){} [c](){} }", "class foo { [a+b](){} [c](){} }", "BinaryExpression"),
650 invalid("class foo { [a](){} [(b+c)](){} [d](){} }", "class foo { [a](){} [b+c](){} [d](){} }", "BinaryExpression"),
651 invalid("class foo { [(a+b)](){} [(c,d)](){} }", "class foo { [a+b](){} [(c,d)](){} }", "BinaryExpression"),
652 invalid("class foo { [(a,b)](){} [(c+d)](){} }", "class foo { [(a,b)](){} [c+d](){} }", "BinaryExpression"),
653 invalid("class foo { [a+(b*c)](){} }", "class foo { [a+b*c](){} }", "BinaryExpression"),
654 invalid("const foo = class { [(a)](){} }", "const foo = class { [a](){} }", "Identifier"),
655 invalid(
656 "var foo = (function*() { if ((yield foo())) { return; } }())",
657 "var foo = (function*() { if (yield foo()) { return; } }())",
658 "YieldExpression",
659 1
660 ),
661 invalid("f((0))", "f(0)", "Literal"),
662 invalid("f(0, (1))", "f(0, 1)", "Literal"),
663 invalid("!(0)", "!0", "Literal"),
664 invalid("a[(1)]", "a[1]", "Literal"),
665 invalid("(a)(b)", "a(b)", "Identifier"),
666 invalid("(async)", "async", "Identifier"),
667 invalid("(a, b)", "a, b", "SequenceExpression"),
668 invalid("var a = (b = c);", "var a = b = c;", "AssignmentExpression"),
669 invalid("function f(){ return (a); }", "function f(){ return a; }", "Identifier"),
670 invalid("[a, (b = c)]", "[a, b = c]", "AssignmentExpression"),
671 invalid("!{a: (b = c)}", "!{a: b = c}", "AssignmentExpression"),
672 invalid("typeof(0)", "typeof 0", "Literal"),
673 invalid("typeof (0)", "typeof 0", "Literal"),
674 invalid("typeof([])", "typeof[]", "ArrayExpression"),
675 invalid("typeof ([])", "typeof []", "ArrayExpression"),
676 invalid("typeof( 0)", "typeof 0", "Literal"),
677 invalid("typeof(typeof 5)", "typeof typeof 5", "UnaryExpression"),
678 invalid("typeof (typeof 5)", "typeof typeof 5", "UnaryExpression"),
679 invalid("+(+foo)", "+ +foo", "UnaryExpression"),
680 invalid("-(-foo)", "- -foo", "UnaryExpression"),
681 invalid("+(-foo)", "+-foo", "UnaryExpression"),
682 invalid("-(+foo)", "-+foo", "UnaryExpression"),
683 invalid("-((bar+foo))", "-(bar+foo)", "BinaryExpression"),
684 invalid("+((bar-foo))", "+(bar-foo)", "BinaryExpression"),
685 invalid("++(foo)", "++foo", "Identifier"),
686 invalid("--(foo)", "--foo", "Identifier"),
687 invalid("(a || b) ? c : d", "a || b ? c : d", "LogicalExpression"),
688 invalid("a ? (b = c) : d", "a ? b = c : d", "AssignmentExpression"),
689 invalid("a ? b : (c = d)", "a ? b : c = d", "AssignmentExpression"),
690 invalid("(c = d) ? (b) : c", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
691 invalid("(c = d) ? b : (c)", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
692 invalid("f((a = b))", "f(a = b)", "AssignmentExpression"),
693 invalid("a, (b = c)", "a, b = c", "AssignmentExpression"),
694 invalid("a = (b * c)", "a = b * c", "BinaryExpression"),
695 invalid("a + (b * c)", "a + b * c", "BinaryExpression"),
696 invalid("(a * b) + c", "a * b + c", "BinaryExpression"),
697 invalid("(a * b) / c", "a * b / c", "BinaryExpression"),
698 invalid("(2) ** 3 ** 4", "2 ** 3 ** 4", "Literal", null),
699 invalid("2 ** (3 ** 4)", "2 ** 3 ** 4", "BinaryExpression", null),
700 invalid("(2 ** 3)", "2 ** 3", "BinaryExpression", null),
701 invalid("(2 ** 3) + 1", "2 ** 3 + 1", "BinaryExpression", null),
702 invalid("1 - (2 ** 3)", "1 - 2 ** 3", "BinaryExpression", null),
703 invalid("-((2 ** 3))", "-(2 ** 3)", "BinaryExpression", null),
704 invalid("typeof ((a ** b));", "typeof (a ** b);", "BinaryExpression", null),
705 invalid("((-2)) ** 3", "(-2) ** 3", "UnaryExpression", null),
706
707 invalid("a = (b * c)", "a = b * c", "BinaryExpression", null, { options: ["all", { nestedBinaryExpressions: false }] }),
708 invalid("(b * c)", "b * c", "BinaryExpression", null, { options: ["all", { nestedBinaryExpressions: false }] }),
709
710 invalid("a = (b = c)", "a = b = c", "AssignmentExpression"),
711 invalid("(a).b", "a.b", "Identifier"),
712 invalid("(0)[a]", "0[a]", "Literal"),
713 invalid("(0.0).a", "0.0.a", "Literal"),
714 invalid("(0xBEEF).a", "0xBEEF.a", "Literal"),
715 invalid("(1e6).a", "1e6.a", "Literal"),
716 invalid("(0123).a", "0123.a", "Literal"),
717 invalid("a[(function() {})]", "a[function() {}]", "FunctionExpression"),
718 invalid("new (function(){})", "new function(){}", "FunctionExpression"),
719 invalid("new (\nfunction(){}\n)", "new \nfunction(){}\n", "FunctionExpression", 1),
720 invalid("((function foo() {return 1;}))()", "(function foo() {return 1;})()", "FunctionExpression"),
721 invalid("((function(){ return bar(); })())", "(function(){ return bar(); })()", "CallExpression"),
722 invalid("(foo()).bar", "foo().bar", "CallExpression"),
723 invalid("(foo.bar()).baz", "foo.bar().baz", "CallExpression"),
724 invalid("(foo\n.bar())\n.baz", "foo\n.bar()\n.baz", "CallExpression"),
725 invalid("(new foo()).bar", "new foo().bar", "NewExpression"),
726 invalid("(new foo())[bar]", "new foo()[bar]", "NewExpression"),
727 invalid("(new foo()).bar()", "new foo().bar()", "NewExpression"),
728 invalid("(new foo(bar)).baz", "new foo(bar).baz", "NewExpression"),
729 invalid("(new foo.bar()).baz", "new foo.bar().baz", "NewExpression"),
730 invalid("(new foo.bar()).baz()", "new foo.bar().baz()", "NewExpression"),
731
732 invalid("(a)()", "a()", "Identifier"),
733 invalid("(a.b)()", "a.b()", "MemberExpression"),
734 invalid("(a())()", "a()()", "CallExpression"),
735 invalid("(a.b())()", "a.b()()", "CallExpression"),
736 invalid("(a().b)()", "a().b()", "MemberExpression"),
737 invalid("(a().b.c)()", "a().b.c()", "MemberExpression"),
738 invalid("new (A)", "new A", "Identifier"),
739 invalid("(new A())()", "new A()()", "NewExpression"),
740 invalid("(new A(1))()", "new A(1)()", "NewExpression"),
741 invalid("((new A))()", "(new A)()", "NewExpression"),
742 invalid("new (foo\n.baz\n.bar\n.foo.baz)", "new foo\n.baz\n.bar\n.foo.baz", "MemberExpression"),
743 invalid("new (foo.baz.bar.baz)", "new foo.baz.bar.baz", "MemberExpression"),
744
745 invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1),
746 invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1),
747 invalid("a = (_ => 0)", "a = _ => 0", "ArrowFunctionExpression", 1),
748 invalid("_ => (a = 0)", "_ => a = 0", "AssignmentExpression", 1),
749 invalid("x => (({}))", "x => ({})", "ObjectExpression", 1),
750
751 invalid("new (function(){})", "new function(){}", "FunctionExpression", null, { options: ["functions"] }),
752 invalid("new (\nfunction(){}\n)", "new \nfunction(){}\n", "FunctionExpression", 1, { options: ["functions"] }),
753 invalid("((function foo() {return 1;}))()", "(function foo() {return 1;})()", "FunctionExpression", null, { options: ["functions"] }),
754 invalid("a[(function() {})]", "a[function() {}]", "FunctionExpression", null, { options: ["functions"] }),
755 invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
756 invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
757 invalid("a = (_ => 0)", "a = _ => 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
758
759
760 invalid("while ((foo = bar())) {}", "while (foo = bar()) {}", "AssignmentExpression"),
761 invalid("while ((foo = bar())) {}", "while (foo = bar()) {}", "AssignmentExpression", 1, { options: ["all", { conditionalAssign: true }] }),
762 invalid("if ((foo = bar())) {}", "if (foo = bar()) {}", "AssignmentExpression"),
763 invalid("do; while ((foo = bar()))", "do; while (foo = bar())", "AssignmentExpression"),
764 invalid("for (;(a = b););", "for (;a = b;);", "AssignmentExpression"),
765
766 // https://github.com/eslint/eslint/issues/3653
767 invalid("((function(){})).foo();", "(function(){}).foo();", "FunctionExpression"),
768 invalid("((function(){}).foo());", "(function(){}).foo();", "CallExpression"),
769 invalid("((function(){}).foo);", "(function(){}).foo;", "MemberExpression"),
770 invalid("0, (function(){}).foo();", "0, function(){}.foo();", "FunctionExpression"),
771 invalid("void (function(){}).foo();", "void function(){}.foo();", "FunctionExpression"),
772 invalid("++(function(){}).foo;", "++function(){}.foo;", "FunctionExpression"),
773 invalid("bar || (function(){}).foo();", "bar || function(){}.foo();", "FunctionExpression"),
774 invalid("1 + (function(){}).foo();", "1 + function(){}.foo();", "FunctionExpression"),
775 invalid("bar ? (function(){}).foo() : baz;", "bar ? function(){}.foo() : baz;", "FunctionExpression"),
776 invalid("bar ? baz : (function(){}).foo();", "bar ? baz : function(){}.foo();", "FunctionExpression"),
777 invalid("bar((function(){}).foo(), 0);", "bar(function(){}.foo(), 0);", "FunctionExpression"),
778 invalid("bar[(function(){}).foo()];", "bar[function(){}.foo()];", "FunctionExpression"),
779 invalid("var bar = (function(){}).foo();", "var bar = function(){}.foo();", "FunctionExpression"),
780
781 invalid("((class{})).foo();", "(class{}).foo();", "ClassExpression", null),
782 invalid("((class{}).foo());", "(class{}).foo();", "CallExpression", null),
783 invalid("((class{}).foo);", "(class{}).foo;", "MemberExpression", null),
784 invalid("0, (class{}).foo();", "0, class{}.foo();", "ClassExpression", null),
785 invalid("void (class{}).foo();", "void class{}.foo();", "ClassExpression", null),
786 invalid("++(class{}).foo;", "++class{}.foo;", "ClassExpression", null),
787 invalid("bar || (class{}).foo();", "bar || class{}.foo();", "ClassExpression", null),
788 invalid("1 + (class{}).foo();", "1 + class{}.foo();", "ClassExpression", null),
789 invalid("bar ? (class{}).foo() : baz;", "bar ? class{}.foo() : baz;", "ClassExpression", null),
790 invalid("bar ? baz : (class{}).foo();", "bar ? baz : class{}.foo();", "ClassExpression", null),
791 invalid("bar((class{}).foo(), 0);", "bar(class{}.foo(), 0);", "ClassExpression", null),
792 invalid("bar[(class{}).foo()];", "bar[class{}.foo()];", "ClassExpression", null),
793 invalid("var bar = (class{}).foo();", "var bar = class{}.foo();", "ClassExpression", null),
794 invalid("var foo = ((bar, baz));", "var foo = (bar, baz);", "SequenceExpression", null),
795
796 // https://github.com/eslint/eslint/issues/4608
797 invalid("function *a() { yield (b); }", "function *a() { yield b; }", "Identifier", null),
798 invalid("function *a() { (yield b), c; }", "function *a() { yield b, c; }", "YieldExpression", null),
799 invalid("function *a() { yield ((b, c)); }", "function *a() { yield (b, c); }", "SequenceExpression", null),
800 invalid("function *a() { yield (b + c); }", "function *a() { yield b + c; }", "BinaryExpression", null),
801
802 // https://github.com/eslint/eslint/issues/4229
803 invalid([
804 "function a() {",
805 " return (b);",
806 "}"
807 ].join("\n"), [
808 "function a() {",
809 " return b;",
810 "}"
811 ].join("\n"), "Identifier"),
812 invalid([
813 "function a() {",
814 " return",
815 " (b);",
816 "}"
817 ].join("\n"), [
818 "function a() {",
819 " return",
820 " b;",
821 "}"
822 ].join("\n"), "Identifier"),
823 invalid([
824 "function a() {",
825 " return ((",
826 " b",
827 " ));",
828 "}"
829 ].join("\n"), [
830 "function a() {",
831 " return (",
832 " b",
833 " );",
834 "}"
835 ].join("\n"), "Identifier"),
836 invalid([
837 "function a() {",
838 " return (<JSX />);",
839 "}"
840 ].join("\n"), [
841 "function a() {",
842 " return <JSX />;",
843 "}"
844 ].join("\n"), "JSXElement", null),
845 invalid([
846 "function a() {",
847 " return",
848 " (<JSX />);",
849 "}"
850 ].join("\n"), [
851 "function a() {",
852 " return",
853 " <JSX />;",
854 "}"
855 ].join("\n"), "JSXElement", null),
856 invalid([
857 "function a() {",
858 " return ((",
859 " <JSX />",
860 " ));",
861 "}"
862 ].join("\n"), [
863 "function a() {",
864 " return (",
865 " <JSX />",
866 " );",
867 "}"
868 ].join("\n"), "JSXElement", null),
869 invalid([
870 "function a() {",
871 " return ((",
872 " <></>",
873 " ));",
874 "}"
875 ].join("\n"), [
876 "function a() {",
877 " return (",
878 " <></>",
879 " );",
880 "}"
881 ].join("\n"), "JSXFragment", null),
882 invalid("throw (a);", "throw a;", "Identifier"),
883 invalid([
884 "throw ((",
885 " a",
886 "));"
887 ].join("\n"), [
888 "throw (",
889 " a",
890 ");"
891 ].join("\n"), "Identifier"),
892 invalid([
893 "function *a() {",
894 " yield (b);",
895 "}"
896 ].join("\n"), [
897 "function *a() {",
898 " yield b;",
899 "}"
900 ].join("\n"), "Identifier", null),
901 invalid([
902 "function *a() {",
903 " yield",
904 " (b);",
905 "}"
906 ].join("\n"), [
907 "function *a() {",
908 " yield",
909 " b;",
910 "}"
911 ].join("\n"), "Identifier", null),
912 invalid([
913 "function *a() {",
914 " yield ((",
915 " b",
916 " ));",
917 "}"
918 ].join("\n"), [
919 "function *a() {",
920 " yield (",
921 " b",
922 " );",
923 "}"
924 ].join("\n"), "Identifier", null),
925
926 // returnAssign option
927 {
928 code: "function a(b) { return (b || c); }",
929 output: "function a(b) { return b || c; }",
930 options: ["all", { returnAssign: false }],
931 errors: [
932 {
933 messageId: "unexpected",
934 type: "LogicalExpression"
935 }
936 ]
937 },
938 {
939 code: "function a(b) { return ((b = c) || (d = e)); }",
940 output: "function a(b) { return (b = c) || (d = e); }",
941 errors: [
942 {
943 messageId: "unexpected",
944 type: "LogicalExpression"
945 }
946 ]
947 },
948 {
949 code: "function a(b) { return (b = 1); }",
950 output: "function a(b) { return b = 1; }",
951 errors: [
952 {
953 messageId: "unexpected",
954 type: "AssignmentExpression"
955 }
956 ]
957 },
958 {
959 code: "function a(b) { return c ? (d = b) : (e = b); }",
960 output: "function a(b) { return c ? d = b : e = b; }",
961 errors: [
962 {
963 messageId: "unexpected",
964 type: "AssignmentExpression"
965 },
966 {
967 messageId: "unexpected",
968 type: "AssignmentExpression"
969 }
970 ]
971 },
972 {
973 code: "b => (b || c);",
974 output: "b => b || c;",
975 options: ["all", { returnAssign: false }],
976
977 errors: [
978 {
979 messageId: "unexpected",
980 type: "LogicalExpression"
981 }
982 ]
983 },
984 {
985 code: "b => ((b = c) || (d = e));",
986 output: "b => (b = c) || (d = e);",
987 errors: [
988 {
989 messageId: "unexpected",
990 type: "LogicalExpression"
991 }
992 ]
993 },
994 {
995 code: "b => (b = 1);",
996 output: "b => b = 1;",
997 errors: [
998 {
999 messageId: "unexpected",
1000 type: "AssignmentExpression"
1001 }
1002 ]
1003 },
1004 {
1005 code: "b => c ? (d = b) : (e = b);",
1006 output: "b => c ? d = b : e = b;",
1007 errors: [
1008 {
1009 messageId: "unexpected",
1010 type: "AssignmentExpression"
1011 },
1012 {
1013 messageId: "unexpected",
1014 type: "AssignmentExpression"
1015 }
1016 ]
1017 },
1018 {
1019 code: "b => { return (b || c); }",
1020 output: "b => { return b || c; }",
1021 options: ["all", { returnAssign: false }],
1022 errors: [
1023 {
1024 messageId: "unexpected",
1025 type: "LogicalExpression"
1026 }
1027 ]
1028 },
1029 {
1030 code: "b => { return ((b = c) || (d = e)) };",
1031 output: "b => { return (b = c) || (d = e) };",
1032 errors: [
1033 {
1034 messageId: "unexpected",
1035 type: "LogicalExpression"
1036 }
1037 ]
1038 },
1039 {
1040 code: "b => { return (b = 1) };",
1041 output: "b => { return b = 1 };",
1042 errors: [
1043 {
1044 messageId: "unexpected",
1045 type: "AssignmentExpression"
1046 }
1047 ]
1048 },
1049 {
1050 code: "b => { return c ? (d = b) : (e = b); }",
1051 output: "b => { return c ? d = b : e = b; }",
1052 errors: [
1053 {
1054 messageId: "unexpected",
1055 type: "AssignmentExpression"
1056 },
1057 {
1058 messageId: "unexpected",
1059 type: "AssignmentExpression"
1060 }
1061 ]
1062 },
1063
1064 // async/await
1065 {
1066 code: "async function a() { (await a) + (await b); }",
1067 output: "async function a() { await a + await b; }",
1068 errors: [
1069 {
1070 messageId: "unexpected",
1071 type: "AwaitExpression"
1072 },
1073 {
1074 messageId: "unexpected",
1075 type: "AwaitExpression"
1076 }
1077 ]
1078 },
1079 invalid("async function a() { await (a); }", "async function a() { await a; }", "Identifier", null),
1080 invalid("async function a() { await (a()); }", "async function a() { await a(); }", "CallExpression", null),
1081 invalid("async function a() { await (+a); }", "async function a() { await +a; }", "UnaryExpression", null),
1082 invalid("async function a() { +(await a); }", "async function a() { +await a; }", "AwaitExpression", null),
1083 invalid("async function a() { await ((a,b)); }", "async function a() { await (a,b); }", "SequenceExpression", null),
1084 invalid("(foo) instanceof bar", "foo instanceof bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1085 invalid("(foo) in bar", "foo in bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1086 invalid("(foo) + bar", "foo + bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1087 invalid("(foo) && bar", "foo && bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1088 invalid("foo instanceof (bar)", "foo instanceof bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1089 invalid("foo in (bar)", "foo in bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1090 invalid("foo + (bar)", "foo + bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1091 invalid("foo && (bar)", "foo && bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1092
1093 // ["all", { ignoreJSX: "multi-line" }]
1094 invalid("const Component = (<div />);", "const Component = <div />;", "JSXElement", 1, {
1095 options: ["all", { ignoreJSX: "multi-line" }]
1096 }),
1097 invalid([
1098 "const Component = (",
1099 " <div />",
1100 ");"
1101 ].join("\n"), "const Component = \n <div />\n;", "JSXElement", 1, {
1102 options: ["all", { ignoreJSX: "multi-line" }]
1103 }),
1104 invalid([
1105 "const Component = (",
1106 " <></>",
1107 ");"
1108 ].join("\n"), "const Component = \n <></>\n;", "JSXFragment", 1, {
1109 options: ["all", { ignoreJSX: "multi-line" }]
1110 }),
1111
1112 // ["all", { ignoreJSX: "single-line" }]
1113 invalid([
1114 "const Component = (",
1115 "<div>",
1116 " <p />",
1117 "</div>",
1118 ");"
1119 ].join("\n"), "const Component = \n<div>\n <p />\n</div>\n;", "JSXElement", 1, {
1120 options: ["all", { ignoreJSX: "single-line" }]
1121 }),
1122 invalid([
1123 "const Component = (<div>",
1124 " <p />",
1125 "</div>);"
1126 ].join("\n"), "const Component = <div>\n <p />\n</div>;", "JSXElement", 1, {
1127 options: ["all", { ignoreJSX: "single-line" }]
1128 }),
1129 invalid([
1130 "const Component = (<div",
1131 " prop={true}",
1132 "/>)"
1133 ].join("\n"), "const Component = <div\n prop={true}\n/>", "JSXElement", 1, {
1134 options: ["all", { ignoreJSX: "single-line" }]
1135 }),
1136
1137 // ["all", { ignoreJSX: "none" }] default, same as unspecified
1138 invalid("const Component = (<div />);", "const Component = <div />;", "JSXElement", 1, {
1139 options: ["all", { ignoreJSX: "none" }]
1140 }),
1141 invalid([
1142 "const Component = (<div>",
1143 "<p />",
1144 "</div>)"
1145 ].join("\n"), "const Component = <div>\n<p />\n</div>", "JSXElement", 1, {
1146 options: ["all", { ignoreJSX: "none" }]
1147 }),
1148
1149 // ["all", { enforceForArrowConditionals: true }]
1150 {
1151 code: "var a = (b) => (1 ? 2 : 3)",
1152 output: "var a = (b) => 1 ? 2 : 3",
1153 options: ["all", { enforceForArrowConditionals: true }],
1154 errors: [
1155 {
1156 messageId: "unexpected"
1157 }
1158 ]
1159 },
1160 {
1161 code: "var a = (b) => ((1 ? 2 : 3))",
1162 output: "var a = (b) => (1 ? 2 : 3)",
1163 options: ["all", { enforceForArrowConditionals: true }],
1164 errors: [
1165 {
1166 messageId: "unexpected"
1167 }
1168 ]
1169 },
1170
1171 // ["all", { enforceForSequenceExpressions: true }]
1172 {
1173 code: "(a, b)",
1174 output: "a, b",
1175 options: ["all"],
1176 errors: [
1177 {
1178 messageId: "unexpected",
1179 type: "SequenceExpression"
1180 }
1181 ]
1182 },
1183 {
1184 code: "(a, b)",
1185 output: "a, b",
1186 options: ["all", {}],
1187 errors: [
1188 {
1189 messageId: "unexpected",
1190 type: "SequenceExpression"
1191 }
1192 ]
1193 },
1194 {
1195 code: "(a, b)",
1196 output: "a, b",
1197 options: ["all", { enforceForSequenceExpressions: true }],
1198 errors: [
1199 {
1200 messageId: "unexpected",
1201 type: "SequenceExpression"
1202 }
1203 ]
1204 },
1205 {
1206 code: "(foo(), bar());",
1207 output: "foo(), bar();",
1208 options: ["all", { enforceForSequenceExpressions: true }],
1209 errors: [
1210 {
1211 messageId: "unexpected",
1212 type: "SequenceExpression"
1213 }
1214 ]
1215 },
1216 {
1217 code: "if((a, b)){}",
1218 output: "if(a, b){}",
1219 options: ["all", { enforceForSequenceExpressions: true }],
1220 errors: [
1221 {
1222 messageId: "unexpected",
1223 type: "SequenceExpression"
1224 }
1225 ]
1226 },
1227 {
1228 code: "while ((val = foo(), val < 10));",
1229 output: "while (val = foo(), val < 10);",
1230 options: ["all", { enforceForSequenceExpressions: true }],
1231 errors: [
1232 {
1233 messageId: "unexpected",
1234 type: "SequenceExpression"
1235 }
1236 ]
1237 },
1238
1239 // ["all", { enforceForNewInMemberExpressions: true }]
1240 {
1241 code: "(new foo()).bar",
1242 output: "new foo().bar",
1243 options: ["all"],
1244 errors: [
1245 {
1246 messageId: "unexpected",
1247 type: "NewExpression"
1248 }
1249 ]
1250 },
1251 {
1252 code: "(new foo()).bar",
1253 output: "new foo().bar",
1254 options: ["all", {}],
1255 errors: [
1256 {
1257 messageId: "unexpected",
1258 type: "NewExpression"
1259 }
1260 ]
1261 },
1262 {
1263 code: "(new foo()).bar",
1264 output: "new foo().bar",
1265 options: ["all", { enforceForNewInMemberExpressions: true }],
1266 errors: [
1267 {
1268 messageId: "unexpected",
1269 type: "NewExpression"
1270 }
1271 ]
1272 },
1273 {
1274 code: "(new foo())[bar]",
1275 output: "new foo()[bar]",
1276 options: ["all", { enforceForNewInMemberExpressions: true }],
1277 errors: [
1278 {
1279 messageId: "unexpected",
1280 type: "NewExpression"
1281 }
1282 ]
1283 },
1284 {
1285 code: "(new foo.bar()).baz",
1286 output: "new foo.bar().baz",
1287 options: ["all", { enforceForNewInMemberExpressions: true }],
1288 errors: [
1289 {
1290 messageId: "unexpected",
1291 type: "NewExpression"
1292 }
1293 ]
1294 },
1295
1296 // https://github.com/eslint/eslint/issues/8175
1297 invalid(
1298 "let a = [...(b)]",
1299 "let a = [...b]",
1300 "Identifier",
1301 1
1302 ),
1303 invalid(
1304 "let a = {...(b)}",
1305 "let a = {...b}",
1306 "Identifier",
1307 1
1308 ),
1309 invalid(
1310 "let a = {...(b)}",
1311 "let a = {...b}",
1312 "Identifier",
1313 1,
1314 { parserOptions: { ecmaVersion: 2018 } }
1315 ),
1316 invalid(
1317 "let a = [...((b, c))]",
1318 "let a = [...(b, c)]",
1319 "SequenceExpression",
1320 1
1321 ),
1322 invalid(
1323 "let a = {...((b, c))}",
1324 "let a = {...(b, c)}",
1325 "SequenceExpression",
1326 1
1327 ),
1328 invalid(
1329 "let a = {...((b, c))}",
1330 "let a = {...(b, c)}",
1331 "SequenceExpression",
1332 1,
1333 { parserOptions: { ecmaVersion: 2018 } }
1334 ),
1335 invalid(
1336 "class A extends (B) {}",
1337 "class A extends B {}",
1338 "Identifier",
1339 1
1340 ),
1341 invalid(
1342 "const A = class extends (B) {}",
1343 "const A = class extends B {}",
1344 "Identifier",
1345 1
1346 ),
1347 invalid(
1348 "class A extends ((B=C)) {}",
1349 "class A extends (B=C) {}",
1350 "AssignmentExpression",
1351 1
1352 ),
1353 invalid(
1354 "const A = class extends ((B=C)) {}",
1355 "const A = class extends (B=C) {}",
1356 "AssignmentExpression",
1357 1
1358 ),
1359 invalid(
1360 "class A extends ((++foo)) {}",
1361 "class A extends (++foo) {}",
1362 "UpdateExpression",
1363 1
1364 ),
1365 invalid(
1366 "export default ((a, b))",
1367 "export default (a, b)",
1368 "SequenceExpression",
1369 1,
1370 { parserOptions: { sourceType: "module" } }
1371 ),
1372 invalid(
1373 "export default (() => {})",
1374 "export default () => {}",
1375 "ArrowFunctionExpression",
1376 1,
1377 { parserOptions: { sourceType: "module" } }
1378 ),
1379 invalid(
1380 "export default ((a, b) => a + b)",
1381 "export default (a, b) => a + b",
1382 "ArrowFunctionExpression",
1383 1,
1384 { parserOptions: { sourceType: "module" } }
1385 ),
1386 invalid(
1387 "export default (a => a)",
1388 "export default a => a",
1389 "ArrowFunctionExpression",
1390 1,
1391 { parserOptions: { sourceType: "module" } }
1392 ),
1393 invalid(
1394 "export default (a = b)",
1395 "export default a = b",
1396 "AssignmentExpression",
1397 1,
1398 { parserOptions: { sourceType: "module" } }
1399 ),
1400 invalid(
1401 "export default (a ? b : c)",
1402 "export default a ? b : c",
1403 "ConditionalExpression",
1404 1,
1405 { parserOptions: { sourceType: "module" } }
1406 ),
1407 invalid(
1408 "export default (a)",
1409 "export default a",
1410 "Identifier",
1411 1,
1412 { parserOptions: { sourceType: "module" } }
1413 ),
1414 invalid(
1415 "for (foo of(bar));",
1416 "for (foo of bar);",
1417 "Identifier",
1418 1
1419 ),
1420 invalid(
1421 "for ((foo) of bar);",
1422 "for (foo of bar);",
1423 "Identifier",
1424 1
1425 ),
1426 invalid(
1427 "for (foo of ((bar, baz)));",
1428 "for (foo of (bar, baz));",
1429 "SequenceExpression",
1430 1
1431 ),
1432 invalid(
1433 "for ((foo)in bar);",
1434 "for (foo in bar);",
1435 "Identifier",
1436 1
1437 ),
1438 invalid(
1439 "for ((foo['bar'])of baz);",
1440 "for (foo['bar']of baz);",
1441 "MemberExpression",
1442 1
1443 ),
1444 invalid(
1445 "() => (({ foo: 1 }).foo)",
1446 "() => ({ foo: 1 }).foo",
1447 "MemberExpression",
1448 1
1449 ),
1450 invalid(
1451 "(let).foo",
1452 "let.foo",
1453 "Identifier",
1454 1
1455 ),
1456 invalid(
1457 "for ((let.foo) in bar);",
1458 "for (let.foo in bar);",
1459 "MemberExpression",
1460 1
1461 ),
1462 invalid(
1463 "for ((let).foo.bar in baz);",
1464 "for (let.foo.bar in baz);",
1465 "Identifier",
1466 1
1467 ),
1468 invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null),
1469 invalid(
1470 "(let)",
1471 "let",
1472 "Identifier",
1473 1
1474 ),
1475 invalid(
1476 "((let))",
1477 "(let)",
1478 "Identifier",
1479 1
1480 ),
1481 invalid("let s = `${(v)}`", "let s = `${v}`", "Identifier"),
1482 invalid("let s = `${(a, b)}`", "let s = `${a, b}`", "SequenceExpression"),
1483 invalid("function foo(a = (b)) {}", "function foo(a = b) {}", "Identifier"),
1484 invalid("const bar = (a = (b)) => a", "const bar = (a = b) => a", "Identifier"),
1485 invalid("const [a = (b)] = []", "const [a = b] = []", "Identifier"),
1486 invalid("const {a = (b)} = {}", "const {a = b} = {}", "Identifier"),
1487
1488 // LHS of assignments/Assignment targets
1489 invalid("(a) = b", "a = b", "Identifier"),
1490 invalid("(a.b) = c", "a.b = c", "MemberExpression"),
1491 invalid("(a) += b", "a += b", "Identifier"),
1492 invalid("(a.b) >>= c", "a.b >>= c", "MemberExpression"),
1493 invalid("[(a) = b] = []", "[a = b] = []", "Identifier"),
1494 invalid("[(a.b) = c] = []", "[a.b = c] = []", "MemberExpression"),
1495 invalid("({ a: (b) = c } = {})", "({ a: b = c } = {})", "Identifier"),
1496 invalid("({ a: (b.c) = d } = {})", "({ a: b.c = d } = {})", "MemberExpression"),
1497 invalid("[(a)] = []", "[a] = []", "Identifier"),
1498 invalid("[(a.b)] = []", "[a.b] = []", "MemberExpression"),
1499 invalid("[,(a),,] = []", "[,a,,] = []", "Identifier"),
1500 invalid("[...(a)] = []", "[...a] = []", "Identifier"),
1501 invalid("[...(a.b)] = []", "[...a.b] = []", "MemberExpression"),
1502 invalid("({ a: (b) } = {})", "({ a: b } = {})", "Identifier"),
1503 invalid("({ a: (b.c) } = {})", "({ a: b.c } = {})", "MemberExpression"),
1504
1505 /*
1506 * TODO: Add these tests for RestElement's parenthesized arguments in object patterns when that becomes supported by Espree.
1507 *
1508 * invalid("({ ...(a) } = {})", "({ ...a } = {})", "Identifier"),
1509 * invalid("({ ...(a.b) } = {})", "({ ...a.b } = {})", "MemberExpression")
1510 */
1511
1512 // https://github.com/eslint/eslint/issues/11706 (also in valid[])
1513 {
1514 code: "for ((a = (b in c)); ;);",
1515 output: "for ((a = b in c); ;);",
1516 errors: [
1517 {
1518 messageId: "unexpected"
1519 }
1520 ]
1521 },
1522 {
1523 code: "for (let a = ((b in c) && (d in e)); ;);",
1524 output: "for (let a = (b in c && d in e); ;);",
1525 errors: Array(2).fill(
1526 {
1527 messageId: "unexpected"
1528 }
1529 )
1530 },
1531 {
1532 code: "for (let a = ((b in c) in d); ;);",
1533 output: "for (let a = (b in c in d); ;);",
1534 errors: [
1535 {
1536 messageId: "unexpected"
1537 }
1538 ]
1539 },
1540 {
1541 code: "for (let a = (b && (c in d)), e = (f in g); ;);",
1542 output: "for (let a = (b && c in d), e = (f in g); ;);",
1543 errors: [
1544 {
1545 messageId: "unexpected"
1546 }
1547 ]
1548 },
1549 {
1550 code: "for (let a = (b + c), d = (e in f); ;);",
1551 output: "for (let a = b + c, d = (e in f); ;);",
1552 errors: [
1553 {
1554 messageId: "unexpected"
1555 }
1556 ]
1557 },
1558 {
1559 code: "for (let a = [(b in c)]; ;);",
1560 output: "for (let a = [b in c]; ;);",
1561 errors: [
1562 {
1563 messageId: "unexpected"
1564 }
1565 ]
1566 },
1567 {
1568 code: "for (let a = [b, (c in d)]; ;);",
1569 output: "for (let a = [b, c in d]; ;);",
1570 errors: [
1571 {
1572 messageId: "unexpected"
1573 }
1574 ]
1575 },
1576 {
1577 code: "for (let a = ([b in c]); ;);",
1578 output: "for (let a = [b in c]; ;);",
1579 errors: [
1580 {
1581 messageId: "unexpected"
1582 }
1583 ]
1584 },
1585 {
1586 code: "for (let a = ([b, c in d]); ;);",
1587 output: "for (let a = [b, c in d]; ;);",
1588 errors: [
1589 {
1590 messageId: "unexpected"
1591 }
1592 ]
1593 },
1594 {
1595 code: "for ((a = [b in c]); ;);",
1596 output: "for (a = [b in c]; ;);",
1597 errors: [
1598 {
1599 messageId: "unexpected"
1600 }
1601 ]
1602 },
1603 {
1604 code: "for (let a = [b && (c in d)]; ;);",
1605 output: "for (let a = [b && c in d]; ;);",
1606 errors: [
1607 {
1608 messageId: "unexpected"
1609 }
1610 ]
1611 },
1612 {
1613 code: "for (let a = [(b && c in d)]; ;);",
1614 output: "for (let a = [b && c in d]; ;);",
1615 errors: [
1616 {
1617 messageId: "unexpected"
1618 }
1619 ]
1620 },
1621 {
1622 code: "for (let a = ([b && c in d]); ;);",
1623 output: "for (let a = [b && c in d]; ;);",
1624 errors: [
1625 {
1626 messageId: "unexpected"
1627 }
1628 ]
1629 },
1630 {
1631 code: "for ((a = [b && c in d]); ;);",
1632 output: "for (a = [b && c in d]; ;);",
1633 errors: [
1634 {
1635 messageId: "unexpected"
1636 }
1637 ]
1638 },
1639 {
1640 code: "for ([(a in b)]; ;);",
1641 output: "for ([a in b]; ;);",
1642 errors: [
1643 {
1644 messageId: "unexpected"
1645 }
1646 ]
1647 },
1648 {
1649 code: "for (([a in b]); ;);",
1650 output: "for ([a in b]; ;);",
1651 errors: [
1652 {
1653 messageId: "unexpected"
1654 }
1655 ]
1656 },
1657 {
1658 code: "for (let a = [(b in c)], d = (e in f); ;);",
1659 output: "for (let a = [b in c], d = (e in f); ;);",
1660 errors: [
1661 {
1662 messageId: "unexpected"
1663 }
1664 ]
1665 },
1666 {
1667 code: "for (let [a = (b in c)] = []; ;);",
1668 output: "for (let [a = b in c] = []; ;);",
1669 errors: [
1670 {
1671 messageId: "unexpected"
1672 }
1673 ]
1674 },
1675 {
1676 code: "for (let [a = b && (c in d)] = []; ;);",
1677 output: "for (let [a = b && c in d] = []; ;);",
1678 errors: [
1679 {
1680 messageId: "unexpected"
1681 }
1682 ]
1683 },
1684 {
1685 code: "for (let a = () => { (b in c) }; ;);",
1686 output: "for (let a = () => { b in c }; ;);",
1687 errors: [
1688 {
1689 messageId: "unexpected"
1690 }
1691 ]
1692 },
1693 {
1694 code: "for (let a = () => { a && (b in c) }; ;);",
1695 output: "for (let a = () => { a && b in c }; ;);",
1696 errors: [
1697 {
1698 messageId: "unexpected"
1699 }
1700 ]
1701 },
1702 {
1703 code: "for (let a = function () { (b in c) }; ;);",
1704 output: "for (let a = function () { b in c }; ;);",
1705 errors: [
1706 {
1707 messageId: "unexpected"
1708 }
1709 ]
1710 },
1711 {
1712 code: "for (let a = { a: (b in c) }; ;);",
1713 output: "for (let a = { a: b in c }; ;);",
1714 errors: [
1715 {
1716 messageId: "unexpected"
1717 }
1718 ]
1719 },
1720 {
1721 code: "for (let a = { a: b && (c in d) }; ;);",
1722 output: "for (let a = { a: b && c in d }; ;);",
1723 errors: [
1724 {
1725 messageId: "unexpected"
1726 }
1727 ]
1728 },
1729 {
1730 code: "for (let { a = (b in c) } = {}; ;);",
1731 output: "for (let { a = b in c } = {}; ;);",
1732 errors: [
1733 {
1734 messageId: "unexpected"
1735 }
1736 ]
1737 },
1738 {
1739 code: "for (let { a = b && (c in d) } = {}; ;);",
1740 output: "for (let { a = b && c in d } = {}; ;);",
1741 errors: [
1742 {
1743 messageId: "unexpected"
1744 }
1745 ]
1746 },
1747 {
1748 code: "for (let { a: { b = c && (d in e) } } = {}; ;);",
1749 output: "for (let { a: { b = c && d in e } } = {}; ;);",
1750 errors: [
1751 {
1752 messageId: "unexpected"
1753 }
1754 ]
1755 },
1756 {
1757 code: "for (let a = `${(a in b)}`; ;);",
1758 output: "for (let a = `${a in b}`; ;);",
1759 errors: [
1760 {
1761 messageId: "unexpected"
1762 }
1763 ]
1764 },
1765 {
1766 code: "for (let a = `${a && (b in c)}`; ;);",
1767 output: "for (let a = `${a && b in c}`; ;);",
1768 errors: [
1769 {
1770 messageId: "unexpected"
1771 }
1772 ]
1773 },
1774 {
1775 code: "for (let a = (b = (c in d)) => {}; ;);",
1776 output: "for (let a = (b = c in d) => {}; ;);",
1777 errors: [
1778 {
1779 messageId: "unexpected"
1780 }
1781 ]
1782 },
1783 {
1784 code: "for (let a = (b = c && (d in e)) => {}; ;);",
1785 output: "for (let a = (b = c && d in e) => {}; ;);",
1786 errors: [
1787 {
1788 messageId: "unexpected"
1789 }
1790 ]
1791 },
1792 {
1793 code: "for (let a = (b, c = d && (e in f)) => {}; ;);",
1794 output: "for (let a = (b, c = d && e in f) => {}; ;);",
1795 errors: [
1796 {
1797 messageId: "unexpected"
1798 }
1799 ]
1800 },
1801 {
1802 code: "for (let a = function (b = c && (d in e)) {}; ;);",
1803 output: "for (let a = function (b = c && d in e) {}; ;);",
1804 errors: [
1805 {
1806 messageId: "unexpected"
1807 }
1808 ]
1809 },
1810 {
1811 code: "for (let a = function (b, c = d && (e in f)) {}; ;);",
1812 output: "for (let a = function (b, c = d && e in f) {}; ;);",
1813 errors: [
1814 {
1815 messageId: "unexpected"
1816 }
1817 ]
1818 },
1819 {
1820 code: "for (let a = b((c in d)); ;);",
1821 output: "for (let a = b(c in d); ;);",
1822 errors: [
1823 {
1824 messageId: "unexpected"
1825 }
1826 ]
1827 },
1828 {
1829 code: "for (let a = b(c, (d in e)); ;);",
1830 output: "for (let a = b(c, d in e); ;);",
1831 errors: [
1832 {
1833 messageId: "unexpected"
1834 }
1835 ]
1836 },
1837 {
1838 code: "for (let a = b(c && (d in e)); ;);",
1839 output: "for (let a = b(c && d in e); ;);",
1840 errors: [
1841 {
1842 messageId: "unexpected"
1843 }
1844 ]
1845 },
1846 {
1847 code: "for (let a = b(c, d && (e in f)); ;);",
1848 output: "for (let a = b(c, d && e in f); ;);",
1849 errors: [
1850 {
1851 messageId: "unexpected"
1852 }
1853 ]
1854 },
1855 {
1856 code: "for (let a = new b((c in d)); ;);",
1857 output: "for (let a = new b(c in d); ;);",
1858 errors: [
1859 {
1860 messageId: "unexpected"
1861 }
1862 ]
1863 },
1864 {
1865 code: "for (let a = new b(c, (d in e)); ;);",
1866 output: "for (let a = new b(c, d in e); ;);",
1867 errors: [
1868 {
1869 messageId: "unexpected"
1870 }
1871 ]
1872 },
1873 {
1874 code: "for (let a = new b(c && (d in e)); ;);",
1875 output: "for (let a = new b(c && d in e); ;);",
1876 errors: [
1877 {
1878 messageId: "unexpected"
1879 }
1880 ]
1881 },
1882 {
1883 code: "for (let a = new b(c, d && (e in f)); ;);",
1884 output: "for (let a = new b(c, d && e in f); ;);",
1885 errors: [
1886 {
1887 messageId: "unexpected"
1888 }
1889 ]
1890 },
1891 {
1892 code: "for (let a = b[(c in d)]; ;);",
1893 output: "for (let a = b[c in d]; ;);",
1894 errors: [
1895 {
1896 messageId: "unexpected"
1897 }
1898 ]
1899 },
1900 {
1901 code: "for (let a = b[c && (d in e)]; ;);",
1902 output: "for (let a = b[c && d in e]; ;);",
1903 errors: [
1904 {
1905 messageId: "unexpected"
1906 }
1907 ]
1908 },
1909 {
1910 code: "for (let a = b ? (c in d) : e; ;);",
1911 output: "for (let a = b ? c in d : e; ;);",
1912 errors: [
1913 {
1914 messageId: "unexpected"
1915 }
1916 ]
1917 },
1918 {
1919 code: "for (let a = b ? c && (d in e) : f; ;);",
1920 output: "for (let a = b ? c && d in e : f; ;);",
1921 errors: [
1922 {
1923 messageId: "unexpected"
1924 }
1925 ]
1926 },
1927 {
1928 code: "for (a ? b && (c in d) : e; ;);",
1929 output: "for (a ? b && c in d : e; ;);",
1930 errors: [
1931 {
1932 messageId: "unexpected"
1933 }
1934 ]
1935 },
1936 {
1937 code: "for (let a = ((b in c)); ;);",
1938 output: "for (let a = (b in c); ;);",
1939 errors: [
1940 {
1941 messageId: "unexpected"
1942 }
1943 ]
1944 },
1945 {
1946 code: "for (((a in b)); ;);",
1947 output: "for ((a in b); ;);",
1948 errors: [
1949 {
1950 messageId: "unexpected"
1951 }
1952 ]
1953 },
1954 {
1955 code: "for (((a && b in c && d)); ;);",
1956 output: "for ((a && b in c && d); ;);",
1957 errors: [
1958 {
1959 messageId: "unexpected"
1960 }
1961 ]
1962 },
1963 {
1964 code: "for (let a = (!(b in c)); ;);",
1965 output: "for (let a = !(b in c); ;);",
1966 errors: [
1967 {
1968 messageId: "unexpected"
1969 }
1970 ]
1971 },
1972 {
1973 code: "for (let a = (!(b && c in d)); ;);",
1974 output: "for (let a = !(b && c in d); ;);",
1975 errors: [
1976 {
1977 messageId: "unexpected"
1978 }
1979 ]
1980 },
1981 {
1982 code: "for (let a = !((b in c) && (d in e)); ;);",
1983 output: "for (let a = !(b in c && d in e); ;);",
1984 errors: Array(2).fill(
1985 {
1986 messageId: "unexpected"
1987 }
1988 )
1989 },
1990 {
1991 code: "for (let a = (x && (b in c)), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
1992 output: "for (let a = (x && b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
1993 errors: [
1994 {
1995 messageId: "unexpected"
1996 }
1997 ]
1998 },
1999 {
2000 code: "for (let a = (b in c), d = () => { for ((x && (e in f)); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2001 output: "for (let a = (b in c), d = () => { for ((x && e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2002 errors: [
2003 {
2004 messageId: "unexpected"
2005 }
2006 ]
2007 },
2008 {
2009 code: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((x && (g in h)); ;); }; ;); for((i in j); ;);",
2010 output: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((x && g in h); ;); }; ;); for((i in j); ;);",
2011 errors: [
2012 {
2013 messageId: "unexpected"
2014 }
2015 ]
2016 },
2017 {
2018 code: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((x && (i in j)); ;);",
2019 output: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((x && i in j); ;);",
2020 errors: [
2021 {
2022 messageId: "unexpected"
2023 }
2024 ]
2025 },
2026 {
2027 code: "for (let a = (x && (b in c)), d = () => { for ((e in f); ;); for ((y && (g in h)); ;); }; ;); for((i in j); ;);",
2028 output: "for (let a = (x && b in c), d = () => { for ((e in f); ;); for ((y && g in h); ;); }; ;); for((i in j); ;);",
2029 errors: Array(2).fill(
2030 {
2031 messageId: "unexpected"
2032 }
2033 )
2034 },
2035 {
2036 code: "for (let a = (x && (b in c)), d = () => { for ((y && (e in f)); ;); for ((z && (g in h)); ;); }; ;); for((w && (i in j)); ;);",
2037 output: "for (let a = (x && b in c), d = () => { for ((y && e in f); ;); for ((z && g in h); ;); }; ;); for((w && i in j); ;);",
2038 errors: Array(4).fill(
2039 {
2040 messageId: "unexpected"
2041 }
2042 )
2043 },
2044
2045 // https://github.com/eslint/eslint/issues/11706 regression tests (also in valid[])
2046 {
2047 code: "for (let a = (b); a > (b); a = (b)) a = (b); a = (b);",
2048 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2049 errors: Array(5).fill(
2050 {
2051 messageId: "unexpected"
2052 }
2053 )
2054 },
2055 {
2056 code: "for ((a = b); (a > b); (a = b)) (a = b); (a = b);",
2057 output: "for (a = b; a > b; a = b) a = b; a = b;",
2058 errors: Array(5).fill(
2059 {
2060 messageId: "unexpected"
2061 }
2062 )
2063 },
2064 {
2065 code: "for (let a = b; a > (b); a = (b)) a = (b); a = (b);",
2066 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2067 errors: Array(4).fill(
2068 {
2069 messageId: "unexpected"
2070 }
2071 )
2072 },
2073 {
2074 code: "for (let a = b; (a > b); (a = b)) (a = b); (a = b);",
2075 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2076 errors: Array(4).fill(
2077 {
2078 messageId: "unexpected"
2079 }
2080 )
2081 },
2082 {
2083 code: "for (; a > (b); a = (b)) a = (b); a = (b);",
2084 output: "for (; a > b; a = b) a = b; a = b;",
2085 errors: Array(4).fill(
2086 {
2087 messageId: "unexpected"
2088 }
2089 )
2090 },
2091 {
2092 code: "for (; (a > b); (a = b)) (a = b); (a = b);",
2093 output: "for (; a > b; a = b) a = b; a = b;",
2094 errors: Array(4).fill(
2095 {
2096 messageId: "unexpected"
2097 }
2098 )
2099 },
2100 {
2101 code: "for (let a = (b); a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2102 output: "for (let a = b; a = b in c; a = b in c) a = b in c; a = b in c;",
2103 errors: Array(5).fill(
2104 {
2105 messageId: "unexpected"
2106 }
2107 )
2108 },
2109 {
2110 code: "for (let a = (b); (a in b); (a in b)) (a in b); (a in b);",
2111 output: "for (let a = b; a in b; a in b) a in b; a in b;",
2112 errors: Array(5).fill(
2113 {
2114 messageId: "unexpected"
2115 }
2116 )
2117 },
2118 {
2119 code: "for (let a = b; a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2120 output: "for (let a = b; a = b in c; a = b in c) a = b in c; a = b in c;",
2121 errors: Array(4).fill(
2122 {
2123 messageId: "unexpected"
2124 }
2125 )
2126 },
2127 {
2128 code: "for (let a = b; (a in b); (a in b)) (a in b); (a in b);",
2129 output: "for (let a = b; a in b; a in b) a in b; a in b;",
2130 errors: Array(4).fill(
2131 {
2132 messageId: "unexpected"
2133 }
2134 )
2135 },
2136 {
2137 code: "for (; a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2138 output: "for (; a = b in c; a = b in c) a = b in c; a = b in c;",
2139 errors: Array(4).fill(
2140 {
2141 messageId: "unexpected"
2142 }
2143 )
2144 },
2145 {
2146 code: "for (; (a in b); (a in b)) (a in b); (a in b);",
2147 output: "for (; a in b; a in b) a in b; a in b;",
2148 errors: Array(4).fill(
2149 {
2150 messageId: "unexpected"
2151 }
2152 )
2153 },
2154 {
2155 code: "for (let a = (b + c), d = () => { for ((e + f); ;); for ((g + h); ;); }; ;); for((i + j); ;);",
2156 output: "for (let a = b + c, d = () => { for (e + f; ;); for (g + h; ;); }; ;); for(i + j; ;);",
2157 errors: Array(4).fill(
2158 {
2159 messageId: "unexpected"
2160 }
2161 )
2162 },
2163
2164 // import expressions
2165 invalid(
2166 "import((source))",
2167 "import(source)",
2168 "Identifier",
2169 1,
2170 { parserOptions: { ecmaVersion: 2020 } }
2171 ),
2172 invalid(
2173 "import((source = 'foo.js'))",
2174 "import(source = 'foo.js')",
2175 "AssignmentExpression",
2176 1,
2177 { parserOptions: { ecmaVersion: 2020 } }
2178 ),
2179 invalid(
2180 "import(((s,t)))",
2181 "import((s,t))",
2182 "SequenceExpression",
2183 1,
2184 { parserOptions: { ecmaVersion: 2020 } }
2185 ),
2186
2187 // https://github.com/eslint/eslint/issues/12127
2188 {
2189 code: "[1, ((2, 3))];",
2190 output: "[1, (2, 3)];",
2191 errors: [{ messageId: "unexpected" }]
2192 },
2193 {
2194 code: "const foo = () => ((bar, baz));",
2195 output: "const foo = () => (bar, baz);",
2196 errors: [{ messageId: "unexpected" }]
2197 },
2198 {
2199 code: "foo = ((bar, baz));",
2200 output: "foo = (bar, baz);",
2201 errors: [{ messageId: "unexpected" }]
2202 },
2203 {
2204 code: "foo + ((bar + baz));",
2205 output: "foo + (bar + baz);",
2206 errors: [{ messageId: "unexpected" }]
2207 },
2208 {
2209 code: "((foo + bar)) + baz;",
2210 output: "(foo + bar) + baz;",
2211 errors: [{ messageId: "unexpected" }]
2212 },
2213 {
2214 code: "foo * ((bar + baz));",
2215 output: "foo * (bar + baz);",
2216 errors: [{ messageId: "unexpected" }]
2217 },
2218 {
2219 code: "((foo + bar)) * baz;",
2220 output: "(foo + bar) * baz;",
2221 errors: [{ messageId: "unexpected" }]
2222 },
2223 {
2224 code: "new A(((foo, bar)))",
2225 output: "new A((foo, bar))",
2226 errors: [{ messageId: "unexpected" }]
2227 },
2228 {
2229 code: "class A{ [((foo, bar))]() {} }",
2230 output: "class A{ [(foo, bar)]() {} }",
2231 errors: [{ messageId: "unexpected" }]
2232 },
2233 {
2234 code: "new ((A, B))()",
2235 output: "new (A, B)()",
2236 errors: [{ messageId: "unexpected" }]
2237 },
2238 {
2239 code: "((foo, bar)) ? bar : baz;",
2240 output: "(foo, bar) ? bar : baz;",
2241 errors: [{ messageId: "unexpected" }]
2242 },
2243 {
2244 code: "((f ? o : o)) ? bar : baz;",
2245 output: "(f ? o : o) ? bar : baz;",
2246 errors: [{ messageId: "unexpected" }]
2247 },
2248 {
2249 code: "((f = oo)) ? bar : baz;",
2250 output: "(f = oo) ? bar : baz;",
2251 errors: [{ messageId: "unexpected" }]
2252 },
2253 {
2254 code: "foo ? ((bar, baz)) : baz;",
2255 output: "foo ? (bar, baz) : baz;",
2256 errors: [{ messageId: "unexpected" }]
2257 },
2258 {
2259 code: "foo ? bar : ((bar, baz));",
2260 output: "foo ? bar : (bar, baz);",
2261 errors: [{ messageId: "unexpected" }]
2262 },
2263 {
2264 code: "function foo(bar = ((baz1, baz2))) {}",
2265 output: "function foo(bar = (baz1, baz2)) {}",
2266 errors: [{ messageId: "unexpected" }]
2267 },
2268 {
2269 code: "var foo = { bar: ((baz1, baz2)) };",
2270 output: "var foo = { bar: (baz1, baz2) };",
2271 errors: [{ messageId: "unexpected" }]
2272 },
2273 {
2274 code: "var foo = { [((bar1, bar2))]: baz };",
2275 output: "var foo = { [(bar1, bar2)]: baz };",
2276 errors: [{ messageId: "unexpected" }]
2277 },
2278
2279 // adjacent tokens tests for division operator, comments and regular expressions
2280 invalid("a+/**/(/**/b)", "a+/**//**/b", "Identifier"),
2281 invalid("a+/**/(//\nb)", "a+/**///\nb", "Identifier"),
2282 invalid("a in(/**/b)", "a in/**/b", "Identifier"),
2283 invalid("a in(//\nb)", "a in//\nb", "Identifier"),
2284 invalid("a+(/**/b)", "a+/**/b", "Identifier"),
2285 invalid("a+/**/(b)", "a+/**/b", "Identifier"),
2286 invalid("a+(//\nb)", "a+//\nb", "Identifier"),
2287 invalid("a+//\n(b)", "a+//\nb", "Identifier"),
2288 invalid("a+(/^b$/)", "a+/^b$/", "Literal"),
2289 invalid("a/(/**/b)", "a/ /**/b", "Identifier"),
2290 invalid("a/(//\nb)", "a/ //\nb", "Identifier"),
2291 invalid("a/(/^b$/)", "a/ /^b$/", "Literal")
2292 ]
2293 });