]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-extra-parens.js
import eslint 7.28.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: 2021,
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 A)()",
100 "new (new a.b)()",
101 "new (new new foo())(bar)",
102 "(new foo).bar",
103 "(new foo)[bar]",
104 "(new foo).bar.baz",
105 "(new foo.bar).baz",
106 "(new foo).bar()",
107 "(new foo.bar).baz()",
108 "new (new foo).bar",
109 "new (new foo.bar).baz",
110 "(new new foo()).baz",
111 "(2 + 3) ** 4",
112 "2 ** (2 + 3)",
113 "new (import(source))",
114 "import((s,t))",
115
116 // same precedence
117 "a, b, c",
118 "a = b = c",
119 "a ? b ? c : d : e",
120 "a ? b : c ? d : e",
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 * b * c",
140 "a * (b * c)",
141 "!!a; typeof +b; void -c; ~delete d;",
142 "a(b)",
143 "a(b)(c)",
144 "a((b, c))",
145 "new new A",
146 "2 ** 3 ** 4",
147 "(2 ** 3) ** 4",
148
149 // constructs that contain expressions
150 "if(a);",
151 "with(a){}",
152 "switch(a){ case 0: break; }",
153 "function a(){ return b; }",
154 "var a = () => { return b; }",
155 "throw a;",
156 "while(a);",
157 "do; while(a);",
158 "for(;;);",
159 "for(a in b);",
160 "for(a in b, c);",
161 "for(a of b);",
162 "for (a of (b, c));",
163 "var a = (b, c);",
164 "[]",
165 "[a, b]",
166 "!{a}",
167 "!{a: 0, b: 1}",
168 "!{[a]:0}",
169 "!{[(a, b)]:0}",
170 "!{a, ...b}",
171 "const {a} = {}",
172 "const {a:b} = {}",
173 "const {a:b=1} = {}",
174 "const {[a]:b} = {}",
175 "const {[a]:b=1} = {}",
176 "const {[(a, b)]:c} = {}",
177 "const {a, ...b} = {}",
178 "class foo {}",
179 "class foo { constructor(){} a(){} get b(){} set b(bar){} get c(){} set d(baz){} static e(){} }",
180 "class foo { [a](){} get [b](){} set [b](bar){} get [c](){} set [d](baz){} static [e](){} }",
181 "class foo { [(a,b)](){} }",
182 "class foo { a(){} [b](){} c(){} [(d,e)](){} }",
183 "class foo { [(a,b)](){} c(){} [d](){} e(){} }",
184 "const foo = class { constructor(){} a(){} get b(){} set b(bar){} get c(){} set d(baz){} static e(){} }",
185
186 // ExpressionStatement restricted productions
187 "({});",
188 "(function(){});",
189 "(let[a] = b);",
190 "(function*(){});",
191 "(class{});",
192
193 // special cases
194 "(0).a",
195 "(123).a",
196 "(08).a",
197 "(09).a",
198 "(018).a",
199 "(012934).a",
200 "(5_000).a",
201 "(5_000_00).a",
202 "(function(){ }())",
203 "({a: function(){}}.a());",
204 "({a:0}.a ? b : c)",
205
206 // RegExp literal is allowed to have parens (#1589)
207 "var isA = (/^a$/).test('a');",
208 "var regex = (/^a$/);",
209 "function a(){ return (/^a$/); }",
210 "function a(){ return (/^a$/).test('a'); }",
211 "var isA = ((/^a$/)).test('a');",
212
213 // IIFE is allowed to have parens in any position (#655)
214 "var foo = (function() { return bar(); }())",
215 "var o = { foo: (function() { return bar(); }()) };",
216 "o.foo = (function(){ return bar(); }());",
217 "(function(){ return bar(); }()), (function(){ return bar(); }())",
218
219 // IIFE is allowed to have outer parens (#1004)
220 "var foo = (function() { return bar(); })()",
221 "var o = { foo: (function() { return bar(); })() };",
222 "o.foo = (function(){ return bar(); })();",
223 "(function(){ return bar(); })(), (function(){ return bar(); })()",
224 "function foo() { return (function(){}()); }",
225
226 // parens are required around yield
227 "var foo = (function*() { if ((yield foo()) + 1) { return; } }())",
228
229 // arrow functions have the precedence of an assignment expression
230 "(() => 0)()",
231 "(_ => 0)()",
232 "_ => 0, _ => 1",
233 "a = () => b = 0",
234 "0 ? _ => 0 : _ => 0",
235 "(_ => 0) || (_ => 0)",
236
237 // Object literals as arrow function bodies need parentheses
238 "x => ({foo: 1})",
239
240
241 // Exponentiation operator `**`
242 "1 + 2 ** 3",
243 "1 - 2 ** 3",
244 "2 ** -3",
245 "(-2) ** 3",
246 "(+2) ** 3",
247 "+ (2 ** 3)",
248
249 // https://github.com/eslint/eslint/issues/5789
250 "a => ({b: c}[d])",
251 "a => ({b: c}.d())",
252 "a => ({b: c}.d.e)",
253
254 // "functions" enables reports for function nodes only
255 { code: "(0)", options: ["functions"] },
256 { code: "((0))", options: ["functions"] },
257 { code: "a + (b * c)", options: ["functions"] },
258 { code: "a + ((b * c))", options: ["functions"] },
259 { code: "(a)(b)", options: ["functions"] },
260 { code: "((a))(b)", options: ["functions"] },
261 { code: "a, (b = c)", options: ["functions"] },
262 { code: "a, ((b = c))", options: ["functions"] },
263 { code: "for(a in (0));", options: ["functions"] },
264 { code: "for(a in ((0)));", options: ["functions"] },
265 { code: "var a = (b = c)", options: ["functions"] },
266 { code: "var a = ((b = c))", options: ["functions"] },
267 { code: "_ => (a = 0)", options: ["functions"] },
268 { code: "_ => ((a = 0))", options: ["functions"] },
269
270 // ["all", { conditionalAssign: false }] enables extra parens around conditional assignments
271 { code: "while ((foo = bar())) {}", options: ["all", { conditionalAssign: false }] },
272 { code: "if ((foo = bar())) {}", options: ["all", { conditionalAssign: false }] },
273 { code: "do; while ((foo = bar()))", options: ["all", { conditionalAssign: false }] },
274 { code: "for (;(a = b););", options: ["all", { conditionalAssign: false }] },
275 { code: "var a = ((b = c)) ? foo : bar;", options: ["all", { conditionalAssign: false }] },
276 { code: "while (((foo = bar()))) {}", options: ["all", { conditionalAssign: false }] },
277 { code: "var a = (((b = c))) ? foo : bar;", options: ["all", { conditionalAssign: false }] },
278
279 // ["all", { nestedBinaryExpressions: false }] enables extra parens around conditional assignments
280 { code: "a + (b * c)", options: ["all", { nestedBinaryExpressions: false }] },
281 { code: "(a * b) + c", options: ["all", { nestedBinaryExpressions: false }] },
282 { code: "(a * b) / c", options: ["all", { nestedBinaryExpressions: false }] },
283 { code: "a || (b && c)", options: ["all", { nestedBinaryExpressions: false }] },
284 { code: "a + ((b * c))", options: ["all", { nestedBinaryExpressions: false }] },
285 { code: "((a * b)) + c", options: ["all", { nestedBinaryExpressions: false }] },
286 { code: "((a * b)) / c", options: ["all", { nestedBinaryExpressions: false }] },
287 { code: "a || ((b && c))", options: ["all", { nestedBinaryExpressions: false }] },
288
289 // ["all", { returnAssign: false }] enables extra parens around expressions returned by return statements
290 { code: "function a(b) { return b || c; }", options: ["all", { returnAssign: false }] },
291 { code: "function a(b) { return; }", options: ["all", { returnAssign: false }] },
292 { code: "function a(b) { return (b = 1); }", options: ["all", { returnAssign: false }] },
293 { code: "function a(b) { return (b = c) || (b = d); }", options: ["all", { returnAssign: false }] },
294 { code: "function a(b) { return c ? (d = b) : (e = b); }", options: ["all", { returnAssign: false }] },
295 { code: "b => b || c;", options: ["all", { returnAssign: false }] },
296 { code: "b => (b = 1);", options: ["all", { returnAssign: false }] },
297 { code: "b => (b = c) || (b = d);", options: ["all", { returnAssign: false }] },
298 { code: "b => c ? (d = b) : (e = b);", options: ["all", { returnAssign: false }] },
299 { code: "b => { return b || c };", options: ["all", { returnAssign: false }] },
300 { code: "b => { return (b = 1) };", options: ["all", { returnAssign: false }] },
301 { code: "b => { return (b = c) || (b = d) };", options: ["all", { returnAssign: false }] },
302 { code: "b => { return c ? (d = b) : (e = b) };", options: ["all", { returnAssign: false }] },
303 { code: "function a(b) { return ((b = 1)); }", options: ["all", { returnAssign: false }] },
304 { code: "b => ((b = 1));", options: ["all", { returnAssign: false }] },
305
306 // https://github.com/eslint/eslint/issues/3653
307 "(function(){}).foo(), 1, 2;",
308 "(function(){}).foo++;",
309 "(function(){}).foo() || bar;",
310 "(function(){}).foo() + 1;",
311 "(function(){}).foo() ? bar : baz;",
312 "(function(){}).foo.bar();",
313 "(function(){}.foo());",
314 "(function(){}.foo.bar);",
315
316 "(class{}).foo(), 1, 2;",
317 "(class{}).foo++;",
318 "(class{}).foo() || bar;",
319 "(class{}).foo() + 1;",
320 "(class{}).foo() ? bar : baz;",
321 "(class{}).foo.bar();",
322 "(class{}.foo());",
323 "(class{}.foo.bar);",
324
325 // https://github.com/eslint/eslint/issues/4608
326 "function *a() { yield b; }",
327 "function *a() { yield yield; }",
328 "function *a() { yield b, c; }",
329 "function *a() { yield (b, c); }",
330 "function *a() { yield b + c; }",
331 "function *a() { (yield b) + c; }",
332
333 // https://github.com/eslint/eslint/issues/4229
334 [
335 "function a() {",
336 " return (",
337 " b",
338 " );",
339 "}"
340 ].join("\n"),
341 [
342 "function a() {",
343 " return (",
344 " <JSX />",
345 " );",
346 "}"
347 ].join("\n"),
348 [
349 "function a() {",
350 " return (",
351 " <></>",
352 " );",
353 "}"
354 ].join("\n"),
355 [
356 "throw (",
357 " a",
358 ");"
359 ].join("\n"),
360 [
361 "function *a() {",
362 " yield (",
363 " b",
364 " );",
365 "}"
366 ].join("\n"),
367
368 // linebreaks before postfix update operators are not allowed
369 "(a\n)++",
370 "(a\n)--",
371 "(a\n\n)++",
372 "(a.b\n)--",
373 "(a\n.b\n)++",
374 "(a[\nb\n]\n)--",
375 "(a[b]\n\n)++",
376
377 // async/await
378 "async function a() { await (a + b) }",
379 "async function a() { await (a + await b) }",
380 "async function a() { (await a)() }",
381 "async function a() { new (await a) }",
382 "async function a() { await (a ** b) }",
383 "async function a() { (await a) ** b }",
384
385 { code: "(foo instanceof bar) instanceof baz", options: ["all", { nestedBinaryExpressions: false }] },
386 { code: "(foo in bar) in baz", options: ["all", { nestedBinaryExpressions: false }] },
387 { code: "(foo + bar) + baz", options: ["all", { nestedBinaryExpressions: false }] },
388 { code: "(foo && bar) && baz", options: ["all", { nestedBinaryExpressions: false }] },
389 { code: "foo instanceof (bar instanceof baz)", options: ["all", { nestedBinaryExpressions: false }] },
390 { code: "foo in (bar in baz)", options: ["all", { nestedBinaryExpressions: false }] },
391 { code: "foo + (bar + baz)", options: ["all", { nestedBinaryExpressions: false }] },
392 { code: "foo && (bar && baz)", options: ["all", { nestedBinaryExpressions: false }] },
393 { code: "((foo instanceof bar)) instanceof baz", options: ["all", { nestedBinaryExpressions: false }] },
394 { code: "((foo in bar)) in baz", options: ["all", { nestedBinaryExpressions: false }] },
395
396 // https://github.com/eslint/eslint/issues/9019
397 "(async function() {});",
398 "(async function () { }());",
399
400 // ["all", { ignoreJSX: "all" }]
401 { code: "const Component = (<div />)", options: ["all", { ignoreJSX: "all" }] },
402 { code: "const Component = ((<div />))", options: ["all", { ignoreJSX: "all" }] },
403 {
404 code: [
405 "const Component = (<>",
406 " <p />",
407 "</>);"
408 ].join("\n"),
409 options: ["all", { ignoreJSX: "all" }]
410 },
411 {
412 code: [
413 "const Component = ((<>",
414 " <p />",
415 "</>));"
416 ].join("\n"),
417 options: ["all", { ignoreJSX: "all" }]
418 },
419 {
420 code: [
421 "const Component = (<div>",
422 " <p />",
423 "</div>);"
424 ].join("\n"),
425 options: ["all", { ignoreJSX: "all" }]
426 },
427 {
428 code: [
429 "const Component = (",
430 " <div />",
431 ");"
432 ].join("\n"),
433 options: ["all", { ignoreJSX: "all" }]
434 },
435 {
436 code: [
437 "const Component =",
438 " (<div />)"
439 ].join("\n"),
440 options: ["all", { ignoreJSX: "all" }]
441 },
442
443 // ["all", { ignoreJSX: "single-line" }]
444 { code: "const Component = (<div />);", options: ["all", { ignoreJSX: "single-line" }] },
445 { code: "const Component = ((<div />));", options: ["all", { ignoreJSX: "single-line" }] },
446 {
447 code: [
448 "const Component = (",
449 " <div />",
450 ");"
451 ].join("\n"),
452 options: ["all", { ignoreJSX: "single-line" }]
453 },
454 {
455 code: [
456 "const Component =",
457 "(<div />)"
458 ].join("\n"),
459 options: ["all", { ignoreJSX: "single-line" }]
460 },
461
462 // ["all", { ignoreJSX: "multi-line" }]
463 {
464 code: [
465 "const Component = (",
466 "<div>",
467 " <p />",
468 "</div>",
469 ");"
470 ].join("\n"),
471 options: ["all", { ignoreJSX: "multi-line" }]
472 },
473 {
474 code: [
475 "const Component = ((",
476 "<div>",
477 " <p />",
478 "</div>",
479 "));"
480 ].join("\n"),
481 options: ["all", { ignoreJSX: "multi-line" }]
482 },
483 {
484 code: [
485 "const Component = (<div>",
486 " <p />",
487 "</div>);"
488 ].join("\n"),
489 options: ["all", { ignoreJSX: "multi-line" }]
490 },
491 {
492 code: [
493 "const Component =",
494 "(<div>",
495 " <p />",
496 "</div>);"
497 ].join("\n"),
498 options: ["all", { ignoreJSX: "multi-line" }]
499 },
500 {
501 code: [
502 "const Component = (<div",
503 " prop={true}",
504 "/>)"
505 ].join("\n"),
506 options: ["all", { ignoreJSX: "multi-line" }]
507 },
508
509 // ["all", { enforceForArrowConditionals: false }]
510 { code: "var a = b => 1 ? 2 : 3", options: ["all", { enforceForArrowConditionals: false }] },
511 { code: "var a = (b) => (1 ? 2 : 3)", options: ["all", { enforceForArrowConditionals: false }] },
512 { code: "var a = (b) => ((1 ? 2 : 3))", options: ["all", { enforceForArrowConditionals: false }] },
513
514 // ["all", { enforceForSequenceExpressions: false }]
515 { code: "(a, b)", options: ["all", { enforceForSequenceExpressions: false }] },
516 { code: "((a, b))", options: ["all", { enforceForSequenceExpressions: false }] },
517 { code: "(foo(), bar());", options: ["all", { enforceForSequenceExpressions: false }] },
518 { code: "((foo(), bar()));", options: ["all", { enforceForSequenceExpressions: false }] },
519 { code: "if((a, b)){}", options: ["all", { enforceForSequenceExpressions: false }] },
520 { code: "if(((a, b))){}", options: ["all", { enforceForSequenceExpressions: false }] },
521 { code: "while ((val = foo(), val < 10));", options: ["all", { enforceForSequenceExpressions: false }] },
522
523 // ["all", { enforceForNewInMemberExpressions: false }]
524 { code: "(new foo()).bar", options: ["all", { enforceForNewInMemberExpressions: false }] },
525 { code: "(new foo())[bar]", options: ["all", { enforceForNewInMemberExpressions: false }] },
526 { code: "(new foo()).bar()", options: ["all", { enforceForNewInMemberExpressions: false }] },
527 { code: "(new foo(bar)).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
528 { code: "(new foo.bar()).baz", options: ["all", { enforceForNewInMemberExpressions: false }] },
529 { code: "(new foo.bar()).baz()", options: ["all", { enforceForNewInMemberExpressions: false }] },
530 { code: "((new foo.bar())).baz()", options: ["all", { enforceForNewInMemberExpressions: false }] },
531
532 // ["all", { enforceForFunctionPrototypeMethods: false }]
533 { code: "var foo = (function(){}).call()", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
534 { code: "var foo = (function(){}).apply()", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
535 { code: "var foo = (function(){}.call())", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
536 { code: "var foo = (function(){}.apply())", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
537 { code: "var foo = (function(){}).call(arg)", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
538 { code: "var foo = (function(){}.apply(arg))", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
539 { code: "var foo = (function(){}['call']())", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
540 { code: "var foo = (function(){})[`apply`]()", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
541 { code: "var foo = ((function(){})).call()", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
542 { code: "var foo = ((function(){}).apply())", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
543 { code: "var foo = ((function(){}.call()))", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
544 { code: "var foo = ((((function(){})).apply()))", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
545 { code: "foo((function(){}).call().bar)", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
546 { code: "foo = (function(){}).call()()", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
547 { code: "foo = (function(){}.call())()", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
548 { code: "var foo = { bar: (function(){}.call()) }", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
549 { code: "var foo = { [(function(){}.call())]: bar }", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
550 { code: "if((function(){}).call()){}", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
551 { code: "while((function(){}.apply())){}", options: ["all", { enforceForFunctionPrototypeMethods: false }] },
552
553 "let a = [ ...b ]",
554 "let a = { ...b }",
555 {
556 code: "let a = { ...b }",
557 parserOptions: { ecmaVersion: 2018 }
558 },
559 "let a = [ ...(b, c) ]",
560 "let a = { ...(b, c) }",
561 {
562 code: "let a = { ...(b, c) }",
563 parserOptions: { ecmaVersion: 2018 }
564 },
565 "var [x = (1, foo)] = bar",
566 "class A extends B {}",
567 "const A = class extends B {}",
568 "class A extends (B=C) {}",
569 "const A = class extends (B=C) {}",
570 "class A extends (++foo) {}",
571 "() => ({ foo: 1 })",
572 "() => ({ foo: 1 }).foo",
573 "() => ({ foo: 1 }.foo().bar).baz.qux()",
574 "() => ({ foo: 1 }.foo().bar + baz)",
575 {
576 code: "export default (a, b)",
577 parserOptions: { sourceType: "module" }
578 },
579 {
580 code: "export default (function(){}).foo",
581 parserOptions: { sourceType: "module" }
582 },
583 {
584 code: "export default (class{}).foo",
585 parserOptions: { sourceType: "module" }
586 },
587 "({}).hasOwnProperty.call(foo, bar)",
588 "({}) ? foo() : bar()",
589 "({}) + foo",
590 "(function(){}) + foo",
591 "(let)\nfoo",
592 "(let[foo]) = 1", // setting the 'foo' property of the 'let' variable to 1
593 {
594 code: "((function(){}).foo.bar)();",
595 options: ["functions"]
596 },
597 {
598 code: "((function(){}).foo)();",
599 options: ["functions"]
600 },
601 "(let)[foo]",
602
603 // ForStatement#init expression cannot start with `let[`. It would be parsed as a `let` declaration with array pattern, or a syntax error.
604 "for ((let[a]);;);",
605 "for ((let)[a];;);",
606 "for ((let[a] = 1);;);",
607 "for ((let[a]) = 1;;);",
608 "for ((let)[a] = 1;;);",
609 "for ((let[a, b] = foo);;);",
610 "for ((let[a].b = 1);;);",
611 "for ((let[a].b) = 1;;);",
612 "for ((let[a]).b = 1;;);",
613 "for ((let)[a].b = 1;;);",
614 "for ((let[a])();;);",
615 "for ((let)[a]();;);",
616 "for ((let[a]) + b;;);",
617
618 // ForInStatement#left expression cannot start with `let[`. It would be parsed as a `let` declaration with array pattern, or a syntax error.
619 "for ((let[foo]) in bar);",
620 "for ((let)[foo] in bar);",
621 "for ((let[foo].bar) in baz);",
622 "for ((let[foo]).bar in baz);",
623 "for ((let)[foo].bar in baz);",
624
625 // ForOfStatement#left expression cannot start with `let`. It's explicitly forbidden by the specification.
626 "for ((let) of foo);",
627 "for ((let).foo of bar);",
628 "for ((let.foo) of bar);",
629 "for ((let[foo]) of bar);",
630 "for ((let)[foo] of bar);",
631 "for ((let.foo.bar) of baz);",
632 "for ((let.foo).bar of baz);",
633 "for ((let).foo.bar of baz);",
634 "for ((let[foo].bar) of baz);",
635 "for ((let[foo]).bar of baz);",
636 "for ((let)[foo].bar of baz);",
637 "for ((let)().foo of bar);",
638 "for ((let()).foo of bar);",
639 "for ((let().foo) of bar);",
640
641 // https://github.com/eslint/eslint/issues/11706 (also in invalid[])
642 "for (let a = (b in c); ;);",
643 "for (let a = (b && c in d); ;);",
644 "for (let a = (b in c && d); ;);",
645 "for (let a = (b => b in c); ;);",
646 "for (let a = b => (b in c); ;);",
647 "for (let a = (b in c in d); ;);",
648 "for (let a = (b in c), d = (e in f); ;);",
649 "for (let a = (b => c => b in c); ;);",
650 "for (let a = (b && c && d in e); ;);",
651 "for (let a = b && (c in d); ;);",
652 "for (let a = (b in c) && (d in e); ;);",
653 "for ((a in b); ;);",
654 "for (a = (b in c); ;);",
655 "for ((a in b && c in d && e in f); ;);",
656 "for (let a = [] && (b in c); ;);",
657 "for (let a = (b in [c]); ;);",
658 "for (let a = b => (c in d); ;);",
659 "for (let a = (b in c) ? d : e; ;);",
660 "for (let a = (b in c ? d : e); ;);",
661 "for (let a = b ? c : (d in e); ;);",
662 "for (let a = (b in c), d = () => { for ((e in f);;); for ((g in h);;); }; ;); for((i in j); ;);",
663
664 // https://github.com/eslint/eslint/issues/11706 regression tests (also in invalid[])
665 "for (let a = b; a; a); a; a;",
666 "for (a; a; a); a; a;",
667 "for (; a; a); a; a;",
668 "for (let a = (b && c) === d; ;);",
669
670 "new (a()).b.c;",
671 "new (a().b).c;",
672 "new (a().b.c);",
673 "new (a().b().d);",
674 "new a().b().d;",
675 "new (a(b()).c)",
676 "new (a.b()).c",
677
678 // Nullish coalescing
679 { code: "var v = (a ?? b) || c", parserOptions: { ecmaVersion: 2020 } },
680 { code: "var v = a ?? (b || c)", parserOptions: { ecmaVersion: 2020 } },
681 { code: "var v = (a ?? b) && c", parserOptions: { ecmaVersion: 2020 } },
682 { code: "var v = a ?? (b && c)", parserOptions: { ecmaVersion: 2020 } },
683 { code: "var v = (a || b) ?? c", parserOptions: { ecmaVersion: 2020 } },
684 { code: "var v = a || (b ?? c)", parserOptions: { ecmaVersion: 2020 } },
685 { code: "var v = (a && b) ?? c", parserOptions: { ecmaVersion: 2020 } },
686 { code: "var v = a && (b ?? c)", parserOptions: { ecmaVersion: 2020 } },
687
688 // Optional chaining
689 { code: "var v = (obj?.aaa).bbb", parserOptions: { ecmaVersion: 2020 } },
690 { code: "var v = (obj?.aaa)()", parserOptions: { ecmaVersion: 2020 } },
691 { code: "var v = new (obj?.aaa)()", parserOptions: { ecmaVersion: 2020 } },
692 { code: "var v = new (obj?.aaa)", parserOptions: { ecmaVersion: 2020 } },
693 { code: "var v = (obj?.aaa)`template`", parserOptions: { ecmaVersion: 2020 } },
694 { code: "var v = (obj?.()).bbb", parserOptions: { ecmaVersion: 2020 } },
695 { code: "var v = (obj?.())()", parserOptions: { ecmaVersion: 2020 } },
696 { code: "var v = new (obj?.())()", parserOptions: { ecmaVersion: 2020 } },
697 { code: "var v = new (obj?.())", parserOptions: { ecmaVersion: 2020 } },
698 { code: "var v = (obj?.())`template`", parserOptions: { ecmaVersion: 2020 } },
699 { code: "(obj?.aaa).bbb = 0", parserOptions: { ecmaVersion: 2020 } },
700 { code: "var foo = (function(){})?.()", parserOptions: { ecmaVersion: 2020 } },
701 { code: "var foo = (function(){}?.())", parserOptions: { ecmaVersion: 2020 } },
702 {
703 code: "var foo = (function(){})?.call()",
704 options: ["all", { enforceForFunctionPrototypeMethods: false }],
705 parserOptions: { ecmaVersion: 2020 }
706 },
707 {
708 code: "var foo = (function(){}?.call())",
709 options: ["all", { enforceForFunctionPrototypeMethods: false }],
710 parserOptions: { ecmaVersion: 2020 }
711 }
712 ],
713
714 invalid: [
715 invalid("(0)", "0", "Literal"),
716 invalid("( 0 )", " 0 ", "Literal"),
717 invalid("if((0));", "if(0);", "Literal"),
718 invalid("if(( 0 ));", "if( 0 );", "Literal"),
719 invalid("with((0)){}", "with(0){}", "Literal"),
720 invalid("switch((0)){}", "switch(0){}", "Literal"),
721 invalid("switch(0){ case (1): break; }", "switch(0){ case 1: break; }", "Literal"),
722 invalid("for((0);;);", "for(0;;);", "Literal"),
723 invalid("for(;(0););", "for(;0;);", "Literal"),
724 invalid("for(;;(0));", "for(;;0);", "Literal"),
725 invalid("throw(0)", "throw 0", "Literal"),
726 invalid("while((0));", "while(0);", "Literal"),
727 invalid("do; while((0))", "do; while(0)", "Literal"),
728 invalid("for(a in (0));", "for(a in 0);", "Literal"),
729 invalid("for(a of (0));", "for(a of 0);", "Literal", 1),
730 invalid("const foo = {[(a)]:1}", "const foo = {[a]:1}", "Identifier", 1),
731 invalid("const foo = {[(a=b)]:1}", "const foo = {[a=b]:1}", "AssignmentExpression", 1),
732 invalid("const foo = {*[(Symbol.iterator)]() {}}", "const foo = {*[Symbol.iterator]() {}}", "MemberExpression", 1),
733 invalid("const foo = { get [(a)]() {}}", "const foo = { get [a]() {}}", "Identifier", 1),
734 invalid("const foo = {[(a+b)]:c, d}", "const foo = {[a+b]:c, d}", "BinaryExpression", 1),
735 invalid("const foo = {a, [(b+c)]:d, e}", "const foo = {a, [b+c]:d, e}", "BinaryExpression", 1),
736 invalid("const foo = {[(a+b)]:c, d:e}", "const foo = {[a+b]:c, d:e}", "BinaryExpression", 1),
737 invalid("const foo = {a:b, [(c+d)]:e, f:g}", "const foo = {a:b, [c+d]:e, f:g}", "BinaryExpression", 1),
738 invalid("const foo = {[(a+b)]:c, [d]:e}", "const foo = {[a+b]:c, [d]:e}", "BinaryExpression", 1),
739 invalid("const foo = {[a]:b, [(c+d)]:e, [f]:g}", "const foo = {[a]:b, [c+d]:e, [f]:g}", "BinaryExpression", 1),
740 invalid("const foo = {[(a+b)]:c, [(d,e)]:f}", "const foo = {[a+b]:c, [(d,e)]:f}", "BinaryExpression", 1),
741 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),
742 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),
743 invalid("const foo = {[a+(b*c)]:d}", "const foo = {[a+b*c]:d}", "BinaryExpression", 1),
744 invalid("const foo = {[(a, (b+c))]:d}", "const foo = {[(a, b+c)]:d}", "BinaryExpression", 1),
745 invalid("const {[(a)]:b} = {}", "const {[a]:b} = {}", "Identifier", 1),
746 invalid("const {[(a=b)]:c=1} = {}", "const {[a=b]:c=1} = {}", "AssignmentExpression", 1),
747 invalid("const {[(a+b)]:c, d} = {}", "const {[a+b]:c, d} = {}", "BinaryExpression", 1),
748 invalid("const {a, [(b+c)]:d, e} = {}", "const {a, [b+c]:d, e} = {}", "BinaryExpression", 1),
749 invalid("const {[(a+b)]:c, d:e} = {}", "const {[a+b]:c, d:e} = {}", "BinaryExpression", 1),
750 invalid("const {a:b, [(c+d)]:e, f:g} = {}", "const {a:b, [c+d]:e, f:g} = {}", "BinaryExpression", 1),
751 invalid("const {[(a+b)]:c, [d]:e} = {}", "const {[a+b]:c, [d]:e} = {}", "BinaryExpression", 1),
752 invalid("const {[a]:b, [(c+d)]:e, [f]:g} = {}", "const {[a]:b, [c+d]:e, [f]:g} = {}", "BinaryExpression", 1),
753 invalid("const {[(a+b)]:c, [(d,e)]:f} = {}", "const {[a+b]:c, [(d,e)]:f} = {}", "BinaryExpression", 1),
754 invalid("const {[(a,b)]:c, [(d+e)]:f, [(g,h)]:e} = {}", "const {[(a,b)]:c, [d+e]:f, [(g,h)]:e} = {}", "BinaryExpression", 1),
755 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),
756 invalid("const {[a+(b*c)]:d} = {}", "const {[a+b*c]:d} = {}", "BinaryExpression", 1),
757 invalid("const {[(a, (b+c))]:d} = {}", "const {[(a, b+c)]:d} = {}", "BinaryExpression", 1),
758 invalid("class foo { [(a)](){} }", "class foo { [a](){} }", "Identifier"),
759 invalid("class foo {*[(Symbol.iterator)]() {}}", "class foo {*[Symbol.iterator]() {}}", "MemberExpression"),
760 invalid("class foo { get [(a)](){} }", "class foo { get [a](){} }", "Identifier"),
761 invalid("class foo { set [(a)](bar){} }", "class foo { set [a](bar){} }", "Identifier"),
762 invalid("class foo { static [(a)](bar){} }", "class foo { static [a](bar){} }", "Identifier"),
763 invalid("class foo { [(a=b)](){} }", "class foo { [a=b](){} }", "AssignmentExpression"),
764 invalid("class foo { constructor (){} [(a+b)](){} }", "class foo { constructor (){} [a+b](){} }", "BinaryExpression"),
765 invalid("class foo { [(a+b)](){} constructor (){} }", "class foo { [a+b](){} constructor (){} }", "BinaryExpression"),
766 invalid("class foo { [(a+b)](){} c(){} }", "class foo { [a+b](){} c(){} }", "BinaryExpression"),
767 invalid("class foo { a(){} [(b+c)](){} d(){} }", "class foo { a(){} [b+c](){} d(){} }", "BinaryExpression"),
768 invalid("class foo { [(a+b)](){} [c](){} }", "class foo { [a+b](){} [c](){} }", "BinaryExpression"),
769 invalid("class foo { [a](){} [(b+c)](){} [d](){} }", "class foo { [a](){} [b+c](){} [d](){} }", "BinaryExpression"),
770 invalid("class foo { [(a+b)](){} [(c,d)](){} }", "class foo { [a+b](){} [(c,d)](){} }", "BinaryExpression"),
771 invalid("class foo { [(a,b)](){} [(c+d)](){} }", "class foo { [(a,b)](){} [c+d](){} }", "BinaryExpression"),
772 invalid("class foo { [a+(b*c)](){} }", "class foo { [a+b*c](){} }", "BinaryExpression"),
773 invalid("const foo = class { [(a)](){} }", "const foo = class { [a](){} }", "Identifier"),
774 invalid(
775 "var foo = (function*() { if ((yield foo())) { return; } }())",
776 "var foo = (function*() { if (yield foo()) { return; } }())",
777 "YieldExpression",
778 1
779 ),
780 invalid("f((0))", "f(0)", "Literal"),
781 invalid("f(0, (1))", "f(0, 1)", "Literal"),
782 invalid("!(0)", "!0", "Literal"),
783 invalid("a[(1)]", "a[1]", "Literal"),
784 invalid("(a)(b)", "a(b)", "Identifier"),
785 invalid("(async)", "async", "Identifier"),
786 invalid("(a, b)", "a, b", "SequenceExpression"),
787 invalid("var a = (b = c);", "var a = b = c;", "AssignmentExpression"),
788 invalid("function f(){ return (a); }", "function f(){ return a; }", "Identifier"),
789 invalid("[a, (b = c)]", "[a, b = c]", "AssignmentExpression"),
790 invalid("!{a: (b = c)}", "!{a: b = c}", "AssignmentExpression"),
791 invalid("typeof(0)", "typeof 0", "Literal"),
792 invalid("typeof (0)", "typeof 0", "Literal"),
793 invalid("typeof([])", "typeof[]", "ArrayExpression"),
794 invalid("typeof ([])", "typeof []", "ArrayExpression"),
795 invalid("typeof( 0)", "typeof 0", "Literal"),
796 invalid("typeof(typeof 5)", "typeof typeof 5", "UnaryExpression"),
797 invalid("typeof (typeof 5)", "typeof typeof 5", "UnaryExpression"),
798 invalid("+(+foo)", "+ +foo", "UnaryExpression"),
799 invalid("-(-foo)", "- -foo", "UnaryExpression"),
800 invalid("+(-foo)", "+-foo", "UnaryExpression"),
801 invalid("-(+foo)", "-+foo", "UnaryExpression"),
802 invalid("-((bar+foo))", "-(bar+foo)", "BinaryExpression"),
803 invalid("+((bar-foo))", "+(bar-foo)", "BinaryExpression"),
804 invalid("++(foo)", "++foo", "Identifier"),
805 invalid("--(foo)", "--foo", "Identifier"),
806 invalid("++\n(foo)", "++\nfoo", "Identifier"),
807 invalid("--\n(foo)", "--\nfoo", "Identifier"),
808 invalid("++(\nfoo)", "++\nfoo", "Identifier"),
809 invalid("--(\nfoo)", "--\nfoo", "Identifier"),
810 invalid("(foo)++", "foo++", "Identifier"),
811 invalid("(foo)--", "foo--", "Identifier"),
812 invalid("((foo)\n)++", "(foo\n)++", "Identifier"),
813 invalid("((foo\n))--", "(foo\n)--", "Identifier"),
814 invalid("((foo\n)\n)++", "(foo\n\n)++", "Identifier"),
815 invalid("(a\n.b)--", "a\n.b--", "MemberExpression"),
816 invalid("(a.\nb)++", "a.\nb++", "MemberExpression"),
817 invalid("(a\n[\nb\n])--", "a\n[\nb\n]--", "MemberExpression"),
818 invalid("(a || b) ? c : d", "a || b ? c : d", "LogicalExpression"),
819 invalid("a ? (b = c) : d", "a ? b = c : d", "AssignmentExpression"),
820 invalid("a ? b : (c = d)", "a ? b : c = d", "AssignmentExpression"),
821 invalid("(c = d) ? (b) : c", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
822 invalid("(c = d) ? b : (c)", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
823 invalid("f((a = b))", "f(a = b)", "AssignmentExpression"),
824 invalid("a, (b = c)", "a, b = c", "AssignmentExpression"),
825 invalid("a = (b * c)", "a = b * c", "BinaryExpression"),
826 invalid("a + (b * c)", "a + b * c", "BinaryExpression"),
827 invalid("(a * b) + c", "a * b + c", "BinaryExpression"),
828 invalid("(a * b) / c", "a * b / c", "BinaryExpression"),
829 invalid("(2) ** 3 ** 4", "2 ** 3 ** 4", "Literal", null),
830 invalid("2 ** (3 ** 4)", "2 ** 3 ** 4", "BinaryExpression", null),
831 invalid("(2 ** 3)", "2 ** 3", "BinaryExpression", null),
832 invalid("(2 ** 3) + 1", "2 ** 3 + 1", "BinaryExpression", null),
833 invalid("1 - (2 ** 3)", "1 - 2 ** 3", "BinaryExpression", null),
834 invalid("-((2 ** 3))", "-(2 ** 3)", "BinaryExpression", null),
835 invalid("typeof ((a ** b));", "typeof (a ** b);", "BinaryExpression", null),
836 invalid("((-2)) ** 3", "(-2) ** 3", "UnaryExpression", null),
837
838 invalid("a = (b * c)", "a = b * c", "BinaryExpression", null, { options: ["all", { nestedBinaryExpressions: false }] }),
839 invalid("(b * c)", "b * c", "BinaryExpression", null, { options: ["all", { nestedBinaryExpressions: false }] }),
840
841 invalid("a = (b = c)", "a = b = c", "AssignmentExpression"),
842 invalid("(a).b", "a.b", "Identifier"),
843 invalid("(0)[a]", "0[a]", "Literal"),
844 invalid("(0.0).a", "0.0.a", "Literal"),
845 invalid("(123.4).a", "123.4.a", "Literal"),
846 invalid("(0.0_0).a", "0.0_0.a", "Literal"),
847 invalid("(0xBEEF).a", "0xBEEF.a", "Literal"),
848 invalid("(0xBE_EF).a", "0xBE_EF.a", "Literal"),
849 invalid("(1e6).a", "1e6.a", "Literal"),
850 invalid("(0123).a", "0123.a", "Literal"),
851 invalid("(08.1).a", "08.1.a", "Literal"),
852 invalid("(09.).a", "09..a", "Literal"),
853 invalid("a[(function() {})]", "a[function() {}]", "FunctionExpression"),
854 invalid("new (function(){})", "new function(){}", "FunctionExpression"),
855 invalid("new (\nfunction(){}\n)", "new \nfunction(){}\n", "FunctionExpression", 1),
856 invalid("((function foo() {return 1;}))()", "(function foo() {return 1;})()", "FunctionExpression"),
857 invalid("((function(){ return bar(); })())", "(function(){ return bar(); })()", "CallExpression"),
858 invalid("(foo()).bar", "foo().bar", "CallExpression"),
859 invalid("(foo.bar()).baz", "foo.bar().baz", "CallExpression"),
860 invalid("(foo\n.bar())\n.baz", "foo\n.bar()\n.baz", "CallExpression"),
861 invalid("(new foo()).bar", "new foo().bar", "NewExpression"),
862 invalid("(new foo())[bar]", "new foo()[bar]", "NewExpression"),
863 invalid("(new foo()).bar()", "new foo().bar()", "NewExpression"),
864 invalid("(new foo(bar)).baz", "new foo(bar).baz", "NewExpression"),
865 invalid("(new foo.bar()).baz", "new foo.bar().baz", "NewExpression"),
866 invalid("(new foo.bar()).baz()", "new foo.bar().baz()", "NewExpression"),
867 invalid("new a[(b()).c]", "new a[b().c]", "CallExpression"),
868
869 invalid("(a)()", "a()", "Identifier"),
870 invalid("(a.b)()", "a.b()", "MemberExpression"),
871 invalid("(a())()", "a()()", "CallExpression"),
872 invalid("(a.b())()", "a.b()()", "CallExpression"),
873 invalid("(a().b)()", "a().b()", "MemberExpression"),
874 invalid("(a().b.c)()", "a().b.c()", "MemberExpression"),
875 invalid("new (A)", "new A", "Identifier"),
876 invalid("(new A())()", "new A()()", "NewExpression"),
877 invalid("(new A(1))()", "new A(1)()", "NewExpression"),
878 invalid("((new A))()", "(new A)()", "NewExpression"),
879 invalid("new (foo\n.baz\n.bar\n.foo.baz)", "new foo\n.baz\n.bar\n.foo.baz", "MemberExpression"),
880 invalid("new (foo.baz.bar.baz)", "new foo.baz.bar.baz", "MemberExpression"),
881 invalid("new ((a.b())).c", "new (a.b()).c", "CallExpression"),
882 invalid("new ((a().b)).c", "new (a().b).c", "MemberExpression"),
883 invalid("new ((a().b().d))", "new (a().b().d)", "MemberExpression"),
884 invalid("new ((a())).b.d", "new (a()).b.d", "CallExpression"),
885 invalid("new (a.b).d;", "new a.b.d;", "MemberExpression"),
886 invalid("new (new A())();", "new new A()();", "NewExpression"),
887 invalid("new (new A());", "new new A();", "NewExpression"),
888 invalid("new (new A);", "new new A;", "NewExpression"),
889 invalid("new (new a.b);", "new new a.b;", "NewExpression"),
890 invalid("(a().b).d;", "a().b.d;", "MemberExpression"),
891 invalid("(a.b()).d;", "a.b().d;", "CallExpression"),
892 invalid("(a.b).d;", "a.b.d;", "MemberExpression"),
893
894 invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1),
895 invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1),
896 invalid("a = (_ => 0)", "a = _ => 0", "ArrowFunctionExpression", 1),
897 invalid("_ => (a = 0)", "_ => a = 0", "AssignmentExpression", 1),
898 invalid("x => (({}))", "x => ({})", "ObjectExpression", 1),
899
900 invalid("new (function(){})", "new function(){}", "FunctionExpression", null, { options: ["functions"] }),
901 invalid("new (\nfunction(){}\n)", "new \nfunction(){}\n", "FunctionExpression", 1, { options: ["functions"] }),
902 invalid("((function foo() {return 1;}))()", "(function foo() {return 1;})()", "FunctionExpression", null, { options: ["functions"] }),
903 invalid("a[(function() {})]", "a[function() {}]", "FunctionExpression", null, { options: ["functions"] }),
904 invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
905 invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
906 invalid("a = (_ => 0)", "a = _ => 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
907
908
909 invalid("while ((foo = bar())) {}", "while (foo = bar()) {}", "AssignmentExpression"),
910 invalid("while ((foo = bar())) {}", "while (foo = bar()) {}", "AssignmentExpression", 1, { options: ["all", { conditionalAssign: true }] }),
911 invalid("if ((foo = bar())) {}", "if (foo = bar()) {}", "AssignmentExpression"),
912 invalid("do; while ((foo = bar()))", "do; while (foo = bar())", "AssignmentExpression"),
913 invalid("for (;(a = b););", "for (;a = b;);", "AssignmentExpression"),
914
915 // https://github.com/eslint/eslint/issues/3653
916 invalid("((function(){})).foo();", "(function(){}).foo();", "FunctionExpression"),
917 invalid("((function(){}).foo());", "(function(){}).foo();", "CallExpression"),
918 invalid("((function(){}).foo);", "(function(){}).foo;", "MemberExpression"),
919 invalid("0, (function(){}).foo();", "0, function(){}.foo();", "FunctionExpression"),
920 invalid("void (function(){}).foo();", "void function(){}.foo();", "FunctionExpression"),
921 invalid("++(function(){}).foo;", "++function(){}.foo;", "FunctionExpression"),
922 invalid("bar || (function(){}).foo();", "bar || function(){}.foo();", "FunctionExpression"),
923 invalid("1 + (function(){}).foo();", "1 + function(){}.foo();", "FunctionExpression"),
924 invalid("bar ? (function(){}).foo() : baz;", "bar ? function(){}.foo() : baz;", "FunctionExpression"),
925 invalid("bar ? baz : (function(){}).foo();", "bar ? baz : function(){}.foo();", "FunctionExpression"),
926 invalid("bar((function(){}).foo(), 0);", "bar(function(){}.foo(), 0);", "FunctionExpression"),
927 invalid("bar[(function(){}).foo()];", "bar[function(){}.foo()];", "FunctionExpression"),
928 invalid("var bar = (function(){}).foo();", "var bar = function(){}.foo();", "FunctionExpression"),
929
930 invalid("((class{})).foo();", "(class{}).foo();", "ClassExpression", null),
931 invalid("((class{}).foo());", "(class{}).foo();", "CallExpression", null),
932 invalid("((class{}).foo);", "(class{}).foo;", "MemberExpression", null),
933 invalid("0, (class{}).foo();", "0, class{}.foo();", "ClassExpression", null),
934 invalid("void (class{}).foo();", "void class{}.foo();", "ClassExpression", null),
935 invalid("++(class{}).foo;", "++class{}.foo;", "ClassExpression", null),
936 invalid("bar || (class{}).foo();", "bar || class{}.foo();", "ClassExpression", null),
937 invalid("1 + (class{}).foo();", "1 + class{}.foo();", "ClassExpression", null),
938 invalid("bar ? (class{}).foo() : baz;", "bar ? class{}.foo() : baz;", "ClassExpression", null),
939 invalid("bar ? baz : (class{}).foo();", "bar ? baz : class{}.foo();", "ClassExpression", null),
940 invalid("bar((class{}).foo(), 0);", "bar(class{}.foo(), 0);", "ClassExpression", null),
941 invalid("bar[(class{}).foo()];", "bar[class{}.foo()];", "ClassExpression", null),
942 invalid("var bar = (class{}).foo();", "var bar = class{}.foo();", "ClassExpression", null),
943 invalid("var foo = ((bar, baz));", "var foo = (bar, baz);", "SequenceExpression", null),
944
945 // https://github.com/eslint/eslint/issues/4608
946 invalid("function *a() { yield (b); }", "function *a() { yield b; }", "Identifier", null),
947 invalid("function *a() { (yield b), c; }", "function *a() { yield b, c; }", "YieldExpression", null),
948 invalid("function *a() { yield ((b, c)); }", "function *a() { yield (b, c); }", "SequenceExpression", null),
949 invalid("function *a() { yield (b + c); }", "function *a() { yield b + c; }", "BinaryExpression", null),
950
951 // https://github.com/eslint/eslint/issues/4229
952 invalid([
953 "function a() {",
954 " return (b);",
955 "}"
956 ].join("\n"), [
957 "function a() {",
958 " return b;",
959 "}"
960 ].join("\n"), "Identifier"),
961 invalid([
962 "function a() {",
963 " return",
964 " (b);",
965 "}"
966 ].join("\n"), [
967 "function a() {",
968 " return",
969 " b;",
970 "}"
971 ].join("\n"), "Identifier"),
972 invalid([
973 "function a() {",
974 " return ((",
975 " b",
976 " ));",
977 "}"
978 ].join("\n"), [
979 "function a() {",
980 " return (",
981 " b",
982 " );",
983 "}"
984 ].join("\n"), "Identifier"),
985 invalid([
986 "function a() {",
987 " return (<JSX />);",
988 "}"
989 ].join("\n"), [
990 "function a() {",
991 " return <JSX />;",
992 "}"
993 ].join("\n"), "JSXElement", null),
994 invalid([
995 "function a() {",
996 " return",
997 " (<JSX />);",
998 "}"
999 ].join("\n"), [
1000 "function a() {",
1001 " return",
1002 " <JSX />;",
1003 "}"
1004 ].join("\n"), "JSXElement", null),
1005 invalid([
1006 "function a() {",
1007 " return ((",
1008 " <JSX />",
1009 " ));",
1010 "}"
1011 ].join("\n"), [
1012 "function a() {",
1013 " return (",
1014 " <JSX />",
1015 " );",
1016 "}"
1017 ].join("\n"), "JSXElement", null),
1018 invalid([
1019 "function a() {",
1020 " return ((",
1021 " <></>",
1022 " ));",
1023 "}"
1024 ].join("\n"), [
1025 "function a() {",
1026 " return (",
1027 " <></>",
1028 " );",
1029 "}"
1030 ].join("\n"), "JSXFragment", null),
1031 invalid("throw (a);", "throw a;", "Identifier"),
1032 invalid([
1033 "throw ((",
1034 " a",
1035 "));"
1036 ].join("\n"), [
1037 "throw (",
1038 " a",
1039 ");"
1040 ].join("\n"), "Identifier"),
1041 invalid([
1042 "function *a() {",
1043 " yield (b);",
1044 "}"
1045 ].join("\n"), [
1046 "function *a() {",
1047 " yield b;",
1048 "}"
1049 ].join("\n"), "Identifier", null),
1050 invalid([
1051 "function *a() {",
1052 " yield",
1053 " (b);",
1054 "}"
1055 ].join("\n"), [
1056 "function *a() {",
1057 " yield",
1058 " b;",
1059 "}"
1060 ].join("\n"), "Identifier", null),
1061 invalid([
1062 "function *a() {",
1063 " yield ((",
1064 " b",
1065 " ));",
1066 "}"
1067 ].join("\n"), [
1068 "function *a() {",
1069 " yield (",
1070 " b",
1071 " );",
1072 "}"
1073 ].join("\n"), "Identifier", null),
1074
1075 // returnAssign option
1076 {
1077 code: "function a(b) { return (b || c); }",
1078 output: "function a(b) { return b || c; }",
1079 options: ["all", { returnAssign: false }],
1080 errors: [
1081 {
1082 messageId: "unexpected",
1083 type: "LogicalExpression"
1084 }
1085 ]
1086 },
1087 {
1088 code: "function a(b) { return ((b = c) || (d = e)); }",
1089 output: "function a(b) { return (b = c) || (d = e); }",
1090 errors: [
1091 {
1092 messageId: "unexpected",
1093 type: "LogicalExpression"
1094 }
1095 ]
1096 },
1097 {
1098 code: "function a(b) { return (b = 1); }",
1099 output: "function a(b) { return b = 1; }",
1100 errors: [
1101 {
1102 messageId: "unexpected",
1103 type: "AssignmentExpression"
1104 }
1105 ]
1106 },
1107 {
1108 code: "function a(b) { return c ? (d = b) : (e = b); }",
1109 output: "function a(b) { return c ? d = b : e = b; }",
1110 errors: [
1111 {
1112 messageId: "unexpected",
1113 type: "AssignmentExpression"
1114 },
1115 {
1116 messageId: "unexpected",
1117 type: "AssignmentExpression"
1118 }
1119 ]
1120 },
1121 {
1122 code: "b => (b || c);",
1123 output: "b => b || c;",
1124 options: ["all", { returnAssign: false }],
1125
1126 errors: [
1127 {
1128 messageId: "unexpected",
1129 type: "LogicalExpression"
1130 }
1131 ]
1132 },
1133 {
1134 code: "b => ((b = c) || (d = e));",
1135 output: "b => (b = c) || (d = e);",
1136 errors: [
1137 {
1138 messageId: "unexpected",
1139 type: "LogicalExpression"
1140 }
1141 ]
1142 },
1143 {
1144 code: "b => (b = 1);",
1145 output: "b => b = 1;",
1146 errors: [
1147 {
1148 messageId: "unexpected",
1149 type: "AssignmentExpression"
1150 }
1151 ]
1152 },
1153 {
1154 code: "b => c ? (d = b) : (e = b);",
1155 output: "b => c ? d = b : e = b;",
1156 errors: [
1157 {
1158 messageId: "unexpected",
1159 type: "AssignmentExpression"
1160 },
1161 {
1162 messageId: "unexpected",
1163 type: "AssignmentExpression"
1164 }
1165 ]
1166 },
1167 {
1168 code: "b => { return (b || c); }",
1169 output: "b => { return b || c; }",
1170 options: ["all", { returnAssign: false }],
1171 errors: [
1172 {
1173 messageId: "unexpected",
1174 type: "LogicalExpression"
1175 }
1176 ]
1177 },
1178 {
1179 code: "b => { return ((b = c) || (d = e)) };",
1180 output: "b => { return (b = c) || (d = e) };",
1181 errors: [
1182 {
1183 messageId: "unexpected",
1184 type: "LogicalExpression"
1185 }
1186 ]
1187 },
1188 {
1189 code: "b => { return (b = 1) };",
1190 output: "b => { return b = 1 };",
1191 errors: [
1192 {
1193 messageId: "unexpected",
1194 type: "AssignmentExpression"
1195 }
1196 ]
1197 },
1198 {
1199 code: "b => { return c ? (d = b) : (e = b); }",
1200 output: "b => { return c ? d = b : e = b; }",
1201 errors: [
1202 {
1203 messageId: "unexpected",
1204 type: "AssignmentExpression"
1205 },
1206 {
1207 messageId: "unexpected",
1208 type: "AssignmentExpression"
1209 }
1210 ]
1211 },
1212
1213 // async/await
1214 {
1215 code: "async function a() { (await a) + (await b); }",
1216 output: "async function a() { await a + await b; }",
1217 errors: [
1218 {
1219 messageId: "unexpected",
1220 type: "AwaitExpression"
1221 },
1222 {
1223 messageId: "unexpected",
1224 type: "AwaitExpression"
1225 }
1226 ]
1227 },
1228 invalid("async function a() { await (a); }", "async function a() { await a; }", "Identifier", null),
1229 invalid("async function a() { await (a()); }", "async function a() { await a(); }", "CallExpression", null),
1230 invalid("async function a() { await (+a); }", "async function a() { await +a; }", "UnaryExpression", null),
1231 invalid("async function a() { +(await a); }", "async function a() { +await a; }", "AwaitExpression", null),
1232 invalid("async function a() { await ((a,b)); }", "async function a() { await (a,b); }", "SequenceExpression", null),
1233 invalid("async function a() { a ** (await b); }", "async function a() { a ** await b; }", "AwaitExpression", null),
1234
1235 invalid("(foo) instanceof bar", "foo instanceof bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1236 invalid("(foo) in bar", "foo in bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1237 invalid("(foo) + bar", "foo + bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1238 invalid("(foo) && bar", "foo && bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1239 invalid("foo instanceof (bar)", "foo instanceof bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1240 invalid("foo in (bar)", "foo in bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1241 invalid("foo + (bar)", "foo + bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1242 invalid("foo && (bar)", "foo && bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1243
1244 // ["all", { ignoreJSX: "multi-line" }]
1245 invalid("const Component = (<div />);", "const Component = <div />;", "JSXElement", 1, {
1246 options: ["all", { ignoreJSX: "multi-line" }]
1247 }),
1248 invalid([
1249 "const Component = (",
1250 " <div />",
1251 ");"
1252 ].join("\n"), "const Component = \n <div />\n;", "JSXElement", 1, {
1253 options: ["all", { ignoreJSX: "multi-line" }]
1254 }),
1255 invalid([
1256 "const Component = (",
1257 " <></>",
1258 ");"
1259 ].join("\n"), "const Component = \n <></>\n;", "JSXFragment", 1, {
1260 options: ["all", { ignoreJSX: "multi-line" }]
1261 }),
1262
1263 // ["all", { ignoreJSX: "single-line" }]
1264 invalid([
1265 "const Component = (",
1266 "<div>",
1267 " <p />",
1268 "</div>",
1269 ");"
1270 ].join("\n"), "const Component = \n<div>\n <p />\n</div>\n;", "JSXElement", 1, {
1271 options: ["all", { ignoreJSX: "single-line" }]
1272 }),
1273 invalid([
1274 "const Component = (<div>",
1275 " <p />",
1276 "</div>);"
1277 ].join("\n"), "const Component = <div>\n <p />\n</div>;", "JSXElement", 1, {
1278 options: ["all", { ignoreJSX: "single-line" }]
1279 }),
1280 invalid([
1281 "const Component = (<div",
1282 " prop={true}",
1283 "/>)"
1284 ].join("\n"), "const Component = <div\n prop={true}\n/>", "JSXElement", 1, {
1285 options: ["all", { ignoreJSX: "single-line" }]
1286 }),
1287
1288 // ["all", { ignoreJSX: "none" }] default, same as unspecified
1289 invalid("const Component = (<div />);", "const Component = <div />;", "JSXElement", 1, {
1290 options: ["all", { ignoreJSX: "none" }]
1291 }),
1292 invalid([
1293 "const Component = (<div>",
1294 "<p />",
1295 "</div>)"
1296 ].join("\n"), "const Component = <div>\n<p />\n</div>", "JSXElement", 1, {
1297 options: ["all", { ignoreJSX: "none" }]
1298 }),
1299
1300 // ["all", { enforceForArrowConditionals: true }]
1301 {
1302 code: "var a = (b) => (1 ? 2 : 3)",
1303 output: "var a = (b) => 1 ? 2 : 3",
1304 options: ["all", { enforceForArrowConditionals: true }],
1305 errors: [
1306 {
1307 messageId: "unexpected"
1308 }
1309 ]
1310 },
1311 {
1312 code: "var a = (b) => ((1 ? 2 : 3))",
1313 output: "var a = (b) => (1 ? 2 : 3)",
1314 options: ["all", { enforceForArrowConditionals: true }],
1315 errors: [
1316 {
1317 messageId: "unexpected"
1318 }
1319 ]
1320 },
1321
1322 // ["all", { enforceForSequenceExpressions: true }]
1323 {
1324 code: "(a, b)",
1325 output: "a, b",
1326 options: ["all"],
1327 errors: [
1328 {
1329 messageId: "unexpected",
1330 type: "SequenceExpression"
1331 }
1332 ]
1333 },
1334 {
1335 code: "(a, b)",
1336 output: "a, b",
1337 options: ["all", {}],
1338 errors: [
1339 {
1340 messageId: "unexpected",
1341 type: "SequenceExpression"
1342 }
1343 ]
1344 },
1345 {
1346 code: "(a, b)",
1347 output: "a, b",
1348 options: ["all", { enforceForSequenceExpressions: true }],
1349 errors: [
1350 {
1351 messageId: "unexpected",
1352 type: "SequenceExpression"
1353 }
1354 ]
1355 },
1356 {
1357 code: "(foo(), bar());",
1358 output: "foo(), bar();",
1359 options: ["all", { enforceForSequenceExpressions: true }],
1360 errors: [
1361 {
1362 messageId: "unexpected",
1363 type: "SequenceExpression"
1364 }
1365 ]
1366 },
1367 {
1368 code: "if((a, b)){}",
1369 output: "if(a, b){}",
1370 options: ["all", { enforceForSequenceExpressions: true }],
1371 errors: [
1372 {
1373 messageId: "unexpected",
1374 type: "SequenceExpression"
1375 }
1376 ]
1377 },
1378 {
1379 code: "while ((val = foo(), val < 10));",
1380 output: "while (val = foo(), val < 10);",
1381 options: ["all", { enforceForSequenceExpressions: true }],
1382 errors: [
1383 {
1384 messageId: "unexpected",
1385 type: "SequenceExpression"
1386 }
1387 ]
1388 },
1389
1390 // ["all", { enforceForNewInMemberExpressions: true }]
1391 {
1392 code: "(new foo()).bar",
1393 output: "new foo().bar",
1394 options: ["all"],
1395 errors: [
1396 {
1397 messageId: "unexpected",
1398 type: "NewExpression"
1399 }
1400 ]
1401 },
1402 {
1403 code: "(new foo()).bar",
1404 output: "new foo().bar",
1405 options: ["all", {}],
1406 errors: [
1407 {
1408 messageId: "unexpected",
1409 type: "NewExpression"
1410 }
1411 ]
1412 },
1413 {
1414 code: "(new foo()).bar",
1415 output: "new foo().bar",
1416 options: ["all", { enforceForNewInMemberExpressions: true }],
1417 errors: [
1418 {
1419 messageId: "unexpected",
1420 type: "NewExpression"
1421 }
1422 ]
1423 },
1424 {
1425 code: "(new foo())[bar]",
1426 output: "new foo()[bar]",
1427 options: ["all", { enforceForNewInMemberExpressions: true }],
1428 errors: [
1429 {
1430 messageId: "unexpected",
1431 type: "NewExpression"
1432 }
1433 ]
1434 },
1435 {
1436 code: "(new foo.bar()).baz",
1437 output: "new foo.bar().baz",
1438 options: ["all", { enforceForNewInMemberExpressions: true }],
1439 errors: [
1440 {
1441 messageId: "unexpected",
1442 type: "NewExpression"
1443 }
1444 ]
1445 },
1446
1447 // enforceForFunctionPrototypeMethods
1448 {
1449 code: "var foo = (function(){}).call()",
1450 output: "var foo = function(){}.call()",
1451 options: ["all"],
1452 errors: [
1453 {
1454 messageId: "unexpected",
1455 type: "FunctionExpression"
1456 }
1457 ]
1458 },
1459 {
1460 code: "var foo = (function(){}.apply())",
1461 output: "var foo = function(){}.apply()",
1462 options: ["all"],
1463 errors: [
1464 {
1465 messageId: "unexpected",
1466 type: "CallExpression"
1467 }
1468 ]
1469 },
1470 {
1471 code: "var foo = (function(){}).apply()",
1472 output: "var foo = function(){}.apply()",
1473 options: ["all", {}],
1474 errors: [
1475 {
1476 messageId: "unexpected",
1477 type: "FunctionExpression"
1478 }
1479 ]
1480 },
1481 {
1482 code: "var foo = (function(){}.call())",
1483 output: "var foo = function(){}.call()",
1484 options: ["all", {}],
1485 errors: [
1486 {
1487 messageId: "unexpected",
1488 type: "CallExpression"
1489 }
1490 ]
1491 },
1492 {
1493 code: "var foo = (function(){}).call()",
1494 output: "var foo = function(){}.call()",
1495 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1496 errors: [
1497 {
1498 messageId: "unexpected",
1499 type: "FunctionExpression"
1500 }
1501 ]
1502 },
1503 {
1504 code: "var foo = (function(){}).apply()",
1505 output: "var foo = function(){}.apply()",
1506 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1507 errors: [
1508 {
1509 messageId: "unexpected",
1510 type: "FunctionExpression"
1511 }
1512 ]
1513 },
1514 {
1515 code: "var foo = (function(){}.call())",
1516 output: "var foo = function(){}.call()",
1517 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1518 errors: [
1519 {
1520 messageId: "unexpected",
1521 type: "CallExpression"
1522 }
1523 ]
1524 },
1525 {
1526 code: "var foo = (function(){}.apply())",
1527 output: "var foo = function(){}.apply()",
1528 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1529 errors: [
1530 {
1531 messageId: "unexpected",
1532 type: "CallExpression"
1533 }
1534 ]
1535 },
1536 {
1537 code: "var foo = (function(){}.call)()", // removing these parens does not cause any conflicts with wrap-iife
1538 output: "var foo = function(){}.call()",
1539 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1540 errors: [
1541 {
1542 messageId: "unexpected",
1543 type: "MemberExpression"
1544 }
1545 ]
1546 },
1547 {
1548 code: "var foo = (function(){}.apply)()", // removing these parens does not cause any conflicts with wrap-iife
1549 output: "var foo = function(){}.apply()",
1550 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1551 errors: [
1552 {
1553 messageId: "unexpected",
1554 type: "MemberExpression"
1555 }
1556 ]
1557 },
1558 {
1559 code: "var foo = (function(){}).call",
1560 output: "var foo = function(){}.call",
1561 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1562 errors: [
1563 {
1564 messageId: "unexpected",
1565 type: "FunctionExpression"
1566 }
1567 ]
1568 },
1569 {
1570 code: "var foo = (function(){}.call)",
1571 output: "var foo = function(){}.call",
1572 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1573 errors: [
1574 {
1575 messageId: "unexpected",
1576 type: "MemberExpression"
1577 }
1578 ]
1579 },
1580 {
1581 code: "var foo = new (function(){}).call()",
1582 output: "var foo = new function(){}.call()",
1583 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1584 errors: [
1585 {
1586 messageId: "unexpected",
1587 type: "FunctionExpression"
1588 }
1589 ]
1590 },
1591 {
1592 code: "var foo = (new function(){}.call())",
1593 output: "var foo = new function(){}.call()",
1594 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1595 errors: [
1596 {
1597 messageId: "unexpected",
1598 type: "NewExpression"
1599 }
1600 ]
1601 },
1602 {
1603 code: "var foo = (function(){})[call]()",
1604 output: "var foo = function(){}[call]()",
1605 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1606 errors: [
1607 {
1608 messageId: "unexpected",
1609 type: "FunctionExpression"
1610 }
1611 ]
1612 },
1613 {
1614 code: "var foo = (function(){}[apply]())",
1615 output: "var foo = function(){}[apply]()",
1616 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1617 errors: [
1618 {
1619 messageId: "unexpected",
1620 type: "CallExpression"
1621 }
1622 ]
1623 },
1624 {
1625 code: "var foo = (function(){}).bar()",
1626 output: "var foo = function(){}.bar()",
1627 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1628 errors: [
1629 {
1630 messageId: "unexpected",
1631 type: "FunctionExpression"
1632 }
1633 ]
1634 },
1635 {
1636 code: "var foo = (function(){}.bar())",
1637 output: "var foo = function(){}.bar()",
1638 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1639 errors: [
1640 {
1641 messageId: "unexpected",
1642 type: "CallExpression"
1643 }
1644 ]
1645 },
1646 {
1647 code: "var foo = (function(){}).call.call()",
1648 output: "var foo = function(){}.call.call()",
1649 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1650 errors: [
1651 {
1652 messageId: "unexpected",
1653 type: "FunctionExpression"
1654 }
1655 ]
1656 },
1657 {
1658 code: "var foo = (function(){}.call.call())",
1659 output: "var foo = function(){}.call.call()",
1660 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1661 errors: [
1662 {
1663 messageId: "unexpected",
1664 type: "CallExpression"
1665 }
1666 ]
1667 },
1668 {
1669 code: "var foo = (call())",
1670 output: "var foo = call()",
1671 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1672 errors: [
1673 {
1674 messageId: "unexpected",
1675 type: "CallExpression"
1676 }
1677 ]
1678 },
1679 {
1680 code: "var foo = (apply())",
1681 output: "var foo = apply()",
1682 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1683 errors: [
1684 {
1685 messageId: "unexpected",
1686 type: "CallExpression"
1687 }
1688 ]
1689 },
1690 {
1691 code: "var foo = (bar).call()",
1692 output: "var foo = bar.call()",
1693 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1694 errors: [
1695 {
1696 messageId: "unexpected",
1697 type: "Identifier"
1698 }
1699 ]
1700 },
1701 {
1702 code: "var foo = (bar.call())",
1703 output: "var foo = bar.call()",
1704 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1705 errors: [
1706 {
1707 messageId: "unexpected",
1708 type: "CallExpression"
1709 }
1710 ]
1711 },
1712 {
1713 code: "((() => {}).call())",
1714 output: "(() => {}).call()",
1715 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1716 errors: [
1717 {
1718 messageId: "unexpected",
1719 type: "CallExpression"
1720 }
1721 ]
1722 },
1723 {
1724 code: "var foo = function(){}.call((a.b))",
1725 output: "var foo = function(){}.call(a.b)",
1726 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1727 errors: [
1728 {
1729 messageId: "unexpected",
1730 type: "MemberExpression"
1731 }
1732 ]
1733 },
1734 {
1735 code: "var foo = function(){}.call((a).b)",
1736 output: "var foo = function(){}.call(a.b)",
1737 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1738 errors: [
1739 {
1740 messageId: "unexpected",
1741 type: "Identifier"
1742 }
1743 ]
1744 },
1745 {
1746 code: "var foo = function(){}[('call')]()",
1747 output: "var foo = function(){}['call']()",
1748 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1749 errors: [
1750 {
1751 messageId: "unexpected",
1752 type: "Literal"
1753 }
1754 ]
1755 },
1756
1757 // https://github.com/eslint/eslint/issues/8175
1758 invalid(
1759 "let a = [...(b)]",
1760 "let a = [...b]",
1761 "Identifier",
1762 1
1763 ),
1764 invalid(
1765 "let a = {...(b)}",
1766 "let a = {...b}",
1767 "Identifier",
1768 1
1769 ),
1770 invalid(
1771 "let a = {...(b)}",
1772 "let a = {...b}",
1773 "Identifier",
1774 1,
1775 { parserOptions: { ecmaVersion: 2018 } }
1776 ),
1777 invalid(
1778 "let a = [...((b, c))]",
1779 "let a = [...(b, c)]",
1780 "SequenceExpression",
1781 1
1782 ),
1783 invalid(
1784 "let a = {...((b, c))}",
1785 "let a = {...(b, c)}",
1786 "SequenceExpression",
1787 1
1788 ),
1789 invalid(
1790 "let a = {...((b, c))}",
1791 "let a = {...(b, c)}",
1792 "SequenceExpression",
1793 1,
1794 { parserOptions: { ecmaVersion: 2018 } }
1795 ),
1796 invalid(
1797 "class A extends (B) {}",
1798 "class A extends B {}",
1799 "Identifier",
1800 1
1801 ),
1802 invalid(
1803 "const A = class extends (B) {}",
1804 "const A = class extends B {}",
1805 "Identifier",
1806 1
1807 ),
1808 invalid(
1809 "class A extends ((B=C)) {}",
1810 "class A extends (B=C) {}",
1811 "AssignmentExpression",
1812 1
1813 ),
1814 invalid(
1815 "const A = class extends ((B=C)) {}",
1816 "const A = class extends (B=C) {}",
1817 "AssignmentExpression",
1818 1
1819 ),
1820 invalid(
1821 "class A extends ((++foo)) {}",
1822 "class A extends (++foo) {}",
1823 "UpdateExpression",
1824 1
1825 ),
1826 invalid(
1827 "export default ((a, b))",
1828 "export default (a, b)",
1829 "SequenceExpression",
1830 1,
1831 { parserOptions: { sourceType: "module" } }
1832 ),
1833 invalid(
1834 "export default (() => {})",
1835 "export default () => {}",
1836 "ArrowFunctionExpression",
1837 1,
1838 { parserOptions: { sourceType: "module" } }
1839 ),
1840 invalid(
1841 "export default ((a, b) => a + b)",
1842 "export default (a, b) => a + b",
1843 "ArrowFunctionExpression",
1844 1,
1845 { parserOptions: { sourceType: "module" } }
1846 ),
1847 invalid(
1848 "export default (a => a)",
1849 "export default a => a",
1850 "ArrowFunctionExpression",
1851 1,
1852 { parserOptions: { sourceType: "module" } }
1853 ),
1854 invalid(
1855 "export default (a = b)",
1856 "export default a = b",
1857 "AssignmentExpression",
1858 1,
1859 { parserOptions: { sourceType: "module" } }
1860 ),
1861 invalid(
1862 "export default (a ? b : c)",
1863 "export default a ? b : c",
1864 "ConditionalExpression",
1865 1,
1866 { parserOptions: { sourceType: "module" } }
1867 ),
1868 invalid(
1869 "export default (a)",
1870 "export default a",
1871 "Identifier",
1872 1,
1873 { parserOptions: { sourceType: "module" } }
1874 ),
1875 invalid(
1876 "for (foo of(bar));",
1877 "for (foo of bar);",
1878 "Identifier",
1879 1
1880 ),
1881 invalid(
1882 "for ((foo) of bar);",
1883 "for (foo of bar);",
1884 "Identifier",
1885 1
1886 ),
1887 invalid(
1888 "for (foo of (baz = bar));",
1889 "for (foo of baz = bar);",
1890 "AssignmentExpression",
1891 1
1892 ),
1893 invalid(
1894 "function* f() { for (foo of (yield bar)); }",
1895 "function* f() { for (foo of yield bar); }",
1896 "YieldExpression",
1897 1
1898 ),
1899 invalid(
1900 "for (foo of ((bar, baz)));",
1901 "for (foo of (bar, baz));",
1902 "SequenceExpression",
1903 1
1904 ),
1905 invalid(
1906 "for ((foo)in bar);",
1907 "for (foo in bar);",
1908 "Identifier",
1909 1
1910 ),
1911 invalid(
1912 "for ((foo['bar'])of baz);",
1913 "for (foo['bar']of baz);",
1914 "MemberExpression",
1915 1
1916 ),
1917 invalid(
1918 "() => (({ foo: 1 }).foo)",
1919 "() => ({ foo: 1 }).foo",
1920 "MemberExpression",
1921 1
1922 ),
1923 invalid(
1924 "(let).foo",
1925 "let.foo",
1926 "Identifier",
1927 1
1928 ),
1929
1930 // ForStatement#init expression cannot start with `let[`, but it can start with `let` if it isn't followed by `[`
1931 invalid(
1932 "for ((let);;);",
1933 "for (let;;);",
1934 "Identifier",
1935 1
1936 ),
1937 invalid(
1938 "for ((let = 1);;);",
1939 "for (let = 1;;);",
1940 "AssignmentExpression",
1941 1
1942 ),
1943 invalid(
1944 "for ((let) = 1;;);",
1945 "for (let = 1;;);",
1946 "Identifier",
1947 1
1948 ),
1949 invalid(
1950 "for ((let = []);;);",
1951 "for (let = [];;);",
1952 "AssignmentExpression",
1953 1
1954 ),
1955 invalid(
1956 "for ((let) = [];;);",
1957 "for (let = [];;);",
1958 "Identifier",
1959 1
1960 ),
1961 invalid(
1962 "for ((let());;);",
1963 "for (let();;);",
1964 "CallExpression",
1965 1
1966 ),
1967 invalid(
1968 "for ((let([]));;);",
1969 "for (let([]);;);",
1970 "CallExpression",
1971 1
1972 ),
1973 invalid(
1974 "for ((let())[a];;);",
1975 "for (let()[a];;);",
1976 "CallExpression",
1977 1
1978 ),
1979 invalid(
1980 "for ((let`[]`);;);",
1981 "for (let`[]`;;);",
1982 "TaggedTemplateExpression",
1983 1
1984 ),
1985 invalid(
1986 "for ((let.a);;);",
1987 "for (let.a;;);",
1988 "MemberExpression",
1989 1
1990 ),
1991 invalid(
1992 "for ((let).a;;);",
1993 "for (let.a;;);",
1994 "Identifier",
1995 1
1996 ),
1997 invalid(
1998 "for ((let).a = 1;;);",
1999 "for (let.a = 1;;);",
2000 "Identifier",
2001 1
2002 ),
2003 invalid(
2004 "for ((let).a[b];;);",
2005 "for (let.a[b];;);",
2006 "Identifier",
2007 1
2008 ),
2009 invalid(
2010 "for ((let.a)[b];;);",
2011 "for (let.a[b];;);",
2012 "MemberExpression",
2013 1
2014 ),
2015 invalid(
2016 "for ((let.a[b]);;);",
2017 "for (let.a[b];;);",
2018 "MemberExpression",
2019 1
2020 ),
2021 invalid(
2022 "for ((let);[];);",
2023 "for (let;[];);",
2024 "Identifier",
2025 1
2026 ),
2027 invalid(
2028 "for (((let[a]));;);",
2029 "for ((let[a]);;);",
2030 "MemberExpression",
2031 1
2032 ),
2033 invalid(
2034 "for (((let))[a];;);",
2035 "for ((let)[a];;);",
2036 "Identifier",
2037 1
2038 ),
2039 invalid(
2040 "for (((let[a])).b;;);",
2041 "for ((let[a]).b;;);",
2042 "MemberExpression",
2043 1
2044 ),
2045 invalid(
2046 "for (((let))[a].b;;);",
2047 "for ((let)[a].b;;);",
2048 "Identifier",
2049 1
2050 ),
2051 invalid(
2052 "for (((let)[a]).b;;);",
2053 "for ((let)[a].b;;);",
2054 "MemberExpression",
2055 1
2056 ),
2057 invalid(
2058 "for (((let[a]) = b);;);",
2059 "for ((let[a]) = b;;);",
2060 "AssignmentExpression",
2061 1
2062 ),
2063 invalid(
2064 "for (((let)[a]) = b;;);",
2065 "for ((let)[a] = b;;);",
2066 "MemberExpression",
2067 1
2068 ),
2069 invalid(
2070 "for (((let)[a] = b);;);",
2071 "for ((let)[a] = b;;);",
2072 "AssignmentExpression",
2073 1
2074 ),
2075 invalid(
2076 "for ((Let[a]);;);",
2077 "for (Let[a];;);",
2078 "MemberExpression",
2079 1
2080 ),
2081 invalid(
2082 "for ((lett)[a];;);",
2083 "for (lett[a];;);",
2084 "Identifier",
2085 1
2086 ),
2087
2088 // ForInStatement#left expression cannot start with `let[`, but it can start with `let` if it isn't followed by `[`
2089 invalid(
2090 "for ((let) in foo);",
2091 "for (let in foo);",
2092 "Identifier",
2093 1
2094 ),
2095 invalid(
2096 "for ((let())[a] in foo);",
2097 "for (let()[a] in foo);",
2098 "CallExpression",
2099 1
2100 ),
2101 invalid(
2102 "for ((let.a) in foo);",
2103 "for (let.a in foo);",
2104 "MemberExpression",
2105 1
2106 ),
2107 invalid(
2108 "for ((let).a in foo);",
2109 "for (let.a in foo);",
2110 "Identifier",
2111 1
2112 ),
2113 invalid(
2114 "for ((let).a.b in foo);",
2115 "for (let.a.b in foo);",
2116 "Identifier",
2117 1
2118 ),
2119 invalid(
2120 "for ((let).a[b] in foo);",
2121 "for (let.a[b] in foo);",
2122 "Identifier",
2123 1
2124 ),
2125 invalid(
2126 "for ((let.a)[b] in foo);",
2127 "for (let.a[b] in foo);",
2128 "MemberExpression",
2129 1
2130 ),
2131 invalid(
2132 "for ((let.a[b]) in foo);",
2133 "for (let.a[b] in foo);",
2134 "MemberExpression",
2135 1
2136 ),
2137 invalid(
2138 "for (((let[a])) in foo);",
2139 "for ((let[a]) in foo);",
2140 "MemberExpression",
2141 1
2142 ),
2143 invalid(
2144 "for (((let))[a] in foo);",
2145 "for ((let)[a] in foo);",
2146 "Identifier",
2147 1
2148 ),
2149 invalid(
2150 "for (((let[a])).b in foo);",
2151 "for ((let[a]).b in foo);",
2152 "MemberExpression",
2153 1
2154 ),
2155 invalid(
2156 "for (((let))[a].b in foo);",
2157 "for ((let)[a].b in foo);",
2158 "Identifier",
2159 1
2160 ),
2161 invalid(
2162 "for (((let)[a]).b in foo);",
2163 "for ((let)[a].b in foo);",
2164 "MemberExpression",
2165 1
2166 ),
2167 invalid(
2168 "for (((let[a]).b) in foo);",
2169 "for ((let[a]).b in foo);",
2170 "MemberExpression",
2171 1
2172 ),
2173 invalid(
2174 "for ((Let[a]) in foo);",
2175 "for (Let[a] in foo);",
2176 "MemberExpression",
2177 1
2178 ),
2179 invalid(
2180 "for ((lett)[a] in foo);",
2181 "for (lett[a] in foo);",
2182 "Identifier",
2183 1
2184 ),
2185
2186 // ForOfStatement#left expression cannot start with `let`
2187 invalid(
2188 "for (((let)) of foo);",
2189 "for ((let) of foo);",
2190 "Identifier",
2191 1
2192 ),
2193 invalid(
2194 "for (((let)).a of foo);",
2195 "for ((let).a of foo);",
2196 "Identifier",
2197 1
2198 ),
2199 invalid(
2200 "for (((let))[a] of foo);",
2201 "for ((let)[a] of foo);",
2202 "Identifier",
2203 1
2204 ),
2205 invalid(
2206 "for (((let).a) of foo);",
2207 "for ((let).a of foo);",
2208 "MemberExpression",
2209 1
2210 ),
2211 invalid(
2212 "for (((let[a]).b) of foo);",
2213 "for ((let[a]).b of foo);",
2214 "MemberExpression",
2215 1
2216 ),
2217 invalid(
2218 "for (((let).a).b of foo);",
2219 "for ((let).a.b of foo);",
2220 "MemberExpression",
2221 1
2222 ),
2223 invalid(
2224 "for (((let).a.b) of foo);",
2225 "for ((let).a.b of foo);",
2226 "MemberExpression",
2227 1
2228 ),
2229 invalid(
2230 "for (((let.a).b) of foo);",
2231 "for ((let.a).b of foo);",
2232 "MemberExpression",
2233 1
2234 ),
2235 invalid(
2236 "for (((let()).a) of foo);",
2237 "for ((let()).a of foo);",
2238 "MemberExpression",
2239 1
2240 ),
2241 invalid(
2242 "for ((Let) of foo);",
2243 "for (Let of foo);",
2244 "Identifier",
2245 1
2246 ),
2247 invalid(
2248 "for ((lett) of foo);",
2249 "for (lett of foo);",
2250 "Identifier",
2251 1
2252 ),
2253
2254 invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null),
2255 invalid(
2256 "(let)",
2257 "let",
2258 "Identifier",
2259 1
2260 ),
2261 invalid(
2262 "((let))",
2263 "(let)",
2264 "Identifier",
2265 1
2266 ),
2267 invalid("let s = `${(v)}`", "let s = `${v}`", "Identifier"),
2268 invalid("let s = `${(a, b)}`", "let s = `${a, b}`", "SequenceExpression"),
2269 invalid("function foo(a = (b)) {}", "function foo(a = b) {}", "Identifier"),
2270 invalid("const bar = (a = (b)) => a", "const bar = (a = b) => a", "Identifier"),
2271 invalid("const [a = (b)] = []", "const [a = b] = []", "Identifier"),
2272 invalid("const {a = (b)} = {}", "const {a = b} = {}", "Identifier"),
2273
2274 // LHS of assignments/Assignment targets
2275 invalid("(a) = b", "a = b", "Identifier"),
2276 invalid("(a.b) = c", "a.b = c", "MemberExpression"),
2277 invalid("(a) += b", "a += b", "Identifier"),
2278 invalid("(a.b) >>= c", "a.b >>= c", "MemberExpression"),
2279 invalid("[(a) = b] = []", "[a = b] = []", "Identifier"),
2280 invalid("[(a.b) = c] = []", "[a.b = c] = []", "MemberExpression"),
2281 invalid("({ a: (b) = c } = {})", "({ a: b = c } = {})", "Identifier"),
2282 invalid("({ a: (b.c) = d } = {})", "({ a: b.c = d } = {})", "MemberExpression"),
2283 invalid("[(a)] = []", "[a] = []", "Identifier"),
2284 invalid("[(a.b)] = []", "[a.b] = []", "MemberExpression"),
2285 invalid("[,(a),,] = []", "[,a,,] = []", "Identifier"),
2286 invalid("[...(a)] = []", "[...a] = []", "Identifier"),
2287 invalid("[...(a.b)] = []", "[...a.b] = []", "MemberExpression"),
2288 invalid("({ a: (b) } = {})", "({ a: b } = {})", "Identifier"),
2289 invalid("({ a: (b.c) } = {})", "({ a: b.c } = {})", "MemberExpression"),
2290
2291 /*
2292 * TODO: Add these tests for RestElement's parenthesized arguments in object patterns when that becomes supported by Espree.
2293 *
2294 * invalid("({ ...(a) } = {})", "({ ...a } = {})", "Identifier"),
2295 * invalid("({ ...(a.b) } = {})", "({ ...a.b } = {})", "MemberExpression")
2296 */
2297
2298 // https://github.com/eslint/eslint/issues/11706 (also in valid[])
2299 {
2300 code: "for ((a = (b in c)); ;);",
2301 output: "for ((a = b in c); ;);",
2302 errors: [
2303 {
2304 messageId: "unexpected"
2305 }
2306 ]
2307 },
2308 {
2309 code: "for (let a = ((b in c) && (d in e)); ;);",
2310 output: "for (let a = (b in c && d in e); ;);",
2311 errors: Array(2).fill(
2312 {
2313 messageId: "unexpected"
2314 }
2315 )
2316 },
2317 {
2318 code: "for (let a = ((b in c) in d); ;);",
2319 output: "for (let a = (b in c in d); ;);",
2320 errors: [
2321 {
2322 messageId: "unexpected"
2323 }
2324 ]
2325 },
2326 {
2327 code: "for (let a = (b && (c in d)), e = (f in g); ;);",
2328 output: "for (let a = (b && c in d), e = (f in g); ;);",
2329 errors: [
2330 {
2331 messageId: "unexpected"
2332 }
2333 ]
2334 },
2335 {
2336 code: "for (let a = (b + c), d = (e in f); ;);",
2337 output: "for (let a = b + c, d = (e in f); ;);",
2338 errors: [
2339 {
2340 messageId: "unexpected"
2341 }
2342 ]
2343 },
2344 {
2345 code: "for (let a = [(b in c)]; ;);",
2346 output: "for (let a = [b in c]; ;);",
2347 errors: [
2348 {
2349 messageId: "unexpected"
2350 }
2351 ]
2352 },
2353 {
2354 code: "for (let a = [b, (c in d)]; ;);",
2355 output: "for (let a = [b, c in d]; ;);",
2356 errors: [
2357 {
2358 messageId: "unexpected"
2359 }
2360 ]
2361 },
2362 {
2363 code: "for (let a = ([b in c]); ;);",
2364 output: "for (let a = [b in c]; ;);",
2365 errors: [
2366 {
2367 messageId: "unexpected"
2368 }
2369 ]
2370 },
2371 {
2372 code: "for (let a = ([b, c in d]); ;);",
2373 output: "for (let a = [b, c in d]; ;);",
2374 errors: [
2375 {
2376 messageId: "unexpected"
2377 }
2378 ]
2379 },
2380 {
2381 code: "for ((a = [b in c]); ;);",
2382 output: "for (a = [b in c]; ;);",
2383 errors: [
2384 {
2385 messageId: "unexpected"
2386 }
2387 ]
2388 },
2389 {
2390 code: "for (let a = [b && (c in d)]; ;);",
2391 output: "for (let a = [b && c in d]; ;);",
2392 errors: [
2393 {
2394 messageId: "unexpected"
2395 }
2396 ]
2397 },
2398 {
2399 code: "for (let a = [(b && c in d)]; ;);",
2400 output: "for (let a = [b && c in d]; ;);",
2401 errors: [
2402 {
2403 messageId: "unexpected"
2404 }
2405 ]
2406 },
2407 {
2408 code: "for (let a = ([b && c in d]); ;);",
2409 output: "for (let a = [b && c in d]; ;);",
2410 errors: [
2411 {
2412 messageId: "unexpected"
2413 }
2414 ]
2415 },
2416 {
2417 code: "for ((a = [b && c in d]); ;);",
2418 output: "for (a = [b && c in d]; ;);",
2419 errors: [
2420 {
2421 messageId: "unexpected"
2422 }
2423 ]
2424 },
2425 {
2426 code: "for ([(a in b)]; ;);",
2427 output: "for ([a in b]; ;);",
2428 errors: [
2429 {
2430 messageId: "unexpected"
2431 }
2432 ]
2433 },
2434 {
2435 code: "for (([a in b]); ;);",
2436 output: "for ([a in b]; ;);",
2437 errors: [
2438 {
2439 messageId: "unexpected"
2440 }
2441 ]
2442 },
2443 {
2444 code: "for (let a = [(b in c)], d = (e in f); ;);",
2445 output: "for (let a = [b in c], d = (e in f); ;);",
2446 errors: [
2447 {
2448 messageId: "unexpected"
2449 }
2450 ]
2451 },
2452 {
2453 code: "for (let [a = (b in c)] = []; ;);",
2454 output: "for (let [a = b in c] = []; ;);",
2455 errors: [
2456 {
2457 messageId: "unexpected"
2458 }
2459 ]
2460 },
2461 {
2462 code: "for (let [a = b && (c in d)] = []; ;);",
2463 output: "for (let [a = b && c in d] = []; ;);",
2464 errors: [
2465 {
2466 messageId: "unexpected"
2467 }
2468 ]
2469 },
2470 {
2471 code: "for (let a = () => { (b in c) }; ;);",
2472 output: "for (let a = () => { b in c }; ;);",
2473 errors: [
2474 {
2475 messageId: "unexpected"
2476 }
2477 ]
2478 },
2479 {
2480 code: "for (let a = () => { a && (b in c) }; ;);",
2481 output: "for (let a = () => { a && b in c }; ;);",
2482 errors: [
2483 {
2484 messageId: "unexpected"
2485 }
2486 ]
2487 },
2488 {
2489 code: "for (let a = function () { (b in c) }; ;);",
2490 output: "for (let a = function () { b in c }; ;);",
2491 errors: [
2492 {
2493 messageId: "unexpected"
2494 }
2495 ]
2496 },
2497 {
2498 code: "for (let a = { a: (b in c) }; ;);",
2499 output: "for (let a = { a: b in c }; ;);",
2500 errors: [
2501 {
2502 messageId: "unexpected"
2503 }
2504 ]
2505 },
2506 {
2507 code: "for (let a = { a: b && (c in d) }; ;);",
2508 output: "for (let a = { a: b && c in d }; ;);",
2509 errors: [
2510 {
2511 messageId: "unexpected"
2512 }
2513 ]
2514 },
2515 {
2516 code: "for (let { a = (b in c) } = {}; ;);",
2517 output: "for (let { a = b in c } = {}; ;);",
2518 errors: [
2519 {
2520 messageId: "unexpected"
2521 }
2522 ]
2523 },
2524 {
2525 code: "for (let { a = b && (c in d) } = {}; ;);",
2526 output: "for (let { a = b && c in d } = {}; ;);",
2527 errors: [
2528 {
2529 messageId: "unexpected"
2530 }
2531 ]
2532 },
2533 {
2534 code: "for (let { a: { b = c && (d in e) } } = {}; ;);",
2535 output: "for (let { a: { b = c && d in e } } = {}; ;);",
2536 errors: [
2537 {
2538 messageId: "unexpected"
2539 }
2540 ]
2541 },
2542 {
2543 code: "for (let a = `${(a in b)}`; ;);",
2544 output: "for (let a = `${a in b}`; ;);",
2545 errors: [
2546 {
2547 messageId: "unexpected"
2548 }
2549 ]
2550 },
2551 {
2552 code: "for (let a = `${a && (b in c)}`; ;);",
2553 output: "for (let a = `${a && b in c}`; ;);",
2554 errors: [
2555 {
2556 messageId: "unexpected"
2557 }
2558 ]
2559 },
2560 {
2561 code: "for (let a = (b = (c in d)) => {}; ;);",
2562 output: "for (let a = (b = c in d) => {}; ;);",
2563 errors: [
2564 {
2565 messageId: "unexpected"
2566 }
2567 ]
2568 },
2569 {
2570 code: "for (let a = (b = c && (d in e)) => {}; ;);",
2571 output: "for (let a = (b = c && d in e) => {}; ;);",
2572 errors: [
2573 {
2574 messageId: "unexpected"
2575 }
2576 ]
2577 },
2578 {
2579 code: "for (let a = (b, c = d && (e in f)) => {}; ;);",
2580 output: "for (let a = (b, c = d && e in f) => {}; ;);",
2581 errors: [
2582 {
2583 messageId: "unexpected"
2584 }
2585 ]
2586 },
2587 {
2588 code: "for (let a = function (b = c && (d in e)) {}; ;);",
2589 output: "for (let a = function (b = c && d in e) {}; ;);",
2590 errors: [
2591 {
2592 messageId: "unexpected"
2593 }
2594 ]
2595 },
2596 {
2597 code: "for (let a = function (b, c = d && (e in f)) {}; ;);",
2598 output: "for (let a = function (b, c = d && e in f) {}; ;);",
2599 errors: [
2600 {
2601 messageId: "unexpected"
2602 }
2603 ]
2604 },
2605 {
2606 code: "for (let a = b((c in d)); ;);",
2607 output: "for (let a = b(c in d); ;);",
2608 errors: [
2609 {
2610 messageId: "unexpected"
2611 }
2612 ]
2613 },
2614 {
2615 code: "for (let a = b(c, (d in e)); ;);",
2616 output: "for (let a = b(c, d in e); ;);",
2617 errors: [
2618 {
2619 messageId: "unexpected"
2620 }
2621 ]
2622 },
2623 {
2624 code: "for (let a = b(c && (d in e)); ;);",
2625 output: "for (let a = b(c && d in e); ;);",
2626 errors: [
2627 {
2628 messageId: "unexpected"
2629 }
2630 ]
2631 },
2632 {
2633 code: "for (let a = b(c, d && (e in f)); ;);",
2634 output: "for (let a = b(c, d && e in f); ;);",
2635 errors: [
2636 {
2637 messageId: "unexpected"
2638 }
2639 ]
2640 },
2641 {
2642 code: "for (let a = new b((c in d)); ;);",
2643 output: "for (let a = new b(c in d); ;);",
2644 errors: [
2645 {
2646 messageId: "unexpected"
2647 }
2648 ]
2649 },
2650 {
2651 code: "for (let a = new b(c, (d in e)); ;);",
2652 output: "for (let a = new b(c, d in e); ;);",
2653 errors: [
2654 {
2655 messageId: "unexpected"
2656 }
2657 ]
2658 },
2659 {
2660 code: "for (let a = new b(c && (d in e)); ;);",
2661 output: "for (let a = new b(c && d in e); ;);",
2662 errors: [
2663 {
2664 messageId: "unexpected"
2665 }
2666 ]
2667 },
2668 {
2669 code: "for (let a = new b(c, d && (e in f)); ;);",
2670 output: "for (let a = new b(c, d && e in f); ;);",
2671 errors: [
2672 {
2673 messageId: "unexpected"
2674 }
2675 ]
2676 },
2677 {
2678 code: "for (let a = b[(c in d)]; ;);",
2679 output: "for (let a = b[c in d]; ;);",
2680 errors: [
2681 {
2682 messageId: "unexpected"
2683 }
2684 ]
2685 },
2686 {
2687 code: "for (let a = b[c && (d in e)]; ;);",
2688 output: "for (let a = b[c && d in e]; ;);",
2689 errors: [
2690 {
2691 messageId: "unexpected"
2692 }
2693 ]
2694 },
2695 {
2696 code: "for (let a = b ? (c in d) : e; ;);",
2697 output: "for (let a = b ? c in d : e; ;);",
2698 errors: [
2699 {
2700 messageId: "unexpected"
2701 }
2702 ]
2703 },
2704 {
2705 code: "for (let a = b ? c && (d in e) : f; ;);",
2706 output: "for (let a = b ? c && d in e : f; ;);",
2707 errors: [
2708 {
2709 messageId: "unexpected"
2710 }
2711 ]
2712 },
2713 {
2714 code: "for (a ? b && (c in d) : e; ;);",
2715 output: "for (a ? b && c in d : e; ;);",
2716 errors: [
2717 {
2718 messageId: "unexpected"
2719 }
2720 ]
2721 },
2722 {
2723 code: "for (let a = ((b in c)); ;);",
2724 output: "for (let a = (b in c); ;);",
2725 errors: [
2726 {
2727 messageId: "unexpected"
2728 }
2729 ]
2730 },
2731 {
2732 code: "for (((a in b)); ;);",
2733 output: "for ((a in b); ;);",
2734 errors: [
2735 {
2736 messageId: "unexpected"
2737 }
2738 ]
2739 },
2740 {
2741 code: "for (((a && b in c && d)); ;);",
2742 output: "for ((a && b in c && d); ;);",
2743 errors: [
2744 {
2745 messageId: "unexpected"
2746 }
2747 ]
2748 },
2749 {
2750 code: "for (let a = (!(b in c)); ;);",
2751 output: "for (let a = !(b in c); ;);",
2752 errors: [
2753 {
2754 messageId: "unexpected"
2755 }
2756 ]
2757 },
2758 {
2759 code: "for (let a = (!(b && c in d)); ;);",
2760 output: "for (let a = !(b && c in d); ;);",
2761 errors: [
2762 {
2763 messageId: "unexpected"
2764 }
2765 ]
2766 },
2767 {
2768 code: "for (let a = !((b in c) && (d in e)); ;);",
2769 output: "for (let a = !(b in c && d in e); ;);",
2770 errors: Array(2).fill(
2771 {
2772 messageId: "unexpected"
2773 }
2774 )
2775 },
2776 {
2777 code: "for (let a = (x && (b in c)), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2778 output: "for (let a = (x && b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2779 errors: [
2780 {
2781 messageId: "unexpected"
2782 }
2783 ]
2784 },
2785 {
2786 code: "for (let a = (b in c), d = () => { for ((x && (e in f)); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2787 output: "for (let a = (b in c), d = () => { for ((x && e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2788 errors: [
2789 {
2790 messageId: "unexpected"
2791 }
2792 ]
2793 },
2794 {
2795 code: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((x && (g in h)); ;); }; ;); for((i in j); ;);",
2796 output: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((x && g in h); ;); }; ;); for((i in j); ;);",
2797 errors: [
2798 {
2799 messageId: "unexpected"
2800 }
2801 ]
2802 },
2803 {
2804 code: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((x && (i in j)); ;);",
2805 output: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((x && i in j); ;);",
2806 errors: [
2807 {
2808 messageId: "unexpected"
2809 }
2810 ]
2811 },
2812 {
2813 code: "for (let a = (x && (b in c)), d = () => { for ((e in f); ;); for ((y && (g in h)); ;); }; ;); for((i in j); ;);",
2814 output: "for (let a = (x && b in c), d = () => { for ((e in f); ;); for ((y && g in h); ;); }; ;); for((i in j); ;);",
2815 errors: Array(2).fill(
2816 {
2817 messageId: "unexpected"
2818 }
2819 )
2820 },
2821 {
2822 code: "for (let a = (x && (b in c)), d = () => { for ((y && (e in f)); ;); for ((z && (g in h)); ;); }; ;); for((w && (i in j)); ;);",
2823 output: "for (let a = (x && b in c), d = () => { for ((y && e in f); ;); for ((z && g in h); ;); }; ;); for((w && i in j); ;);",
2824 errors: Array(4).fill(
2825 {
2826 messageId: "unexpected"
2827 }
2828 )
2829 },
2830
2831 // https://github.com/eslint/eslint/issues/11706 regression tests (also in valid[])
2832 {
2833 code: "for (let a = (b); a > (b); a = (b)) a = (b); a = (b);",
2834 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2835 errors: Array(5).fill(
2836 {
2837 messageId: "unexpected"
2838 }
2839 )
2840 },
2841 {
2842 code: "for ((a = b); (a > b); (a = b)) (a = b); (a = b);",
2843 output: "for (a = b; a > b; a = b) a = b; a = b;",
2844 errors: Array(5).fill(
2845 {
2846 messageId: "unexpected"
2847 }
2848 )
2849 },
2850 {
2851 code: "for (let a = b; a > (b); a = (b)) a = (b); a = (b);",
2852 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2853 errors: Array(4).fill(
2854 {
2855 messageId: "unexpected"
2856 }
2857 )
2858 },
2859 {
2860 code: "for (let a = b; (a > b); (a = b)) (a = b); (a = b);",
2861 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2862 errors: Array(4).fill(
2863 {
2864 messageId: "unexpected"
2865 }
2866 )
2867 },
2868 {
2869 code: "for (; a > (b); a = (b)) a = (b); a = (b);",
2870 output: "for (; a > b; a = b) a = b; a = b;",
2871 errors: Array(4).fill(
2872 {
2873 messageId: "unexpected"
2874 }
2875 )
2876 },
2877 {
2878 code: "for (; (a > b); (a = b)) (a = b); (a = b);",
2879 output: "for (; a > b; a = b) a = b; a = b;",
2880 errors: Array(4).fill(
2881 {
2882 messageId: "unexpected"
2883 }
2884 )
2885 },
2886 {
2887 code: "for (let a = (b); a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2888 output: "for (let a = b; a = b in c; a = b in c) a = b in c; a = b in c;",
2889 errors: Array(5).fill(
2890 {
2891 messageId: "unexpected"
2892 }
2893 )
2894 },
2895 {
2896 code: "for (let a = (b); (a in b); (a in b)) (a in b); (a in b);",
2897 output: "for (let a = b; a in b; a in b) a in b; a in b;",
2898 errors: Array(5).fill(
2899 {
2900 messageId: "unexpected"
2901 }
2902 )
2903 },
2904 {
2905 code: "for (let a = b; a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2906 output: "for (let a = b; a = b in c; a = b in c) a = b in c; a = b in c;",
2907 errors: Array(4).fill(
2908 {
2909 messageId: "unexpected"
2910 }
2911 )
2912 },
2913 {
2914 code: "for (let a = b; (a in b); (a in b)) (a in b); (a in b);",
2915 output: "for (let a = b; a in b; a in b) a in b; a in b;",
2916 errors: Array(4).fill(
2917 {
2918 messageId: "unexpected"
2919 }
2920 )
2921 },
2922 {
2923 code: "for (; a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2924 output: "for (; a = b in c; a = b in c) a = b in c; a = b in c;",
2925 errors: Array(4).fill(
2926 {
2927 messageId: "unexpected"
2928 }
2929 )
2930 },
2931 {
2932 code: "for (; (a in b); (a in b)) (a in b); (a in b);",
2933 output: "for (; a in b; a in b) a in b; a in b;",
2934 errors: Array(4).fill(
2935 {
2936 messageId: "unexpected"
2937 }
2938 )
2939 },
2940 {
2941 code: "for (let a = (b + c), d = () => { for ((e + f); ;); for ((g + h); ;); }; ;); for((i + j); ;);",
2942 output: "for (let a = b + c, d = () => { for (e + f; ;); for (g + h; ;); }; ;); for(i + j; ;);",
2943 errors: Array(4).fill(
2944 {
2945 messageId: "unexpected"
2946 }
2947 )
2948 },
2949
2950 // import expressions
2951 invalid(
2952 "import((source))",
2953 "import(source)",
2954 "Identifier",
2955 1,
2956 { parserOptions: { ecmaVersion: 2020 } }
2957 ),
2958 invalid(
2959 "import((source = 'foo.js'))",
2960 "import(source = 'foo.js')",
2961 "AssignmentExpression",
2962 1,
2963 { parserOptions: { ecmaVersion: 2020 } }
2964 ),
2965 invalid(
2966 "import(((s,t)))",
2967 "import((s,t))",
2968 "SequenceExpression",
2969 1,
2970 { parserOptions: { ecmaVersion: 2020 } }
2971 ),
2972
2973 // https://github.com/eslint/eslint/issues/12127
2974 {
2975 code: "[1, ((2, 3))];",
2976 output: "[1, (2, 3)];",
2977 errors: [{ messageId: "unexpected" }]
2978 },
2979 {
2980 code: "const foo = () => ((bar, baz));",
2981 output: "const foo = () => (bar, baz);",
2982 errors: [{ messageId: "unexpected" }]
2983 },
2984 {
2985 code: "foo = ((bar, baz));",
2986 output: "foo = (bar, baz);",
2987 errors: [{ messageId: "unexpected" }]
2988 },
2989 {
2990 code: "foo + ((bar + baz));",
2991 output: "foo + (bar + baz);",
2992 errors: [{ messageId: "unexpected" }]
2993 },
2994 {
2995 code: "((foo + bar)) + baz;",
2996 output: "(foo + bar) + baz;",
2997 errors: [{ messageId: "unexpected" }]
2998 },
2999 {
3000 code: "foo * ((bar + baz));",
3001 output: "foo * (bar + baz);",
3002 errors: [{ messageId: "unexpected" }]
3003 },
3004 {
3005 code: "((foo + bar)) * baz;",
3006 output: "(foo + bar) * baz;",
3007 errors: [{ messageId: "unexpected" }]
3008 },
3009 {
3010 code: "new A(((foo, bar)))",
3011 output: "new A((foo, bar))",
3012 errors: [{ messageId: "unexpected" }]
3013 },
3014 {
3015 code: "class A{ [((foo, bar))]() {} }",
3016 output: "class A{ [(foo, bar)]() {} }",
3017 errors: [{ messageId: "unexpected" }]
3018 },
3019 {
3020 code: "new ((A, B))()",
3021 output: "new (A, B)()",
3022 errors: [{ messageId: "unexpected" }]
3023 },
3024 {
3025 code: "((foo, bar)) ? bar : baz;",
3026 output: "(foo, bar) ? bar : baz;",
3027 errors: [{ messageId: "unexpected" }]
3028 },
3029 {
3030 code: "((f ? o : o)) ? bar : baz;",
3031 output: "(f ? o : o) ? bar : baz;",
3032 errors: [{ messageId: "unexpected" }]
3033 },
3034 {
3035 code: "((f = oo)) ? bar : baz;",
3036 output: "(f = oo) ? bar : baz;",
3037 errors: [{ messageId: "unexpected" }]
3038 },
3039 {
3040 code: "foo ? ((bar, baz)) : baz;",
3041 output: "foo ? (bar, baz) : baz;",
3042 errors: [{ messageId: "unexpected" }]
3043 },
3044 {
3045 code: "foo ? bar : ((bar, baz));",
3046 output: "foo ? bar : (bar, baz);",
3047 errors: [{ messageId: "unexpected" }]
3048 },
3049 {
3050 code: "function foo(bar = ((baz1, baz2))) {}",
3051 output: "function foo(bar = (baz1, baz2)) {}",
3052 errors: [{ messageId: "unexpected" }]
3053 },
3054 {
3055 code: "var foo = { bar: ((baz1, baz2)) };",
3056 output: "var foo = { bar: (baz1, baz2) };",
3057 errors: [{ messageId: "unexpected" }]
3058 },
3059 {
3060 code: "var foo = { [((bar1, bar2))]: baz };",
3061 output: "var foo = { [(bar1, bar2)]: baz };",
3062 errors: [{ messageId: "unexpected" }]
3063 },
3064
3065 // adjacent tokens tests for division operator, comments and regular expressions
3066 invalid("a+/**/(/**/b)", "a+/**//**/b", "Identifier"),
3067 invalid("a+/**/(//\nb)", "a+/**///\nb", "Identifier"),
3068 invalid("a in(/**/b)", "a in/**/b", "Identifier"),
3069 invalid("a in(//\nb)", "a in//\nb", "Identifier"),
3070 invalid("a+(/**/b)", "a+/**/b", "Identifier"),
3071 invalid("a+/**/(b)", "a+/**/b", "Identifier"),
3072 invalid("a+(//\nb)", "a+//\nb", "Identifier"),
3073 invalid("a+//\n(b)", "a+//\nb", "Identifier"),
3074 invalid("a+(/^b$/)", "a+/^b$/", "Literal"),
3075 invalid("a/(/**/b)", "a/ /**/b", "Identifier"),
3076 invalid("a/(//\nb)", "a/ //\nb", "Identifier"),
3077 invalid("a/(/^b$/)", "a/ /^b$/", "Literal"),
3078
3079
3080 // Nullish coalescing
3081 {
3082 code: "var v = ((a ?? b)) || c",
3083 output: "var v = (a ?? b) || c",
3084 parserOptions: { ecmaVersion: 2020 },
3085 errors: [{ messageId: "unexpected" }]
3086 },
3087 {
3088 code: "var v = a ?? ((b || c))",
3089 output: "var v = a ?? (b || c)",
3090 parserOptions: { ecmaVersion: 2020 },
3091 errors: [{ messageId: "unexpected" }]
3092 },
3093 {
3094 code: "var v = ((a ?? b)) && c",
3095 output: "var v = (a ?? b) && c",
3096 parserOptions: { ecmaVersion: 2020 },
3097 errors: [{ messageId: "unexpected" }]
3098 },
3099 {
3100 code: "var v = a ?? ((b && c))",
3101 output: "var v = a ?? (b && c)",
3102 parserOptions: { ecmaVersion: 2020 },
3103 errors: [{ messageId: "unexpected" }]
3104 },
3105 {
3106 code: "var v = ((a || b)) ?? c",
3107 output: "var v = (a || b) ?? c",
3108 parserOptions: { ecmaVersion: 2020 },
3109 errors: [{ messageId: "unexpected" }]
3110 },
3111 {
3112 code: "var v = a || ((b ?? c))",
3113 output: "var v = a || (b ?? c)",
3114 parserOptions: { ecmaVersion: 2020 },
3115 errors: [{ messageId: "unexpected" }]
3116 },
3117 {
3118 code: "var v = ((a && b)) ?? c",
3119 output: "var v = (a && b) ?? c",
3120 parserOptions: { ecmaVersion: 2020 },
3121 errors: [{ messageId: "unexpected" }]
3122 },
3123 {
3124 code: "var v = a && ((b ?? c))",
3125 output: "var v = a && (b ?? c)",
3126 parserOptions: { ecmaVersion: 2020 },
3127 errors: [{ messageId: "unexpected" }]
3128 },
3129 {
3130 code: "var v = (a ?? b) ? b : c",
3131 output: "var v = a ?? b ? b : c",
3132 parserOptions: { ecmaVersion: 2020 },
3133 errors: [{ messageId: "unexpected" }]
3134 },
3135 {
3136 code: "var v = (a | b) ?? c | d",
3137 output: "var v = a | b ?? c | d",
3138 parserOptions: { ecmaVersion: 2020 },
3139 errors: [{ messageId: "unexpected" }]
3140 },
3141 {
3142 code: "var v = a | b ?? (c | d)",
3143 output: "var v = a | b ?? c | d",
3144 parserOptions: { ecmaVersion: 2020 },
3145 errors: [{ messageId: "unexpected" }]
3146 },
3147
3148 // Optional chaining
3149 {
3150 code: "var v = (obj?.aaa)?.aaa",
3151 output: "var v = obj?.aaa?.aaa",
3152 parserOptions: { ecmaVersion: 2020 },
3153 errors: [{ messageId: "unexpected" }]
3154 },
3155 {
3156 code: "var v = (obj.aaa)?.aaa",
3157 output: "var v = obj.aaa?.aaa",
3158 parserOptions: { ecmaVersion: 2020 },
3159 errors: [{ messageId: "unexpected" }]
3160 },
3161 {
3162 code: "var foo = (function(){})?.call()",
3163 output: "var foo = function(){}?.call()",
3164 options: ["all", { enforceForFunctionPrototypeMethods: true }],
3165 parserOptions: { ecmaVersion: 2020 },
3166 errors: [{ messageId: "unexpected" }]
3167 },
3168 {
3169 code: "var foo = (function(){}?.call())",
3170 output: "var foo = function(){}?.call()",
3171 options: ["all", { enforceForFunctionPrototypeMethods: true }],
3172 parserOptions: { ecmaVersion: 2020 },
3173 errors: [{ messageId: "unexpected" }]
3174 }
3175 ]
3176 });