]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-extra-parens.js
buildsys: change upload dist to bullseye
[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 "for ((let) in foo);",
619 "for ((let[foo]) in bar);",
620 "for ((let)[foo] in bar);",
621 "for ((let[foo].bar) in baz);",
622
623 // https://github.com/eslint/eslint/issues/11706 (also in invalid[])
624 "for (let a = (b in c); ;);",
625 "for (let a = (b && c in d); ;);",
626 "for (let a = (b in c && d); ;);",
627 "for (let a = (b => b in c); ;);",
628 "for (let a = b => (b in c); ;);",
629 "for (let a = (b in c in d); ;);",
630 "for (let a = (b in c), d = (e in f); ;);",
631 "for (let a = (b => c => b in c); ;);",
632 "for (let a = (b && c && d in e); ;);",
633 "for (let a = b && (c in d); ;);",
634 "for (let a = (b in c) && (d in e); ;);",
635 "for ((a in b); ;);",
636 "for (a = (b in c); ;);",
637 "for ((a in b && c in d && e in f); ;);",
638 "for (let a = [] && (b in c); ;);",
639 "for (let a = (b in [c]); ;);",
640 "for (let a = b => (c in d); ;);",
641 "for (let a = (b in c) ? d : e; ;);",
642 "for (let a = (b in c ? d : e); ;);",
643 "for (let a = b ? c : (d in e); ;);",
644 "for (let a = (b in c), d = () => { for ((e in f);;); for ((g in h);;); }; ;); for((i in j); ;);",
645
646 // https://github.com/eslint/eslint/issues/11706 regression tests (also in invalid[])
647 "for (let a = b; a; a); a; a;",
648 "for (a; a; a); a; a;",
649 "for (; a; a); a; a;",
650 "for (let a = (b && c) === d; ;);",
651
652 "new (a()).b.c;",
653 "new (a().b).c;",
654 "new (a().b.c);",
655 "new (a().b().d);",
656 "new a().b().d;",
657 "new (a(b()).c)",
658 "new (a.b()).c",
659
660 // Nullish coalescing
661 { code: "var v = (a ?? b) || c", parserOptions: { ecmaVersion: 2020 } },
662 { code: "var v = a ?? (b || c)", parserOptions: { ecmaVersion: 2020 } },
663 { code: "var v = (a ?? b) && c", parserOptions: { ecmaVersion: 2020 } },
664 { code: "var v = a ?? (b && c)", parserOptions: { ecmaVersion: 2020 } },
665 { code: "var v = (a || b) ?? c", parserOptions: { ecmaVersion: 2020 } },
666 { code: "var v = a || (b ?? c)", parserOptions: { ecmaVersion: 2020 } },
667 { code: "var v = (a && b) ?? c", parserOptions: { ecmaVersion: 2020 } },
668 { code: "var v = a && (b ?? c)", parserOptions: { ecmaVersion: 2020 } },
669
670 // Optional chaining
671 { code: "var v = (obj?.aaa).bbb", parserOptions: { ecmaVersion: 2020 } },
672 { code: "var v = (obj?.aaa)()", parserOptions: { ecmaVersion: 2020 } },
673 { code: "var v = new (obj?.aaa)()", parserOptions: { ecmaVersion: 2020 } },
674 { code: "var v = new (obj?.aaa)", parserOptions: { ecmaVersion: 2020 } },
675 { code: "var v = (obj?.aaa)`template`", parserOptions: { ecmaVersion: 2020 } },
676 { code: "var v = (obj?.()).bbb", parserOptions: { ecmaVersion: 2020 } },
677 { code: "var v = (obj?.())()", parserOptions: { ecmaVersion: 2020 } },
678 { code: "var v = new (obj?.())()", parserOptions: { ecmaVersion: 2020 } },
679 { code: "var v = new (obj?.())", parserOptions: { ecmaVersion: 2020 } },
680 { code: "var v = (obj?.())`template`", parserOptions: { ecmaVersion: 2020 } },
681 { code: "(obj?.aaa).bbb = 0", parserOptions: { ecmaVersion: 2020 } },
682 { code: "var foo = (function(){})?.()", parserOptions: { ecmaVersion: 2020 } },
683 { code: "var foo = (function(){}?.())", parserOptions: { ecmaVersion: 2020 } },
684 {
685 code: "var foo = (function(){})?.call()",
686 options: ["all", { enforceForFunctionPrototypeMethods: false }],
687 parserOptions: { ecmaVersion: 2020 }
688 },
689 {
690 code: "var foo = (function(){}?.call())",
691 options: ["all", { enforceForFunctionPrototypeMethods: false }],
692 parserOptions: { ecmaVersion: 2020 }
693 }
694 ],
695
696 invalid: [
697 invalid("(0)", "0", "Literal"),
698 invalid("( 0 )", " 0 ", "Literal"),
699 invalid("if((0));", "if(0);", "Literal"),
700 invalid("if(( 0 ));", "if( 0 );", "Literal"),
701 invalid("with((0)){}", "with(0){}", "Literal"),
702 invalid("switch((0)){}", "switch(0){}", "Literal"),
703 invalid("switch(0){ case (1): break; }", "switch(0){ case 1: break; }", "Literal"),
704 invalid("for((0);;);", "for(0;;);", "Literal"),
705 invalid("for(;(0););", "for(;0;);", "Literal"),
706 invalid("for(;;(0));", "for(;;0);", "Literal"),
707 invalid("throw(0)", "throw 0", "Literal"),
708 invalid("while((0));", "while(0);", "Literal"),
709 invalid("do; while((0))", "do; while(0)", "Literal"),
710 invalid("for(a in (0));", "for(a in 0);", "Literal"),
711 invalid("for(a of (0));", "for(a of 0);", "Literal", 1),
712 invalid("const foo = {[(a)]:1}", "const foo = {[a]:1}", "Identifier", 1),
713 invalid("const foo = {[(a=b)]:1}", "const foo = {[a=b]:1}", "AssignmentExpression", 1),
714 invalid("const foo = {*[(Symbol.iterator)]() {}}", "const foo = {*[Symbol.iterator]() {}}", "MemberExpression", 1),
715 invalid("const foo = { get [(a)]() {}}", "const foo = { get [a]() {}}", "Identifier", 1),
716 invalid("const foo = {[(a+b)]:c, d}", "const foo = {[a+b]:c, d}", "BinaryExpression", 1),
717 invalid("const foo = {a, [(b+c)]:d, e}", "const foo = {a, [b+c]:d, e}", "BinaryExpression", 1),
718 invalid("const foo = {[(a+b)]:c, d:e}", "const foo = {[a+b]:c, d:e}", "BinaryExpression", 1),
719 invalid("const foo = {a:b, [(c+d)]:e, f:g}", "const foo = {a:b, [c+d]:e, f:g}", "BinaryExpression", 1),
720 invalid("const foo = {[(a+b)]:c, [d]:e}", "const foo = {[a+b]:c, [d]:e}", "BinaryExpression", 1),
721 invalid("const foo = {[a]:b, [(c+d)]:e, [f]:g}", "const foo = {[a]:b, [c+d]:e, [f]:g}", "BinaryExpression", 1),
722 invalid("const foo = {[(a+b)]:c, [(d,e)]:f}", "const foo = {[a+b]:c, [(d,e)]:f}", "BinaryExpression", 1),
723 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),
724 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),
725 invalid("const foo = {[a+(b*c)]:d}", "const foo = {[a+b*c]:d}", "BinaryExpression", 1),
726 invalid("const foo = {[(a, (b+c))]:d}", "const foo = {[(a, b+c)]:d}", "BinaryExpression", 1),
727 invalid("const {[(a)]:b} = {}", "const {[a]:b} = {}", "Identifier", 1),
728 invalid("const {[(a=b)]:c=1} = {}", "const {[a=b]:c=1} = {}", "AssignmentExpression", 1),
729 invalid("const {[(a+b)]:c, d} = {}", "const {[a+b]:c, d} = {}", "BinaryExpression", 1),
730 invalid("const {a, [(b+c)]:d, e} = {}", "const {a, [b+c]:d, e} = {}", "BinaryExpression", 1),
731 invalid("const {[(a+b)]:c, d:e} = {}", "const {[a+b]:c, d:e} = {}", "BinaryExpression", 1),
732 invalid("const {a:b, [(c+d)]:e, f:g} = {}", "const {a:b, [c+d]:e, f:g} = {}", "BinaryExpression", 1),
733 invalid("const {[(a+b)]:c, [d]:e} = {}", "const {[a+b]:c, [d]:e} = {}", "BinaryExpression", 1),
734 invalid("const {[a]:b, [(c+d)]:e, [f]:g} = {}", "const {[a]:b, [c+d]:e, [f]:g} = {}", "BinaryExpression", 1),
735 invalid("const {[(a+b)]:c, [(d,e)]:f} = {}", "const {[a+b]:c, [(d,e)]:f} = {}", "BinaryExpression", 1),
736 invalid("const {[(a,b)]:c, [(d+e)]:f, [(g,h)]:e} = {}", "const {[(a,b)]:c, [d+e]:f, [(g,h)]:e} = {}", "BinaryExpression", 1),
737 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),
738 invalid("const {[a+(b*c)]:d} = {}", "const {[a+b*c]:d} = {}", "BinaryExpression", 1),
739 invalid("const {[(a, (b+c))]:d} = {}", "const {[(a, b+c)]:d} = {}", "BinaryExpression", 1),
740 invalid("class foo { [(a)](){} }", "class foo { [a](){} }", "Identifier"),
741 invalid("class foo {*[(Symbol.iterator)]() {}}", "class foo {*[Symbol.iterator]() {}}", "MemberExpression"),
742 invalid("class foo { get [(a)](){} }", "class foo { get [a](){} }", "Identifier"),
743 invalid("class foo { set [(a)](bar){} }", "class foo { set [a](bar){} }", "Identifier"),
744 invalid("class foo { static [(a)](bar){} }", "class foo { static [a](bar){} }", "Identifier"),
745 invalid("class foo { [(a=b)](){} }", "class foo { [a=b](){} }", "AssignmentExpression"),
746 invalid("class foo { constructor (){} [(a+b)](){} }", "class foo { constructor (){} [a+b](){} }", "BinaryExpression"),
747 invalid("class foo { [(a+b)](){} constructor (){} }", "class foo { [a+b](){} constructor (){} }", "BinaryExpression"),
748 invalid("class foo { [(a+b)](){} c(){} }", "class foo { [a+b](){} c(){} }", "BinaryExpression"),
749 invalid("class foo { a(){} [(b+c)](){} d(){} }", "class foo { a(){} [b+c](){} d(){} }", "BinaryExpression"),
750 invalid("class foo { [(a+b)](){} [c](){} }", "class foo { [a+b](){} [c](){} }", "BinaryExpression"),
751 invalid("class foo { [a](){} [(b+c)](){} [d](){} }", "class foo { [a](){} [b+c](){} [d](){} }", "BinaryExpression"),
752 invalid("class foo { [(a+b)](){} [(c,d)](){} }", "class foo { [a+b](){} [(c,d)](){} }", "BinaryExpression"),
753 invalid("class foo { [(a,b)](){} [(c+d)](){} }", "class foo { [(a,b)](){} [c+d](){} }", "BinaryExpression"),
754 invalid("class foo { [a+(b*c)](){} }", "class foo { [a+b*c](){} }", "BinaryExpression"),
755 invalid("const foo = class { [(a)](){} }", "const foo = class { [a](){} }", "Identifier"),
756 invalid(
757 "var foo = (function*() { if ((yield foo())) { return; } }())",
758 "var foo = (function*() { if (yield foo()) { return; } }())",
759 "YieldExpression",
760 1
761 ),
762 invalid("f((0))", "f(0)", "Literal"),
763 invalid("f(0, (1))", "f(0, 1)", "Literal"),
764 invalid("!(0)", "!0", "Literal"),
765 invalid("a[(1)]", "a[1]", "Literal"),
766 invalid("(a)(b)", "a(b)", "Identifier"),
767 invalid("(async)", "async", "Identifier"),
768 invalid("(a, b)", "a, b", "SequenceExpression"),
769 invalid("var a = (b = c);", "var a = b = c;", "AssignmentExpression"),
770 invalid("function f(){ return (a); }", "function f(){ return a; }", "Identifier"),
771 invalid("[a, (b = c)]", "[a, b = c]", "AssignmentExpression"),
772 invalid("!{a: (b = c)}", "!{a: b = c}", "AssignmentExpression"),
773 invalid("typeof(0)", "typeof 0", "Literal"),
774 invalid("typeof (0)", "typeof 0", "Literal"),
775 invalid("typeof([])", "typeof[]", "ArrayExpression"),
776 invalid("typeof ([])", "typeof []", "ArrayExpression"),
777 invalid("typeof( 0)", "typeof 0", "Literal"),
778 invalid("typeof(typeof 5)", "typeof typeof 5", "UnaryExpression"),
779 invalid("typeof (typeof 5)", "typeof typeof 5", "UnaryExpression"),
780 invalid("+(+foo)", "+ +foo", "UnaryExpression"),
781 invalid("-(-foo)", "- -foo", "UnaryExpression"),
782 invalid("+(-foo)", "+-foo", "UnaryExpression"),
783 invalid("-(+foo)", "-+foo", "UnaryExpression"),
784 invalid("-((bar+foo))", "-(bar+foo)", "BinaryExpression"),
785 invalid("+((bar-foo))", "+(bar-foo)", "BinaryExpression"),
786 invalid("++(foo)", "++foo", "Identifier"),
787 invalid("--(foo)", "--foo", "Identifier"),
788 invalid("++\n(foo)", "++\nfoo", "Identifier"),
789 invalid("--\n(foo)", "--\nfoo", "Identifier"),
790 invalid("++(\nfoo)", "++\nfoo", "Identifier"),
791 invalid("--(\nfoo)", "--\nfoo", "Identifier"),
792 invalid("(foo)++", "foo++", "Identifier"),
793 invalid("(foo)--", "foo--", "Identifier"),
794 invalid("((foo)\n)++", "(foo\n)++", "Identifier"),
795 invalid("((foo\n))--", "(foo\n)--", "Identifier"),
796 invalid("((foo\n)\n)++", "(foo\n\n)++", "Identifier"),
797 invalid("(a\n.b)--", "a\n.b--", "MemberExpression"),
798 invalid("(a.\nb)++", "a.\nb++", "MemberExpression"),
799 invalid("(a\n[\nb\n])--", "a\n[\nb\n]--", "MemberExpression"),
800 invalid("(a || b) ? c : d", "a || b ? c : d", "LogicalExpression"),
801 invalid("a ? (b = c) : d", "a ? b = c : d", "AssignmentExpression"),
802 invalid("a ? b : (c = d)", "a ? b : c = d", "AssignmentExpression"),
803 invalid("(c = d) ? (b) : c", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
804 invalid("(c = d) ? b : (c)", "(c = d) ? b : c", "Identifier", null, { options: ["all", { conditionalAssign: false }] }),
805 invalid("f((a = b))", "f(a = b)", "AssignmentExpression"),
806 invalid("a, (b = c)", "a, b = c", "AssignmentExpression"),
807 invalid("a = (b * c)", "a = b * c", "BinaryExpression"),
808 invalid("a + (b * c)", "a + b * c", "BinaryExpression"),
809 invalid("(a * b) + c", "a * b + c", "BinaryExpression"),
810 invalid("(a * b) / c", "a * b / c", "BinaryExpression"),
811 invalid("(2) ** 3 ** 4", "2 ** 3 ** 4", "Literal", null),
812 invalid("2 ** (3 ** 4)", "2 ** 3 ** 4", "BinaryExpression", null),
813 invalid("(2 ** 3)", "2 ** 3", "BinaryExpression", null),
814 invalid("(2 ** 3) + 1", "2 ** 3 + 1", "BinaryExpression", null),
815 invalid("1 - (2 ** 3)", "1 - 2 ** 3", "BinaryExpression", null),
816 invalid("-((2 ** 3))", "-(2 ** 3)", "BinaryExpression", null),
817 invalid("typeof ((a ** b));", "typeof (a ** b);", "BinaryExpression", null),
818 invalid("((-2)) ** 3", "(-2) ** 3", "UnaryExpression", null),
819
820 invalid("a = (b * c)", "a = b * c", "BinaryExpression", null, { options: ["all", { nestedBinaryExpressions: false }] }),
821 invalid("(b * c)", "b * c", "BinaryExpression", null, { options: ["all", { nestedBinaryExpressions: false }] }),
822
823 invalid("a = (b = c)", "a = b = c", "AssignmentExpression"),
824 invalid("(a).b", "a.b", "Identifier"),
825 invalid("(0)[a]", "0[a]", "Literal"),
826 invalid("(0.0).a", "0.0.a", "Literal"),
827 invalid("(123.4).a", "123.4.a", "Literal"),
828 invalid("(0.0_0).a", "0.0_0.a", "Literal"),
829 invalid("(0xBEEF).a", "0xBEEF.a", "Literal"),
830 invalid("(0xBE_EF).a", "0xBE_EF.a", "Literal"),
831 invalid("(1e6).a", "1e6.a", "Literal"),
832 invalid("(0123).a", "0123.a", "Literal"),
833 invalid("(08.1).a", "08.1.a", "Literal"),
834 invalid("(09.).a", "09..a", "Literal"),
835 invalid("a[(function() {})]", "a[function() {}]", "FunctionExpression"),
836 invalid("new (function(){})", "new function(){}", "FunctionExpression"),
837 invalid("new (\nfunction(){}\n)", "new \nfunction(){}\n", "FunctionExpression", 1),
838 invalid("((function foo() {return 1;}))()", "(function foo() {return 1;})()", "FunctionExpression"),
839 invalid("((function(){ return bar(); })())", "(function(){ return bar(); })()", "CallExpression"),
840 invalid("(foo()).bar", "foo().bar", "CallExpression"),
841 invalid("(foo.bar()).baz", "foo.bar().baz", "CallExpression"),
842 invalid("(foo\n.bar())\n.baz", "foo\n.bar()\n.baz", "CallExpression"),
843 invalid("(new foo()).bar", "new foo().bar", "NewExpression"),
844 invalid("(new foo())[bar]", "new foo()[bar]", "NewExpression"),
845 invalid("(new foo()).bar()", "new foo().bar()", "NewExpression"),
846 invalid("(new foo(bar)).baz", "new foo(bar).baz", "NewExpression"),
847 invalid("(new foo.bar()).baz", "new foo.bar().baz", "NewExpression"),
848 invalid("(new foo.bar()).baz()", "new foo.bar().baz()", "NewExpression"),
849 invalid("new a[(b()).c]", "new a[b().c]", "CallExpression"),
850
851 invalid("(a)()", "a()", "Identifier"),
852 invalid("(a.b)()", "a.b()", "MemberExpression"),
853 invalid("(a())()", "a()()", "CallExpression"),
854 invalid("(a.b())()", "a.b()()", "CallExpression"),
855 invalid("(a().b)()", "a().b()", "MemberExpression"),
856 invalid("(a().b.c)()", "a().b.c()", "MemberExpression"),
857 invalid("new (A)", "new A", "Identifier"),
858 invalid("(new A())()", "new A()()", "NewExpression"),
859 invalid("(new A(1))()", "new A(1)()", "NewExpression"),
860 invalid("((new A))()", "(new A)()", "NewExpression"),
861 invalid("new (foo\n.baz\n.bar\n.foo.baz)", "new foo\n.baz\n.bar\n.foo.baz", "MemberExpression"),
862 invalid("new (foo.baz.bar.baz)", "new foo.baz.bar.baz", "MemberExpression"),
863 invalid("new ((a.b())).c", "new (a.b()).c", "CallExpression"),
864 invalid("new ((a().b)).c", "new (a().b).c", "MemberExpression"),
865 invalid("new ((a().b().d))", "new (a().b().d)", "MemberExpression"),
866 invalid("new ((a())).b.d", "new (a()).b.d", "CallExpression"),
867 invalid("new (a.b).d;", "new a.b.d;", "MemberExpression"),
868 invalid("new (new A())();", "new new A()();", "NewExpression"),
869 invalid("new (new A());", "new new A();", "NewExpression"),
870 invalid("new (new A);", "new new A;", "NewExpression"),
871 invalid("new (new a.b);", "new new a.b;", "NewExpression"),
872 invalid("(a().b).d;", "a().b.d;", "MemberExpression"),
873 invalid("(a.b()).d;", "a.b().d;", "CallExpression"),
874 invalid("(a.b).d;", "a.b.d;", "MemberExpression"),
875
876 invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1),
877 invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1),
878 invalid("a = (_ => 0)", "a = _ => 0", "ArrowFunctionExpression", 1),
879 invalid("_ => (a = 0)", "_ => a = 0", "AssignmentExpression", 1),
880 invalid("x => (({}))", "x => ({})", "ObjectExpression", 1),
881
882 invalid("new (function(){})", "new function(){}", "FunctionExpression", null, { options: ["functions"] }),
883 invalid("new (\nfunction(){}\n)", "new \nfunction(){}\n", "FunctionExpression", 1, { options: ["functions"] }),
884 invalid("((function foo() {return 1;}))()", "(function foo() {return 1;})()", "FunctionExpression", null, { options: ["functions"] }),
885 invalid("a[(function() {})]", "a[function() {}]", "FunctionExpression", null, { options: ["functions"] }),
886 invalid("0, (_ => 0)", "0, _ => 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
887 invalid("(_ => 0), 0", "_ => 0, 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
888 invalid("a = (_ => 0)", "a = _ => 0", "ArrowFunctionExpression", 1, { options: ["functions"] }),
889
890
891 invalid("while ((foo = bar())) {}", "while (foo = bar()) {}", "AssignmentExpression"),
892 invalid("while ((foo = bar())) {}", "while (foo = bar()) {}", "AssignmentExpression", 1, { options: ["all", { conditionalAssign: true }] }),
893 invalid("if ((foo = bar())) {}", "if (foo = bar()) {}", "AssignmentExpression"),
894 invalid("do; while ((foo = bar()))", "do; while (foo = bar())", "AssignmentExpression"),
895 invalid("for (;(a = b););", "for (;a = b;);", "AssignmentExpression"),
896
897 // https://github.com/eslint/eslint/issues/3653
898 invalid("((function(){})).foo();", "(function(){}).foo();", "FunctionExpression"),
899 invalid("((function(){}).foo());", "(function(){}).foo();", "CallExpression"),
900 invalid("((function(){}).foo);", "(function(){}).foo;", "MemberExpression"),
901 invalid("0, (function(){}).foo();", "0, function(){}.foo();", "FunctionExpression"),
902 invalid("void (function(){}).foo();", "void function(){}.foo();", "FunctionExpression"),
903 invalid("++(function(){}).foo;", "++function(){}.foo;", "FunctionExpression"),
904 invalid("bar || (function(){}).foo();", "bar || function(){}.foo();", "FunctionExpression"),
905 invalid("1 + (function(){}).foo();", "1 + function(){}.foo();", "FunctionExpression"),
906 invalid("bar ? (function(){}).foo() : baz;", "bar ? function(){}.foo() : baz;", "FunctionExpression"),
907 invalid("bar ? baz : (function(){}).foo();", "bar ? baz : function(){}.foo();", "FunctionExpression"),
908 invalid("bar((function(){}).foo(), 0);", "bar(function(){}.foo(), 0);", "FunctionExpression"),
909 invalid("bar[(function(){}).foo()];", "bar[function(){}.foo()];", "FunctionExpression"),
910 invalid("var bar = (function(){}).foo();", "var bar = function(){}.foo();", "FunctionExpression"),
911
912 invalid("((class{})).foo();", "(class{}).foo();", "ClassExpression", null),
913 invalid("((class{}).foo());", "(class{}).foo();", "CallExpression", null),
914 invalid("((class{}).foo);", "(class{}).foo;", "MemberExpression", null),
915 invalid("0, (class{}).foo();", "0, class{}.foo();", "ClassExpression", null),
916 invalid("void (class{}).foo();", "void class{}.foo();", "ClassExpression", null),
917 invalid("++(class{}).foo;", "++class{}.foo;", "ClassExpression", null),
918 invalid("bar || (class{}).foo();", "bar || class{}.foo();", "ClassExpression", null),
919 invalid("1 + (class{}).foo();", "1 + class{}.foo();", "ClassExpression", null),
920 invalid("bar ? (class{}).foo() : baz;", "bar ? class{}.foo() : baz;", "ClassExpression", null),
921 invalid("bar ? baz : (class{}).foo();", "bar ? baz : class{}.foo();", "ClassExpression", null),
922 invalid("bar((class{}).foo(), 0);", "bar(class{}.foo(), 0);", "ClassExpression", null),
923 invalid("bar[(class{}).foo()];", "bar[class{}.foo()];", "ClassExpression", null),
924 invalid("var bar = (class{}).foo();", "var bar = class{}.foo();", "ClassExpression", null),
925 invalid("var foo = ((bar, baz));", "var foo = (bar, baz);", "SequenceExpression", null),
926
927 // https://github.com/eslint/eslint/issues/4608
928 invalid("function *a() { yield (b); }", "function *a() { yield b; }", "Identifier", null),
929 invalid("function *a() { (yield b), c; }", "function *a() { yield b, c; }", "YieldExpression", null),
930 invalid("function *a() { yield ((b, c)); }", "function *a() { yield (b, c); }", "SequenceExpression", null),
931 invalid("function *a() { yield (b + c); }", "function *a() { yield b + c; }", "BinaryExpression", null),
932
933 // https://github.com/eslint/eslint/issues/4229
934 invalid([
935 "function a() {",
936 " return (b);",
937 "}"
938 ].join("\n"), [
939 "function a() {",
940 " return b;",
941 "}"
942 ].join("\n"), "Identifier"),
943 invalid([
944 "function a() {",
945 " return",
946 " (b);",
947 "}"
948 ].join("\n"), [
949 "function a() {",
950 " return",
951 " b;",
952 "}"
953 ].join("\n"), "Identifier"),
954 invalid([
955 "function a() {",
956 " return ((",
957 " b",
958 " ));",
959 "}"
960 ].join("\n"), [
961 "function a() {",
962 " return (",
963 " b",
964 " );",
965 "}"
966 ].join("\n"), "Identifier"),
967 invalid([
968 "function a() {",
969 " return (<JSX />);",
970 "}"
971 ].join("\n"), [
972 "function a() {",
973 " return <JSX />;",
974 "}"
975 ].join("\n"), "JSXElement", null),
976 invalid([
977 "function a() {",
978 " return",
979 " (<JSX />);",
980 "}"
981 ].join("\n"), [
982 "function a() {",
983 " return",
984 " <JSX />;",
985 "}"
986 ].join("\n"), "JSXElement", null),
987 invalid([
988 "function a() {",
989 " return ((",
990 " <JSX />",
991 " ));",
992 "}"
993 ].join("\n"), [
994 "function a() {",
995 " return (",
996 " <JSX />",
997 " );",
998 "}"
999 ].join("\n"), "JSXElement", null),
1000 invalid([
1001 "function a() {",
1002 " return ((",
1003 " <></>",
1004 " ));",
1005 "}"
1006 ].join("\n"), [
1007 "function a() {",
1008 " return (",
1009 " <></>",
1010 " );",
1011 "}"
1012 ].join("\n"), "JSXFragment", null),
1013 invalid("throw (a);", "throw a;", "Identifier"),
1014 invalid([
1015 "throw ((",
1016 " a",
1017 "));"
1018 ].join("\n"), [
1019 "throw (",
1020 " a",
1021 ");"
1022 ].join("\n"), "Identifier"),
1023 invalid([
1024 "function *a() {",
1025 " yield (b);",
1026 "}"
1027 ].join("\n"), [
1028 "function *a() {",
1029 " yield b;",
1030 "}"
1031 ].join("\n"), "Identifier", null),
1032 invalid([
1033 "function *a() {",
1034 " yield",
1035 " (b);",
1036 "}"
1037 ].join("\n"), [
1038 "function *a() {",
1039 " yield",
1040 " b;",
1041 "}"
1042 ].join("\n"), "Identifier", null),
1043 invalid([
1044 "function *a() {",
1045 " yield ((",
1046 " b",
1047 " ));",
1048 "}"
1049 ].join("\n"), [
1050 "function *a() {",
1051 " yield (",
1052 " b",
1053 " );",
1054 "}"
1055 ].join("\n"), "Identifier", null),
1056
1057 // returnAssign option
1058 {
1059 code: "function a(b) { return (b || c); }",
1060 output: "function a(b) { return b || c; }",
1061 options: ["all", { returnAssign: false }],
1062 errors: [
1063 {
1064 messageId: "unexpected",
1065 type: "LogicalExpression"
1066 }
1067 ]
1068 },
1069 {
1070 code: "function a(b) { return ((b = c) || (d = e)); }",
1071 output: "function a(b) { return (b = c) || (d = e); }",
1072 errors: [
1073 {
1074 messageId: "unexpected",
1075 type: "LogicalExpression"
1076 }
1077 ]
1078 },
1079 {
1080 code: "function a(b) { return (b = 1); }",
1081 output: "function a(b) { return b = 1; }",
1082 errors: [
1083 {
1084 messageId: "unexpected",
1085 type: "AssignmentExpression"
1086 }
1087 ]
1088 },
1089 {
1090 code: "function a(b) { return c ? (d = b) : (e = b); }",
1091 output: "function a(b) { return c ? d = b : e = b; }",
1092 errors: [
1093 {
1094 messageId: "unexpected",
1095 type: "AssignmentExpression"
1096 },
1097 {
1098 messageId: "unexpected",
1099 type: "AssignmentExpression"
1100 }
1101 ]
1102 },
1103 {
1104 code: "b => (b || c);",
1105 output: "b => b || c;",
1106 options: ["all", { returnAssign: false }],
1107
1108 errors: [
1109 {
1110 messageId: "unexpected",
1111 type: "LogicalExpression"
1112 }
1113 ]
1114 },
1115 {
1116 code: "b => ((b = c) || (d = e));",
1117 output: "b => (b = c) || (d = e);",
1118 errors: [
1119 {
1120 messageId: "unexpected",
1121 type: "LogicalExpression"
1122 }
1123 ]
1124 },
1125 {
1126 code: "b => (b = 1);",
1127 output: "b => b = 1;",
1128 errors: [
1129 {
1130 messageId: "unexpected",
1131 type: "AssignmentExpression"
1132 }
1133 ]
1134 },
1135 {
1136 code: "b => c ? (d = b) : (e = b);",
1137 output: "b => c ? d = b : e = b;",
1138 errors: [
1139 {
1140 messageId: "unexpected",
1141 type: "AssignmentExpression"
1142 },
1143 {
1144 messageId: "unexpected",
1145 type: "AssignmentExpression"
1146 }
1147 ]
1148 },
1149 {
1150 code: "b => { return (b || c); }",
1151 output: "b => { return b || c; }",
1152 options: ["all", { returnAssign: false }],
1153 errors: [
1154 {
1155 messageId: "unexpected",
1156 type: "LogicalExpression"
1157 }
1158 ]
1159 },
1160 {
1161 code: "b => { return ((b = c) || (d = e)) };",
1162 output: "b => { return (b = c) || (d = e) };",
1163 errors: [
1164 {
1165 messageId: "unexpected",
1166 type: "LogicalExpression"
1167 }
1168 ]
1169 },
1170 {
1171 code: "b => { return (b = 1) };",
1172 output: "b => { return b = 1 };",
1173 errors: [
1174 {
1175 messageId: "unexpected",
1176 type: "AssignmentExpression"
1177 }
1178 ]
1179 },
1180 {
1181 code: "b => { return c ? (d = b) : (e = b); }",
1182 output: "b => { return c ? d = b : e = b; }",
1183 errors: [
1184 {
1185 messageId: "unexpected",
1186 type: "AssignmentExpression"
1187 },
1188 {
1189 messageId: "unexpected",
1190 type: "AssignmentExpression"
1191 }
1192 ]
1193 },
1194
1195 // async/await
1196 {
1197 code: "async function a() { (await a) + (await b); }",
1198 output: "async function a() { await a + await b; }",
1199 errors: [
1200 {
1201 messageId: "unexpected",
1202 type: "AwaitExpression"
1203 },
1204 {
1205 messageId: "unexpected",
1206 type: "AwaitExpression"
1207 }
1208 ]
1209 },
1210 invalid("async function a() { await (a); }", "async function a() { await a; }", "Identifier", null),
1211 invalid("async function a() { await (a()); }", "async function a() { await a(); }", "CallExpression", null),
1212 invalid("async function a() { await (+a); }", "async function a() { await +a; }", "UnaryExpression", null),
1213 invalid("async function a() { +(await a); }", "async function a() { +await a; }", "AwaitExpression", null),
1214 invalid("async function a() { await ((a,b)); }", "async function a() { await (a,b); }", "SequenceExpression", null),
1215 invalid("async function a() { a ** (await b); }", "async function a() { a ** await b; }", "AwaitExpression", null),
1216
1217 invalid("(foo) instanceof bar", "foo instanceof bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1218 invalid("(foo) in bar", "foo in bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1219 invalid("(foo) + bar", "foo + bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1220 invalid("(foo) && bar", "foo && bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1221 invalid("foo instanceof (bar)", "foo instanceof bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1222 invalid("foo in (bar)", "foo in bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1223 invalid("foo + (bar)", "foo + bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1224 invalid("foo && (bar)", "foo && bar", "Identifier", 1, { options: ["all", { nestedBinaryExpressions: false }] }),
1225
1226 // ["all", { ignoreJSX: "multi-line" }]
1227 invalid("const Component = (<div />);", "const Component = <div />;", "JSXElement", 1, {
1228 options: ["all", { ignoreJSX: "multi-line" }]
1229 }),
1230 invalid([
1231 "const Component = (",
1232 " <div />",
1233 ");"
1234 ].join("\n"), "const Component = \n <div />\n;", "JSXElement", 1, {
1235 options: ["all", { ignoreJSX: "multi-line" }]
1236 }),
1237 invalid([
1238 "const Component = (",
1239 " <></>",
1240 ");"
1241 ].join("\n"), "const Component = \n <></>\n;", "JSXFragment", 1, {
1242 options: ["all", { ignoreJSX: "multi-line" }]
1243 }),
1244
1245 // ["all", { ignoreJSX: "single-line" }]
1246 invalid([
1247 "const Component = (",
1248 "<div>",
1249 " <p />",
1250 "</div>",
1251 ");"
1252 ].join("\n"), "const Component = \n<div>\n <p />\n</div>\n;", "JSXElement", 1, {
1253 options: ["all", { ignoreJSX: "single-line" }]
1254 }),
1255 invalid([
1256 "const Component = (<div>",
1257 " <p />",
1258 "</div>);"
1259 ].join("\n"), "const Component = <div>\n <p />\n</div>;", "JSXElement", 1, {
1260 options: ["all", { ignoreJSX: "single-line" }]
1261 }),
1262 invalid([
1263 "const Component = (<div",
1264 " prop={true}",
1265 "/>)"
1266 ].join("\n"), "const Component = <div\n prop={true}\n/>", "JSXElement", 1, {
1267 options: ["all", { ignoreJSX: "single-line" }]
1268 }),
1269
1270 // ["all", { ignoreJSX: "none" }] default, same as unspecified
1271 invalid("const Component = (<div />);", "const Component = <div />;", "JSXElement", 1, {
1272 options: ["all", { ignoreJSX: "none" }]
1273 }),
1274 invalid([
1275 "const Component = (<div>",
1276 "<p />",
1277 "</div>)"
1278 ].join("\n"), "const Component = <div>\n<p />\n</div>", "JSXElement", 1, {
1279 options: ["all", { ignoreJSX: "none" }]
1280 }),
1281
1282 // ["all", { enforceForArrowConditionals: true }]
1283 {
1284 code: "var a = (b) => (1 ? 2 : 3)",
1285 output: "var a = (b) => 1 ? 2 : 3",
1286 options: ["all", { enforceForArrowConditionals: true }],
1287 errors: [
1288 {
1289 messageId: "unexpected"
1290 }
1291 ]
1292 },
1293 {
1294 code: "var a = (b) => ((1 ? 2 : 3))",
1295 output: "var a = (b) => (1 ? 2 : 3)",
1296 options: ["all", { enforceForArrowConditionals: true }],
1297 errors: [
1298 {
1299 messageId: "unexpected"
1300 }
1301 ]
1302 },
1303
1304 // ["all", { enforceForSequenceExpressions: true }]
1305 {
1306 code: "(a, b)",
1307 output: "a, b",
1308 options: ["all"],
1309 errors: [
1310 {
1311 messageId: "unexpected",
1312 type: "SequenceExpression"
1313 }
1314 ]
1315 },
1316 {
1317 code: "(a, b)",
1318 output: "a, b",
1319 options: ["all", {}],
1320 errors: [
1321 {
1322 messageId: "unexpected",
1323 type: "SequenceExpression"
1324 }
1325 ]
1326 },
1327 {
1328 code: "(a, b)",
1329 output: "a, b",
1330 options: ["all", { enforceForSequenceExpressions: true }],
1331 errors: [
1332 {
1333 messageId: "unexpected",
1334 type: "SequenceExpression"
1335 }
1336 ]
1337 },
1338 {
1339 code: "(foo(), bar());",
1340 output: "foo(), bar();",
1341 options: ["all", { enforceForSequenceExpressions: true }],
1342 errors: [
1343 {
1344 messageId: "unexpected",
1345 type: "SequenceExpression"
1346 }
1347 ]
1348 },
1349 {
1350 code: "if((a, b)){}",
1351 output: "if(a, b){}",
1352 options: ["all", { enforceForSequenceExpressions: true }],
1353 errors: [
1354 {
1355 messageId: "unexpected",
1356 type: "SequenceExpression"
1357 }
1358 ]
1359 },
1360 {
1361 code: "while ((val = foo(), val < 10));",
1362 output: "while (val = foo(), val < 10);",
1363 options: ["all", { enforceForSequenceExpressions: true }],
1364 errors: [
1365 {
1366 messageId: "unexpected",
1367 type: "SequenceExpression"
1368 }
1369 ]
1370 },
1371
1372 // ["all", { enforceForNewInMemberExpressions: true }]
1373 {
1374 code: "(new foo()).bar",
1375 output: "new foo().bar",
1376 options: ["all"],
1377 errors: [
1378 {
1379 messageId: "unexpected",
1380 type: "NewExpression"
1381 }
1382 ]
1383 },
1384 {
1385 code: "(new foo()).bar",
1386 output: "new foo().bar",
1387 options: ["all", {}],
1388 errors: [
1389 {
1390 messageId: "unexpected",
1391 type: "NewExpression"
1392 }
1393 ]
1394 },
1395 {
1396 code: "(new foo()).bar",
1397 output: "new foo().bar",
1398 options: ["all", { enforceForNewInMemberExpressions: true }],
1399 errors: [
1400 {
1401 messageId: "unexpected",
1402 type: "NewExpression"
1403 }
1404 ]
1405 },
1406 {
1407 code: "(new foo())[bar]",
1408 output: "new foo()[bar]",
1409 options: ["all", { enforceForNewInMemberExpressions: true }],
1410 errors: [
1411 {
1412 messageId: "unexpected",
1413 type: "NewExpression"
1414 }
1415 ]
1416 },
1417 {
1418 code: "(new foo.bar()).baz",
1419 output: "new foo.bar().baz",
1420 options: ["all", { enforceForNewInMemberExpressions: true }],
1421 errors: [
1422 {
1423 messageId: "unexpected",
1424 type: "NewExpression"
1425 }
1426 ]
1427 },
1428
1429 // enforceForFunctionPrototypeMethods
1430 {
1431 code: "var foo = (function(){}).call()",
1432 output: "var foo = function(){}.call()",
1433 options: ["all"],
1434 errors: [
1435 {
1436 messageId: "unexpected",
1437 type: "FunctionExpression"
1438 }
1439 ]
1440 },
1441 {
1442 code: "var foo = (function(){}.apply())",
1443 output: "var foo = function(){}.apply()",
1444 options: ["all"],
1445 errors: [
1446 {
1447 messageId: "unexpected",
1448 type: "CallExpression"
1449 }
1450 ]
1451 },
1452 {
1453 code: "var foo = (function(){}).apply()",
1454 output: "var foo = function(){}.apply()",
1455 options: ["all", {}],
1456 errors: [
1457 {
1458 messageId: "unexpected",
1459 type: "FunctionExpression"
1460 }
1461 ]
1462 },
1463 {
1464 code: "var foo = (function(){}.call())",
1465 output: "var foo = function(){}.call()",
1466 options: ["all", {}],
1467 errors: [
1468 {
1469 messageId: "unexpected",
1470 type: "CallExpression"
1471 }
1472 ]
1473 },
1474 {
1475 code: "var foo = (function(){}).call()",
1476 output: "var foo = function(){}.call()",
1477 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1478 errors: [
1479 {
1480 messageId: "unexpected",
1481 type: "FunctionExpression"
1482 }
1483 ]
1484 },
1485 {
1486 code: "var foo = (function(){}).apply()",
1487 output: "var foo = function(){}.apply()",
1488 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1489 errors: [
1490 {
1491 messageId: "unexpected",
1492 type: "FunctionExpression"
1493 }
1494 ]
1495 },
1496 {
1497 code: "var foo = (function(){}.call())",
1498 output: "var foo = function(){}.call()",
1499 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1500 errors: [
1501 {
1502 messageId: "unexpected",
1503 type: "CallExpression"
1504 }
1505 ]
1506 },
1507 {
1508 code: "var foo = (function(){}.apply())",
1509 output: "var foo = function(){}.apply()",
1510 options: ["all", { enforceForFunctionPrototypeMethods: true }],
1511 errors: [
1512 {
1513 messageId: "unexpected",
1514 type: "CallExpression"
1515 }
1516 ]
1517 },
1518 {
1519 code: "var foo = (function(){}.call)()", // removing these parens does not cause any conflicts with wrap-iife
1520 output: "var foo = function(){}.call()",
1521 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1522 errors: [
1523 {
1524 messageId: "unexpected",
1525 type: "MemberExpression"
1526 }
1527 ]
1528 },
1529 {
1530 code: "var foo = (function(){}.apply)()", // removing these parens does not cause any conflicts with wrap-iife
1531 output: "var foo = function(){}.apply()",
1532 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1533 errors: [
1534 {
1535 messageId: "unexpected",
1536 type: "MemberExpression"
1537 }
1538 ]
1539 },
1540 {
1541 code: "var foo = (function(){}).call",
1542 output: "var foo = function(){}.call",
1543 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1544 errors: [
1545 {
1546 messageId: "unexpected",
1547 type: "FunctionExpression"
1548 }
1549 ]
1550 },
1551 {
1552 code: "var foo = (function(){}.call)",
1553 output: "var foo = function(){}.call",
1554 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1555 errors: [
1556 {
1557 messageId: "unexpected",
1558 type: "MemberExpression"
1559 }
1560 ]
1561 },
1562 {
1563 code: "var foo = new (function(){}).call()",
1564 output: "var foo = new function(){}.call()",
1565 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1566 errors: [
1567 {
1568 messageId: "unexpected",
1569 type: "FunctionExpression"
1570 }
1571 ]
1572 },
1573 {
1574 code: "var foo = (new function(){}.call())",
1575 output: "var foo = new function(){}.call()",
1576 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1577 errors: [
1578 {
1579 messageId: "unexpected",
1580 type: "NewExpression"
1581 }
1582 ]
1583 },
1584 {
1585 code: "var foo = (function(){})[call]()",
1586 output: "var foo = function(){}[call]()",
1587 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1588 errors: [
1589 {
1590 messageId: "unexpected",
1591 type: "FunctionExpression"
1592 }
1593 ]
1594 },
1595 {
1596 code: "var foo = (function(){}[apply]())",
1597 output: "var foo = function(){}[apply]()",
1598 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1599 errors: [
1600 {
1601 messageId: "unexpected",
1602 type: "CallExpression"
1603 }
1604 ]
1605 },
1606 {
1607 code: "var foo = (function(){}).bar()",
1608 output: "var foo = function(){}.bar()",
1609 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1610 errors: [
1611 {
1612 messageId: "unexpected",
1613 type: "FunctionExpression"
1614 }
1615 ]
1616 },
1617 {
1618 code: "var foo = (function(){}.bar())",
1619 output: "var foo = function(){}.bar()",
1620 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1621 errors: [
1622 {
1623 messageId: "unexpected",
1624 type: "CallExpression"
1625 }
1626 ]
1627 },
1628 {
1629 code: "var foo = (function(){}).call.call()",
1630 output: "var foo = function(){}.call.call()",
1631 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1632 errors: [
1633 {
1634 messageId: "unexpected",
1635 type: "FunctionExpression"
1636 }
1637 ]
1638 },
1639 {
1640 code: "var foo = (function(){}.call.call())",
1641 output: "var foo = function(){}.call.call()",
1642 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1643 errors: [
1644 {
1645 messageId: "unexpected",
1646 type: "CallExpression"
1647 }
1648 ]
1649 },
1650 {
1651 code: "var foo = (call())",
1652 output: "var foo = call()",
1653 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1654 errors: [
1655 {
1656 messageId: "unexpected",
1657 type: "CallExpression"
1658 }
1659 ]
1660 },
1661 {
1662 code: "var foo = (apply())",
1663 output: "var foo = apply()",
1664 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1665 errors: [
1666 {
1667 messageId: "unexpected",
1668 type: "CallExpression"
1669 }
1670 ]
1671 },
1672 {
1673 code: "var foo = (bar).call()",
1674 output: "var foo = bar.call()",
1675 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1676 errors: [
1677 {
1678 messageId: "unexpected",
1679 type: "Identifier"
1680 }
1681 ]
1682 },
1683 {
1684 code: "var foo = (bar.call())",
1685 output: "var foo = bar.call()",
1686 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1687 errors: [
1688 {
1689 messageId: "unexpected",
1690 type: "CallExpression"
1691 }
1692 ]
1693 },
1694 {
1695 code: "((() => {}).call())",
1696 output: "(() => {}).call()",
1697 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1698 errors: [
1699 {
1700 messageId: "unexpected",
1701 type: "CallExpression"
1702 }
1703 ]
1704 },
1705 {
1706 code: "var foo = function(){}.call((a.b))",
1707 output: "var foo = function(){}.call(a.b)",
1708 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1709 errors: [
1710 {
1711 messageId: "unexpected",
1712 type: "MemberExpression"
1713 }
1714 ]
1715 },
1716 {
1717 code: "var foo = function(){}.call((a).b)",
1718 output: "var foo = function(){}.call(a.b)",
1719 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1720 errors: [
1721 {
1722 messageId: "unexpected",
1723 type: "Identifier"
1724 }
1725 ]
1726 },
1727 {
1728 code: "var foo = function(){}[('call')]()",
1729 output: "var foo = function(){}['call']()",
1730 options: ["all", { enforceForFunctionPrototypeMethods: false }],
1731 errors: [
1732 {
1733 messageId: "unexpected",
1734 type: "Literal"
1735 }
1736 ]
1737 },
1738
1739 // https://github.com/eslint/eslint/issues/8175
1740 invalid(
1741 "let a = [...(b)]",
1742 "let a = [...b]",
1743 "Identifier",
1744 1
1745 ),
1746 invalid(
1747 "let a = {...(b)}",
1748 "let a = {...b}",
1749 "Identifier",
1750 1
1751 ),
1752 invalid(
1753 "let a = {...(b)}",
1754 "let a = {...b}",
1755 "Identifier",
1756 1,
1757 { parserOptions: { ecmaVersion: 2018 } }
1758 ),
1759 invalid(
1760 "let a = [...((b, c))]",
1761 "let a = [...(b, c)]",
1762 "SequenceExpression",
1763 1
1764 ),
1765 invalid(
1766 "let a = {...((b, c))}",
1767 "let a = {...(b, c)}",
1768 "SequenceExpression",
1769 1
1770 ),
1771 invalid(
1772 "let a = {...((b, c))}",
1773 "let a = {...(b, c)}",
1774 "SequenceExpression",
1775 1,
1776 { parserOptions: { ecmaVersion: 2018 } }
1777 ),
1778 invalid(
1779 "class A extends (B) {}",
1780 "class A extends B {}",
1781 "Identifier",
1782 1
1783 ),
1784 invalid(
1785 "const A = class extends (B) {}",
1786 "const A = class extends B {}",
1787 "Identifier",
1788 1
1789 ),
1790 invalid(
1791 "class A extends ((B=C)) {}",
1792 "class A extends (B=C) {}",
1793 "AssignmentExpression",
1794 1
1795 ),
1796 invalid(
1797 "const A = class extends ((B=C)) {}",
1798 "const A = class extends (B=C) {}",
1799 "AssignmentExpression",
1800 1
1801 ),
1802 invalid(
1803 "class A extends ((++foo)) {}",
1804 "class A extends (++foo) {}",
1805 "UpdateExpression",
1806 1
1807 ),
1808 invalid(
1809 "export default ((a, b))",
1810 "export default (a, b)",
1811 "SequenceExpression",
1812 1,
1813 { parserOptions: { sourceType: "module" } }
1814 ),
1815 invalid(
1816 "export default (() => {})",
1817 "export default () => {}",
1818 "ArrowFunctionExpression",
1819 1,
1820 { parserOptions: { sourceType: "module" } }
1821 ),
1822 invalid(
1823 "export default ((a, b) => a + b)",
1824 "export default (a, b) => a + b",
1825 "ArrowFunctionExpression",
1826 1,
1827 { parserOptions: { sourceType: "module" } }
1828 ),
1829 invalid(
1830 "export default (a => a)",
1831 "export default a => a",
1832 "ArrowFunctionExpression",
1833 1,
1834 { parserOptions: { sourceType: "module" } }
1835 ),
1836 invalid(
1837 "export default (a = b)",
1838 "export default a = b",
1839 "AssignmentExpression",
1840 1,
1841 { parserOptions: { sourceType: "module" } }
1842 ),
1843 invalid(
1844 "export default (a ? b : c)",
1845 "export default a ? b : c",
1846 "ConditionalExpression",
1847 1,
1848 { parserOptions: { sourceType: "module" } }
1849 ),
1850 invalid(
1851 "export default (a)",
1852 "export default a",
1853 "Identifier",
1854 1,
1855 { parserOptions: { sourceType: "module" } }
1856 ),
1857 invalid(
1858 "for (foo of(bar));",
1859 "for (foo of bar);",
1860 "Identifier",
1861 1
1862 ),
1863 invalid(
1864 "for ((foo) of bar);",
1865 "for (foo of bar);",
1866 "Identifier",
1867 1
1868 ),
1869 invalid(
1870 "for (foo of ((bar, baz)));",
1871 "for (foo of (bar, baz));",
1872 "SequenceExpression",
1873 1
1874 ),
1875 invalid(
1876 "for ((foo)in bar);",
1877 "for (foo in bar);",
1878 "Identifier",
1879 1
1880 ),
1881 invalid(
1882 "for ((foo['bar'])of baz);",
1883 "for (foo['bar']of baz);",
1884 "MemberExpression",
1885 1
1886 ),
1887 invalid(
1888 "() => (({ foo: 1 }).foo)",
1889 "() => ({ foo: 1 }).foo",
1890 "MemberExpression",
1891 1
1892 ),
1893 invalid(
1894 "(let).foo",
1895 "let.foo",
1896 "Identifier",
1897 1
1898 ),
1899
1900 // ForStatement#init expression cannot start with `let[`, but it can start with `let` if it isn't followed by `[`
1901 invalid(
1902 "for ((let);;);",
1903 "for (let;;);",
1904 "Identifier",
1905 1
1906 ),
1907 invalid(
1908 "for ((let = 1);;);",
1909 "for (let = 1;;);",
1910 "AssignmentExpression",
1911 1
1912 ),
1913 invalid(
1914 "for ((let) = 1;;);",
1915 "for (let = 1;;);",
1916 "Identifier",
1917 1
1918 ),
1919 invalid(
1920 "for ((let = []);;);",
1921 "for (let = [];;);",
1922 "AssignmentExpression",
1923 1
1924 ),
1925 invalid(
1926 "for ((let) = [];;);",
1927 "for (let = [];;);",
1928 "Identifier",
1929 1
1930 ),
1931 invalid(
1932 "for ((let());;);",
1933 "for (let();;);",
1934 "CallExpression",
1935 1
1936 ),
1937 invalid(
1938 "for ((let([]));;);",
1939 "for (let([]);;);",
1940 "CallExpression",
1941 1
1942 ),
1943 invalid(
1944 "for ((let())[a];;);",
1945 "for (let()[a];;);",
1946 "CallExpression",
1947 1
1948 ),
1949 invalid(
1950 "for ((let`[]`);;);",
1951 "for (let`[]`;;);",
1952 "TaggedTemplateExpression",
1953 1
1954 ),
1955 invalid(
1956 "for ((let.a);;);",
1957 "for (let.a;;);",
1958 "MemberExpression",
1959 1
1960 ),
1961 invalid(
1962 "for ((let).a;;);",
1963 "for (let.a;;);",
1964 "Identifier",
1965 1
1966 ),
1967 invalid(
1968 "for ((let).a = 1;;);",
1969 "for (let.a = 1;;);",
1970 "Identifier",
1971 1
1972 ),
1973 invalid(
1974 "for ((let).a[b];;);",
1975 "for (let.a[b];;);",
1976 "Identifier",
1977 1
1978 ),
1979 invalid(
1980 "for ((let.a)[b];;);",
1981 "for (let.a[b];;);",
1982 "MemberExpression",
1983 1
1984 ),
1985 invalid(
1986 "for ((let.a[b]);;);",
1987 "for (let.a[b];;);",
1988 "MemberExpression",
1989 1
1990 ),
1991 invalid(
1992 "for ((let);[];);",
1993 "for (let;[];);",
1994 "Identifier",
1995 1
1996 ),
1997 invalid(
1998 "for (((let[a]));;);",
1999 "for ((let[a]);;);",
2000 "MemberExpression",
2001 1
2002 ),
2003 invalid(
2004 "for (((let))[a];;);",
2005 "for ((let)[a];;);",
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 "Identifier",
2019 1
2020 ),
2021 invalid(
2022 "for (((let)[a]).b;;);",
2023 "for ((let)[a].b;;);",
2024 "MemberExpression",
2025 1
2026 ),
2027 invalid(
2028 "for (((let[a]) = b);;);",
2029 "for ((let[a]) = b;;);",
2030 "AssignmentExpression",
2031 1
2032 ),
2033 invalid(
2034 "for (((let)[a]) = b;;);",
2035 "for ((let)[a] = b;;);",
2036 "MemberExpression",
2037 1
2038 ),
2039 invalid(
2040 "for (((let)[a] = b);;);",
2041 "for ((let)[a] = b;;);",
2042 "AssignmentExpression",
2043 1
2044 ),
2045 invalid(
2046 "for ((Let[a]);;);",
2047 "for (Let[a];;);",
2048 "MemberExpression",
2049 1
2050 ),
2051 invalid(
2052 "for ((lett)[a];;);",
2053 "for (lett[a];;);",
2054 "Identifier",
2055 1
2056 ),
2057
2058 invalid(
2059 "for ((let.foo) in bar);",
2060 "for (let.foo in bar);",
2061 "MemberExpression",
2062 1
2063 ),
2064 invalid(
2065 "for ((let).foo.bar in baz);",
2066 "for (let.foo.bar in baz);",
2067 "Identifier",
2068 1
2069 ),
2070 invalid("for (a in (b, c));", "for (a in b, c);", "SequenceExpression", null),
2071 invalid(
2072 "(let)",
2073 "let",
2074 "Identifier",
2075 1
2076 ),
2077 invalid(
2078 "((let))",
2079 "(let)",
2080 "Identifier",
2081 1
2082 ),
2083 invalid("let s = `${(v)}`", "let s = `${v}`", "Identifier"),
2084 invalid("let s = `${(a, b)}`", "let s = `${a, b}`", "SequenceExpression"),
2085 invalid("function foo(a = (b)) {}", "function foo(a = b) {}", "Identifier"),
2086 invalid("const bar = (a = (b)) => a", "const bar = (a = b) => a", "Identifier"),
2087 invalid("const [a = (b)] = []", "const [a = b] = []", "Identifier"),
2088 invalid("const {a = (b)} = {}", "const {a = b} = {}", "Identifier"),
2089
2090 // LHS of assignments/Assignment targets
2091 invalid("(a) = b", "a = b", "Identifier"),
2092 invalid("(a.b) = c", "a.b = c", "MemberExpression"),
2093 invalid("(a) += b", "a += b", "Identifier"),
2094 invalid("(a.b) >>= c", "a.b >>= c", "MemberExpression"),
2095 invalid("[(a) = b] = []", "[a = b] = []", "Identifier"),
2096 invalid("[(a.b) = c] = []", "[a.b = c] = []", "MemberExpression"),
2097 invalid("({ a: (b) = c } = {})", "({ a: b = c } = {})", "Identifier"),
2098 invalid("({ a: (b.c) = d } = {})", "({ a: b.c = d } = {})", "MemberExpression"),
2099 invalid("[(a)] = []", "[a] = []", "Identifier"),
2100 invalid("[(a.b)] = []", "[a.b] = []", "MemberExpression"),
2101 invalid("[,(a),,] = []", "[,a,,] = []", "Identifier"),
2102 invalid("[...(a)] = []", "[...a] = []", "Identifier"),
2103 invalid("[...(a.b)] = []", "[...a.b] = []", "MemberExpression"),
2104 invalid("({ a: (b) } = {})", "({ a: b } = {})", "Identifier"),
2105 invalid("({ a: (b.c) } = {})", "({ a: b.c } = {})", "MemberExpression"),
2106
2107 /*
2108 * TODO: Add these tests for RestElement's parenthesized arguments in object patterns when that becomes supported by Espree.
2109 *
2110 * invalid("({ ...(a) } = {})", "({ ...a } = {})", "Identifier"),
2111 * invalid("({ ...(a.b) } = {})", "({ ...a.b } = {})", "MemberExpression")
2112 */
2113
2114 // https://github.com/eslint/eslint/issues/11706 (also in valid[])
2115 {
2116 code: "for ((a = (b in c)); ;);",
2117 output: "for ((a = b in c); ;);",
2118 errors: [
2119 {
2120 messageId: "unexpected"
2121 }
2122 ]
2123 },
2124 {
2125 code: "for (let a = ((b in c) && (d in e)); ;);",
2126 output: "for (let a = (b in c && d in e); ;);",
2127 errors: Array(2).fill(
2128 {
2129 messageId: "unexpected"
2130 }
2131 )
2132 },
2133 {
2134 code: "for (let a = ((b in c) in d); ;);",
2135 output: "for (let a = (b in c in d); ;);",
2136 errors: [
2137 {
2138 messageId: "unexpected"
2139 }
2140 ]
2141 },
2142 {
2143 code: "for (let a = (b && (c in d)), e = (f in g); ;);",
2144 output: "for (let a = (b && c in d), e = (f in g); ;);",
2145 errors: [
2146 {
2147 messageId: "unexpected"
2148 }
2149 ]
2150 },
2151 {
2152 code: "for (let a = (b + c), d = (e in f); ;);",
2153 output: "for (let a = b + c, d = (e in f); ;);",
2154 errors: [
2155 {
2156 messageId: "unexpected"
2157 }
2158 ]
2159 },
2160 {
2161 code: "for (let a = [(b in c)]; ;);",
2162 output: "for (let a = [b in c]; ;);",
2163 errors: [
2164 {
2165 messageId: "unexpected"
2166 }
2167 ]
2168 },
2169 {
2170 code: "for (let a = [b, (c in d)]; ;);",
2171 output: "for (let a = [b, c in d]; ;);",
2172 errors: [
2173 {
2174 messageId: "unexpected"
2175 }
2176 ]
2177 },
2178 {
2179 code: "for (let a = ([b in c]); ;);",
2180 output: "for (let a = [b in c]; ;);",
2181 errors: [
2182 {
2183 messageId: "unexpected"
2184 }
2185 ]
2186 },
2187 {
2188 code: "for (let a = ([b, c in d]); ;);",
2189 output: "for (let a = [b, c in d]; ;);",
2190 errors: [
2191 {
2192 messageId: "unexpected"
2193 }
2194 ]
2195 },
2196 {
2197 code: "for ((a = [b in c]); ;);",
2198 output: "for (a = [b in c]; ;);",
2199 errors: [
2200 {
2201 messageId: "unexpected"
2202 }
2203 ]
2204 },
2205 {
2206 code: "for (let a = [b && (c in d)]; ;);",
2207 output: "for (let a = [b && c in d]; ;);",
2208 errors: [
2209 {
2210 messageId: "unexpected"
2211 }
2212 ]
2213 },
2214 {
2215 code: "for (let a = [(b && c in d)]; ;);",
2216 output: "for (let a = [b && c in d]; ;);",
2217 errors: [
2218 {
2219 messageId: "unexpected"
2220 }
2221 ]
2222 },
2223 {
2224 code: "for (let a = ([b && c in d]); ;);",
2225 output: "for (let a = [b && c in d]; ;);",
2226 errors: [
2227 {
2228 messageId: "unexpected"
2229 }
2230 ]
2231 },
2232 {
2233 code: "for ((a = [b && c in d]); ;);",
2234 output: "for (a = [b && c in d]; ;);",
2235 errors: [
2236 {
2237 messageId: "unexpected"
2238 }
2239 ]
2240 },
2241 {
2242 code: "for ([(a in b)]; ;);",
2243 output: "for ([a in b]; ;);",
2244 errors: [
2245 {
2246 messageId: "unexpected"
2247 }
2248 ]
2249 },
2250 {
2251 code: "for (([a in b]); ;);",
2252 output: "for ([a in b]; ;);",
2253 errors: [
2254 {
2255 messageId: "unexpected"
2256 }
2257 ]
2258 },
2259 {
2260 code: "for (let a = [(b in c)], d = (e in f); ;);",
2261 output: "for (let a = [b in c], d = (e in f); ;);",
2262 errors: [
2263 {
2264 messageId: "unexpected"
2265 }
2266 ]
2267 },
2268 {
2269 code: "for (let [a = (b in c)] = []; ;);",
2270 output: "for (let [a = b in c] = []; ;);",
2271 errors: [
2272 {
2273 messageId: "unexpected"
2274 }
2275 ]
2276 },
2277 {
2278 code: "for (let [a = b && (c in d)] = []; ;);",
2279 output: "for (let [a = b && c in d] = []; ;);",
2280 errors: [
2281 {
2282 messageId: "unexpected"
2283 }
2284 ]
2285 },
2286 {
2287 code: "for (let a = () => { (b in c) }; ;);",
2288 output: "for (let a = () => { b in c }; ;);",
2289 errors: [
2290 {
2291 messageId: "unexpected"
2292 }
2293 ]
2294 },
2295 {
2296 code: "for (let a = () => { a && (b in c) }; ;);",
2297 output: "for (let a = () => { a && b in c }; ;);",
2298 errors: [
2299 {
2300 messageId: "unexpected"
2301 }
2302 ]
2303 },
2304 {
2305 code: "for (let a = function () { (b in c) }; ;);",
2306 output: "for (let a = function () { b in c }; ;);",
2307 errors: [
2308 {
2309 messageId: "unexpected"
2310 }
2311 ]
2312 },
2313 {
2314 code: "for (let a = { a: (b in c) }; ;);",
2315 output: "for (let a = { a: b in c }; ;);",
2316 errors: [
2317 {
2318 messageId: "unexpected"
2319 }
2320 ]
2321 },
2322 {
2323 code: "for (let a = { a: b && (c in d) }; ;);",
2324 output: "for (let a = { a: b && c in d }; ;);",
2325 errors: [
2326 {
2327 messageId: "unexpected"
2328 }
2329 ]
2330 },
2331 {
2332 code: "for (let { a = (b in c) } = {}; ;);",
2333 output: "for (let { a = b in c } = {}; ;);",
2334 errors: [
2335 {
2336 messageId: "unexpected"
2337 }
2338 ]
2339 },
2340 {
2341 code: "for (let { a = b && (c in d) } = {}; ;);",
2342 output: "for (let { a = b && c in d } = {}; ;);",
2343 errors: [
2344 {
2345 messageId: "unexpected"
2346 }
2347 ]
2348 },
2349 {
2350 code: "for (let { a: { b = c && (d in e) } } = {}; ;);",
2351 output: "for (let { a: { b = c && d in e } } = {}; ;);",
2352 errors: [
2353 {
2354 messageId: "unexpected"
2355 }
2356 ]
2357 },
2358 {
2359 code: "for (let a = `${(a in b)}`; ;);",
2360 output: "for (let a = `${a in b}`; ;);",
2361 errors: [
2362 {
2363 messageId: "unexpected"
2364 }
2365 ]
2366 },
2367 {
2368 code: "for (let a = `${a && (b in c)}`; ;);",
2369 output: "for (let a = `${a && b in c}`; ;);",
2370 errors: [
2371 {
2372 messageId: "unexpected"
2373 }
2374 ]
2375 },
2376 {
2377 code: "for (let a = (b = (c in d)) => {}; ;);",
2378 output: "for (let a = (b = c in d) => {}; ;);",
2379 errors: [
2380 {
2381 messageId: "unexpected"
2382 }
2383 ]
2384 },
2385 {
2386 code: "for (let a = (b = c && (d in e)) => {}; ;);",
2387 output: "for (let a = (b = c && d in e) => {}; ;);",
2388 errors: [
2389 {
2390 messageId: "unexpected"
2391 }
2392 ]
2393 },
2394 {
2395 code: "for (let a = (b, c = d && (e in f)) => {}; ;);",
2396 output: "for (let a = (b, c = d && e in f) => {}; ;);",
2397 errors: [
2398 {
2399 messageId: "unexpected"
2400 }
2401 ]
2402 },
2403 {
2404 code: "for (let a = function (b = c && (d in e)) {}; ;);",
2405 output: "for (let a = function (b = c && d in e) {}; ;);",
2406 errors: [
2407 {
2408 messageId: "unexpected"
2409 }
2410 ]
2411 },
2412 {
2413 code: "for (let a = function (b, c = d && (e in f)) {}; ;);",
2414 output: "for (let a = function (b, c = d && e in f) {}; ;);",
2415 errors: [
2416 {
2417 messageId: "unexpected"
2418 }
2419 ]
2420 },
2421 {
2422 code: "for (let a = b((c in d)); ;);",
2423 output: "for (let a = b(c in d); ;);",
2424 errors: [
2425 {
2426 messageId: "unexpected"
2427 }
2428 ]
2429 },
2430 {
2431 code: "for (let a = b(c, (d in e)); ;);",
2432 output: "for (let a = b(c, d in e); ;);",
2433 errors: [
2434 {
2435 messageId: "unexpected"
2436 }
2437 ]
2438 },
2439 {
2440 code: "for (let a = b(c && (d in e)); ;);",
2441 output: "for (let a = b(c && d in e); ;);",
2442 errors: [
2443 {
2444 messageId: "unexpected"
2445 }
2446 ]
2447 },
2448 {
2449 code: "for (let a = b(c, d && (e in f)); ;);",
2450 output: "for (let a = b(c, d && e in f); ;);",
2451 errors: [
2452 {
2453 messageId: "unexpected"
2454 }
2455 ]
2456 },
2457 {
2458 code: "for (let a = new b((c in d)); ;);",
2459 output: "for (let a = new b(c in d); ;);",
2460 errors: [
2461 {
2462 messageId: "unexpected"
2463 }
2464 ]
2465 },
2466 {
2467 code: "for (let a = new b(c, (d in e)); ;);",
2468 output: "for (let a = new b(c, d in e); ;);",
2469 errors: [
2470 {
2471 messageId: "unexpected"
2472 }
2473 ]
2474 },
2475 {
2476 code: "for (let a = new b(c && (d in e)); ;);",
2477 output: "for (let a = new b(c && d in e); ;);",
2478 errors: [
2479 {
2480 messageId: "unexpected"
2481 }
2482 ]
2483 },
2484 {
2485 code: "for (let a = new b(c, d && (e in f)); ;);",
2486 output: "for (let a = new b(c, d && e in f); ;);",
2487 errors: [
2488 {
2489 messageId: "unexpected"
2490 }
2491 ]
2492 },
2493 {
2494 code: "for (let a = b[(c in d)]; ;);",
2495 output: "for (let a = b[c in d]; ;);",
2496 errors: [
2497 {
2498 messageId: "unexpected"
2499 }
2500 ]
2501 },
2502 {
2503 code: "for (let a = b[c && (d in e)]; ;);",
2504 output: "for (let a = b[c && d in e]; ;);",
2505 errors: [
2506 {
2507 messageId: "unexpected"
2508 }
2509 ]
2510 },
2511 {
2512 code: "for (let a = b ? (c in d) : e; ;);",
2513 output: "for (let a = b ? c in d : e; ;);",
2514 errors: [
2515 {
2516 messageId: "unexpected"
2517 }
2518 ]
2519 },
2520 {
2521 code: "for (let a = b ? c && (d in e) : f; ;);",
2522 output: "for (let a = b ? c && d in e : f; ;);",
2523 errors: [
2524 {
2525 messageId: "unexpected"
2526 }
2527 ]
2528 },
2529 {
2530 code: "for (a ? b && (c in d) : e; ;);",
2531 output: "for (a ? b && c in d : e; ;);",
2532 errors: [
2533 {
2534 messageId: "unexpected"
2535 }
2536 ]
2537 },
2538 {
2539 code: "for (let a = ((b in c)); ;);",
2540 output: "for (let a = (b in c); ;);",
2541 errors: [
2542 {
2543 messageId: "unexpected"
2544 }
2545 ]
2546 },
2547 {
2548 code: "for (((a in b)); ;);",
2549 output: "for ((a in b); ;);",
2550 errors: [
2551 {
2552 messageId: "unexpected"
2553 }
2554 ]
2555 },
2556 {
2557 code: "for (((a && b in c && d)); ;);",
2558 output: "for ((a && b in c && d); ;);",
2559 errors: [
2560 {
2561 messageId: "unexpected"
2562 }
2563 ]
2564 },
2565 {
2566 code: "for (let a = (!(b in c)); ;);",
2567 output: "for (let a = !(b in c); ;);",
2568 errors: [
2569 {
2570 messageId: "unexpected"
2571 }
2572 ]
2573 },
2574 {
2575 code: "for (let a = (!(b && c in d)); ;);",
2576 output: "for (let a = !(b && c in d); ;);",
2577 errors: [
2578 {
2579 messageId: "unexpected"
2580 }
2581 ]
2582 },
2583 {
2584 code: "for (let a = !((b in c) && (d in e)); ;);",
2585 output: "for (let a = !(b in c && d in e); ;);",
2586 errors: Array(2).fill(
2587 {
2588 messageId: "unexpected"
2589 }
2590 )
2591 },
2592 {
2593 code: "for (let a = (x && (b in c)), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2594 output: "for (let a = (x && b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2595 errors: [
2596 {
2597 messageId: "unexpected"
2598 }
2599 ]
2600 },
2601 {
2602 code: "for (let a = (b in c), d = () => { for ((x && (e in f)); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2603 output: "for (let a = (b in c), d = () => { for ((x && e in f); ;); for ((g in h); ;); }; ;); for((i in j); ;);",
2604 errors: [
2605 {
2606 messageId: "unexpected"
2607 }
2608 ]
2609 },
2610 {
2611 code: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((x && (g in h)); ;); }; ;); for((i in j); ;);",
2612 output: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((x && g in h); ;); }; ;); for((i in j); ;);",
2613 errors: [
2614 {
2615 messageId: "unexpected"
2616 }
2617 ]
2618 },
2619 {
2620 code: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((x && (i in j)); ;);",
2621 output: "for (let a = (b in c), d = () => { for ((e in f); ;); for ((g in h); ;); }; ;); for((x && i in j); ;);",
2622 errors: [
2623 {
2624 messageId: "unexpected"
2625 }
2626 ]
2627 },
2628 {
2629 code: "for (let a = (x && (b in c)), d = () => { for ((e in f); ;); for ((y && (g in h)); ;); }; ;); for((i in j); ;);",
2630 output: "for (let a = (x && b in c), d = () => { for ((e in f); ;); for ((y && g in h); ;); }; ;); for((i in j); ;);",
2631 errors: Array(2).fill(
2632 {
2633 messageId: "unexpected"
2634 }
2635 )
2636 },
2637 {
2638 code: "for (let a = (x && (b in c)), d = () => { for ((y && (e in f)); ;); for ((z && (g in h)); ;); }; ;); for((w && (i in j)); ;);",
2639 output: "for (let a = (x && b in c), d = () => { for ((y && e in f); ;); for ((z && g in h); ;); }; ;); for((w && i in j); ;);",
2640 errors: Array(4).fill(
2641 {
2642 messageId: "unexpected"
2643 }
2644 )
2645 },
2646
2647 // https://github.com/eslint/eslint/issues/11706 regression tests (also in valid[])
2648 {
2649 code: "for (let a = (b); a > (b); a = (b)) a = (b); a = (b);",
2650 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2651 errors: Array(5).fill(
2652 {
2653 messageId: "unexpected"
2654 }
2655 )
2656 },
2657 {
2658 code: "for ((a = b); (a > b); (a = b)) (a = b); (a = b);",
2659 output: "for (a = b; a > b; a = b) a = b; a = b;",
2660 errors: Array(5).fill(
2661 {
2662 messageId: "unexpected"
2663 }
2664 )
2665 },
2666 {
2667 code: "for (let a = b; a > (b); a = (b)) a = (b); a = (b);",
2668 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2669 errors: Array(4).fill(
2670 {
2671 messageId: "unexpected"
2672 }
2673 )
2674 },
2675 {
2676 code: "for (let a = b; (a > b); (a = b)) (a = b); (a = b);",
2677 output: "for (let a = b; a > b; a = b) a = b; a = b;",
2678 errors: Array(4).fill(
2679 {
2680 messageId: "unexpected"
2681 }
2682 )
2683 },
2684 {
2685 code: "for (; a > (b); a = (b)) a = (b); a = (b);",
2686 output: "for (; a > b; a = b) a = b; a = b;",
2687 errors: Array(4).fill(
2688 {
2689 messageId: "unexpected"
2690 }
2691 )
2692 },
2693 {
2694 code: "for (; (a > b); (a = b)) (a = b); (a = b);",
2695 output: "for (; a > b; a = b) a = b; a = b;",
2696 errors: Array(4).fill(
2697 {
2698 messageId: "unexpected"
2699 }
2700 )
2701 },
2702 {
2703 code: "for (let a = (b); a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2704 output: "for (let a = b; a = b in c; a = b in c) a = b in c; a = b in c;",
2705 errors: Array(5).fill(
2706 {
2707 messageId: "unexpected"
2708 }
2709 )
2710 },
2711 {
2712 code: "for (let a = (b); (a in b); (a in b)) (a in b); (a in b);",
2713 output: "for (let a = b; a in b; a in b) a in b; a in b;",
2714 errors: Array(5).fill(
2715 {
2716 messageId: "unexpected"
2717 }
2718 )
2719 },
2720 {
2721 code: "for (let a = b; a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2722 output: "for (let a = b; a = b in c; a = b in c) a = b in c; a = b in c;",
2723 errors: Array(4).fill(
2724 {
2725 messageId: "unexpected"
2726 }
2727 )
2728 },
2729 {
2730 code: "for (let a = b; (a in b); (a in b)) (a in b); (a in b);",
2731 output: "for (let a = b; a in b; a in b) a in b; a in b;",
2732 errors: Array(4).fill(
2733 {
2734 messageId: "unexpected"
2735 }
2736 )
2737 },
2738 {
2739 code: "for (; a = (b in c); a = (b in c)) a = (b in c); a = (b in c);",
2740 output: "for (; a = b in c; a = b in c) a = b in c; a = b in c;",
2741 errors: Array(4).fill(
2742 {
2743 messageId: "unexpected"
2744 }
2745 )
2746 },
2747 {
2748 code: "for (; (a in b); (a in b)) (a in b); (a in b);",
2749 output: "for (; a in b; a in b) a in b; a in b;",
2750 errors: Array(4).fill(
2751 {
2752 messageId: "unexpected"
2753 }
2754 )
2755 },
2756 {
2757 code: "for (let a = (b + c), d = () => { for ((e + f); ;); for ((g + h); ;); }; ;); for((i + j); ;);",
2758 output: "for (let a = b + c, d = () => { for (e + f; ;); for (g + h; ;); }; ;); for(i + j; ;);",
2759 errors: Array(4).fill(
2760 {
2761 messageId: "unexpected"
2762 }
2763 )
2764 },
2765
2766 // import expressions
2767 invalid(
2768 "import((source))",
2769 "import(source)",
2770 "Identifier",
2771 1,
2772 { parserOptions: { ecmaVersion: 2020 } }
2773 ),
2774 invalid(
2775 "import((source = 'foo.js'))",
2776 "import(source = 'foo.js')",
2777 "AssignmentExpression",
2778 1,
2779 { parserOptions: { ecmaVersion: 2020 } }
2780 ),
2781 invalid(
2782 "import(((s,t)))",
2783 "import((s,t))",
2784 "SequenceExpression",
2785 1,
2786 { parserOptions: { ecmaVersion: 2020 } }
2787 ),
2788
2789 // https://github.com/eslint/eslint/issues/12127
2790 {
2791 code: "[1, ((2, 3))];",
2792 output: "[1, (2, 3)];",
2793 errors: [{ messageId: "unexpected" }]
2794 },
2795 {
2796 code: "const foo = () => ((bar, baz));",
2797 output: "const foo = () => (bar, baz);",
2798 errors: [{ messageId: "unexpected" }]
2799 },
2800 {
2801 code: "foo = ((bar, baz));",
2802 output: "foo = (bar, baz);",
2803 errors: [{ messageId: "unexpected" }]
2804 },
2805 {
2806 code: "foo + ((bar + baz));",
2807 output: "foo + (bar + baz);",
2808 errors: [{ messageId: "unexpected" }]
2809 },
2810 {
2811 code: "((foo + bar)) + baz;",
2812 output: "(foo + bar) + baz;",
2813 errors: [{ messageId: "unexpected" }]
2814 },
2815 {
2816 code: "foo * ((bar + baz));",
2817 output: "foo * (bar + baz);",
2818 errors: [{ messageId: "unexpected" }]
2819 },
2820 {
2821 code: "((foo + bar)) * baz;",
2822 output: "(foo + bar) * baz;",
2823 errors: [{ messageId: "unexpected" }]
2824 },
2825 {
2826 code: "new A(((foo, bar)))",
2827 output: "new A((foo, bar))",
2828 errors: [{ messageId: "unexpected" }]
2829 },
2830 {
2831 code: "class A{ [((foo, bar))]() {} }",
2832 output: "class A{ [(foo, bar)]() {} }",
2833 errors: [{ messageId: "unexpected" }]
2834 },
2835 {
2836 code: "new ((A, B))()",
2837 output: "new (A, B)()",
2838 errors: [{ messageId: "unexpected" }]
2839 },
2840 {
2841 code: "((foo, bar)) ? bar : baz;",
2842 output: "(foo, bar) ? bar : baz;",
2843 errors: [{ messageId: "unexpected" }]
2844 },
2845 {
2846 code: "((f ? o : o)) ? bar : baz;",
2847 output: "(f ? o : o) ? bar : baz;",
2848 errors: [{ messageId: "unexpected" }]
2849 },
2850 {
2851 code: "((f = oo)) ? bar : baz;",
2852 output: "(f = oo) ? bar : baz;",
2853 errors: [{ messageId: "unexpected" }]
2854 },
2855 {
2856 code: "foo ? ((bar, baz)) : baz;",
2857 output: "foo ? (bar, baz) : baz;",
2858 errors: [{ messageId: "unexpected" }]
2859 },
2860 {
2861 code: "foo ? bar : ((bar, baz));",
2862 output: "foo ? bar : (bar, baz);",
2863 errors: [{ messageId: "unexpected" }]
2864 },
2865 {
2866 code: "function foo(bar = ((baz1, baz2))) {}",
2867 output: "function foo(bar = (baz1, baz2)) {}",
2868 errors: [{ messageId: "unexpected" }]
2869 },
2870 {
2871 code: "var foo = { bar: ((baz1, baz2)) };",
2872 output: "var foo = { bar: (baz1, baz2) };",
2873 errors: [{ messageId: "unexpected" }]
2874 },
2875 {
2876 code: "var foo = { [((bar1, bar2))]: baz };",
2877 output: "var foo = { [(bar1, bar2)]: baz };",
2878 errors: [{ messageId: "unexpected" }]
2879 },
2880
2881 // adjacent tokens tests for division operator, comments and regular expressions
2882 invalid("a+/**/(/**/b)", "a+/**//**/b", "Identifier"),
2883 invalid("a+/**/(//\nb)", "a+/**///\nb", "Identifier"),
2884 invalid("a in(/**/b)", "a in/**/b", "Identifier"),
2885 invalid("a in(//\nb)", "a in//\nb", "Identifier"),
2886 invalid("a+(/**/b)", "a+/**/b", "Identifier"),
2887 invalid("a+/**/(b)", "a+/**/b", "Identifier"),
2888 invalid("a+(//\nb)", "a+//\nb", "Identifier"),
2889 invalid("a+//\n(b)", "a+//\nb", "Identifier"),
2890 invalid("a+(/^b$/)", "a+/^b$/", "Literal"),
2891 invalid("a/(/**/b)", "a/ /**/b", "Identifier"),
2892 invalid("a/(//\nb)", "a/ //\nb", "Identifier"),
2893 invalid("a/(/^b$/)", "a/ /^b$/", "Literal"),
2894
2895
2896 // Nullish coalescing
2897 {
2898 code: "var v = ((a ?? b)) || c",
2899 output: "var v = (a ?? b) || c",
2900 parserOptions: { ecmaVersion: 2020 },
2901 errors: [{ messageId: "unexpected" }]
2902 },
2903 {
2904 code: "var v = a ?? ((b || c))",
2905 output: "var v = a ?? (b || c)",
2906 parserOptions: { ecmaVersion: 2020 },
2907 errors: [{ messageId: "unexpected" }]
2908 },
2909 {
2910 code: "var v = ((a ?? b)) && c",
2911 output: "var v = (a ?? b) && c",
2912 parserOptions: { ecmaVersion: 2020 },
2913 errors: [{ messageId: "unexpected" }]
2914 },
2915 {
2916 code: "var v = a ?? ((b && c))",
2917 output: "var v = a ?? (b && c)",
2918 parserOptions: { ecmaVersion: 2020 },
2919 errors: [{ messageId: "unexpected" }]
2920 },
2921 {
2922 code: "var v = ((a || b)) ?? c",
2923 output: "var v = (a || b) ?? c",
2924 parserOptions: { ecmaVersion: 2020 },
2925 errors: [{ messageId: "unexpected" }]
2926 },
2927 {
2928 code: "var v = a || ((b ?? c))",
2929 output: "var v = a || (b ?? c)",
2930 parserOptions: { ecmaVersion: 2020 },
2931 errors: [{ messageId: "unexpected" }]
2932 },
2933 {
2934 code: "var v = ((a && b)) ?? c",
2935 output: "var v = (a && b) ?? c",
2936 parserOptions: { ecmaVersion: 2020 },
2937 errors: [{ messageId: "unexpected" }]
2938 },
2939 {
2940 code: "var v = a && ((b ?? c))",
2941 output: "var v = a && (b ?? c)",
2942 parserOptions: { ecmaVersion: 2020 },
2943 errors: [{ messageId: "unexpected" }]
2944 },
2945 {
2946 code: "var v = (a ?? b) ? b : c",
2947 output: "var v = a ?? b ? b : c",
2948 parserOptions: { ecmaVersion: 2020 },
2949 errors: [{ messageId: "unexpected" }]
2950 },
2951 {
2952 code: "var v = (a | b) ?? c | d",
2953 output: "var v = a | b ?? c | d",
2954 parserOptions: { ecmaVersion: 2020 },
2955 errors: [{ messageId: "unexpected" }]
2956 },
2957 {
2958 code: "var v = a | b ?? (c | d)",
2959 output: "var v = a | b ?? c | d",
2960 parserOptions: { ecmaVersion: 2020 },
2961 errors: [{ messageId: "unexpected" }]
2962 },
2963
2964 // Optional chaining
2965 {
2966 code: "var v = (obj?.aaa)?.aaa",
2967 output: "var v = obj?.aaa?.aaa",
2968 parserOptions: { ecmaVersion: 2020 },
2969 errors: [{ messageId: "unexpected" }]
2970 },
2971 {
2972 code: "var v = (obj.aaa)?.aaa",
2973 output: "var v = obj.aaa?.aaa",
2974 parserOptions: { ecmaVersion: 2020 },
2975 errors: [{ messageId: "unexpected" }]
2976 },
2977 {
2978 code: "var foo = (function(){})?.call()",
2979 output: "var foo = function(){}?.call()",
2980 options: ["all", { enforceForFunctionPrototypeMethods: true }],
2981 parserOptions: { ecmaVersion: 2020 },
2982 errors: [{ messageId: "unexpected" }]
2983 },
2984 {
2985 code: "var foo = (function(){}?.call())",
2986 output: "var foo = function(){}?.call()",
2987 options: ["all", { enforceForFunctionPrototypeMethods: true }],
2988 parserOptions: { ecmaVersion: 2020 },
2989 errors: [{ messageId: "unexpected" }]
2990 }
2991 ]
2992 });