@@ -64,6 +64,137 @@ ruleTester.run("wrap-iife", rule, {
64
64
code : "var a = function(){return 1;};" ,
65
65
options : [ "any" ]
66
66
} ,
67
+ {
68
+ code : "var a = ((function(){return 1;})());" , // always allows existing extra parens (parens both inside and outside)
69
+ options : [ "any" ]
70
+ } ,
71
+ {
72
+ code : "var a = ((function(){return 1;})());" , // always allows existing extra parens (parens both inside and outside)
73
+ options : [ "inside" ]
74
+ } ,
75
+ {
76
+ code : "var a = ((function(){return 1;})());" , // always allows existing extra parens (parens both inside and outside)
77
+ options : [ "outside" ]
78
+ } ,
79
+ {
80
+ code : "if (function (){}()) {}" ,
81
+ options : [ "any" ]
82
+ } ,
83
+ {
84
+ code : "while (function (){}()) {}" ,
85
+ options : [ "any" ]
86
+ } ,
87
+ {
88
+ code : "do {} while (function (){}())" ,
89
+ options : [ "any" ]
90
+ } ,
91
+ {
92
+ code : "switch (function (){}()) {}" ,
93
+ options : [ "any" ]
94
+ } ,
95
+ {
96
+ code : "with (function (){}()) {}" ,
97
+ options : [ "any" ]
98
+ } ,
99
+ {
100
+ code : "foo(function (){}());" ,
101
+ options : [ "any" ]
102
+ } ,
103
+ {
104
+ code : "new foo(function (){}());" ,
105
+ options : [ "any" ]
106
+ } ,
107
+ {
108
+ code : "import(function (){}());" ,
109
+ options : [ "any" ] ,
110
+ parserOptions : { ecmaVersion : 2020 }
111
+ } ,
112
+ {
113
+ code : "if ((function (){})()) {}" ,
114
+ options : [ "any" ]
115
+ } ,
116
+ {
117
+ code : "while (((function (){})())) {}" ,
118
+ options : [ "any" ]
119
+ } ,
120
+ {
121
+ code : "if (function (){}()) {}" ,
122
+ options : [ "outside" ]
123
+ } ,
124
+ {
125
+ code : "while (function (){}()) {}" ,
126
+ options : [ "outside" ]
127
+ } ,
128
+ {
129
+ code : "do {} while (function (){}())" ,
130
+ options : [ "outside" ]
131
+ } ,
132
+ {
133
+ code : "switch (function (){}()) {}" ,
134
+ options : [ "outside" ]
135
+ } ,
136
+ {
137
+ code : "with (function (){}()) {}" ,
138
+ options : [ "outside" ]
139
+ } ,
140
+ {
141
+ code : "foo(function (){}());" ,
142
+ options : [ "outside" ]
143
+ } ,
144
+ {
145
+ code : "new foo(function (){}());" ,
146
+ options : [ "outside" ]
147
+ } ,
148
+ {
149
+ code : "import(function (){}());" ,
150
+ options : [ "outside" ] ,
151
+ parserOptions : { ecmaVersion : 2020 }
152
+ } ,
153
+ {
154
+ code : "if ((function (){})()) {}" ,
155
+ options : [ "outside" ]
156
+ } ,
157
+ {
158
+ code : "while (((function (){})())) {}" ,
159
+ options : [ "outside" ]
160
+ } ,
161
+ {
162
+ code : "if ((function (){})()) {}" ,
163
+ options : [ "inside" ]
164
+ } ,
165
+ {
166
+ code : "while ((function (){})()) {}" ,
167
+ options : [ "inside" ]
168
+ } ,
169
+ {
170
+ code : "do {} while ((function (){})())" ,
171
+ options : [ "inside" ]
172
+ } ,
173
+ {
174
+ code : "switch ((function (){})()) {}" ,
175
+ options : [ "inside" ]
176
+ } ,
177
+ {
178
+ code : "with ((function (){})()) {}" ,
179
+ options : [ "inside" ]
180
+ } ,
181
+ {
182
+ code : "foo((function (){})());" ,
183
+ options : [ "inside" ]
184
+ } ,
185
+ {
186
+ code : "new foo((function (){})());" ,
187
+ options : [ "inside" ]
188
+ } ,
189
+ {
190
+ code : "import((function (){})());" ,
191
+ options : [ "inside" ] ,
192
+ parserOptions : { ecmaVersion : 2020 }
193
+ } ,
194
+ {
195
+ code : "while (((function (){})())) {}" ,
196
+ options : [ "inside" ]
197
+ } ,
67
198
{
68
199
code : "window.bar = (function() { return 3; }.call(this, arg1));" ,
69
200
options : [ "outside" , { functionPrototypeMethods : true } ]
@@ -84,6 +215,10 @@ ruleTester.run("wrap-iife", rule, {
84
215
code : "window.bar = function() { return 3; }.call(this, arg1);" ,
85
216
options : [ "inside" ]
86
217
} ,
218
+ {
219
+ code : "window.bar = function() { return 3; }.call(this, arg1);" ,
220
+ options : [ "inside" , { } ]
221
+ } ,
87
222
{
88
223
code : "window.bar = function() { return 3; }.call(this, arg1);" ,
89
224
options : [ "inside" , { functionPrototypeMethods : false } ]
@@ -107,6 +242,137 @@ ruleTester.run("wrap-iife", rule, {
107
242
{
108
243
code : "var a = function(){return 1;}.bind(this).apply(that);" ,
109
244
options : [ "inside" , { functionPrototypeMethods : true } ]
245
+ } ,
246
+ {
247
+ code : "var a = ((function(){return 1;}).call());" , // always allows existing extra parens (parens both inside and outside)
248
+ options : [ "any" , { functionPrototypeMethods : true } ]
249
+ } ,
250
+ {
251
+ code : "var a = ((function(){return 1;}).call());" , // always allows existing extra parens (parens both inside and outside)
252
+ options : [ "inside" , { functionPrototypeMethods : true } ]
253
+ } ,
254
+ {
255
+ code : "var a = ((function(){return 1;}).call());" , // always allows existing extra parens (parens both inside and outside)
256
+ options : [ "outside" , { functionPrototypeMethods : true } ]
257
+ } ,
258
+ {
259
+ code : "if (function (){}.call()) {}" ,
260
+ options : [ "any" , { functionPrototypeMethods : true } ]
261
+ } ,
262
+ {
263
+ code : "while (function (){}.call()) {}" ,
264
+ options : [ "any" , { functionPrototypeMethods : true } ]
265
+ } ,
266
+ {
267
+ code : "do {} while (function (){}.call())" ,
268
+ options : [ "any" , { functionPrototypeMethods : true } ]
269
+ } ,
270
+ {
271
+ code : "switch (function (){}.call()) {}" ,
272
+ options : [ "any" , { functionPrototypeMethods : true } ]
273
+ } ,
274
+ {
275
+ code : "with (function (){}.call()) {}" ,
276
+ options : [ "any" , { functionPrototypeMethods : true } ]
277
+ } ,
278
+ {
279
+ code : "foo(function (){}.call())" ,
280
+ options : [ "any" , { functionPrototypeMethods : true } ]
281
+ } ,
282
+ {
283
+ code : "new foo(function (){}.call())" ,
284
+ options : [ "any" , { functionPrototypeMethods : true } ]
285
+ } ,
286
+ {
287
+ code : "import(function (){}.call())" ,
288
+ options : [ "any" , { functionPrototypeMethods : true } ] ,
289
+ parserOptions : { ecmaVersion : 2020 }
290
+ } ,
291
+ {
292
+ code : "if ((function (){}).call()) {}" ,
293
+ options : [ "any" , { functionPrototypeMethods : true } ]
294
+ } ,
295
+ {
296
+ code : "while (((function (){}).call())) {}" ,
297
+ options : [ "any" , { functionPrototypeMethods : true } ]
298
+ } ,
299
+ {
300
+ code : "if (function (){}.call()) {}" ,
301
+ options : [ "outside" , { functionPrototypeMethods : true } ]
302
+ } ,
303
+ {
304
+ code : "while (function (){}.call()) {}" ,
305
+ options : [ "outside" , { functionPrototypeMethods : true } ]
306
+ } ,
307
+ {
308
+ code : "do {} while (function (){}.call())" ,
309
+ options : [ "outside" , { functionPrototypeMethods : true } ]
310
+ } ,
311
+ {
312
+ code : "switch (function (){}.call()) {}" ,
313
+ options : [ "outside" , { functionPrototypeMethods : true } ]
314
+ } ,
315
+ {
316
+ code : "with (function (){}.call()) {}" ,
317
+ options : [ "outside" , { functionPrototypeMethods : true } ]
318
+ } ,
319
+ {
320
+ code : "foo(function (){}.call())" ,
321
+ options : [ "outside" , { functionPrototypeMethods : true } ]
322
+ } ,
323
+ {
324
+ code : "new foo(function (){}.call())" ,
325
+ options : [ "outside" , { functionPrototypeMethods : true } ]
326
+ } ,
327
+ {
328
+ code : "import(function (){}.call())" ,
329
+ options : [ "outside" , { functionPrototypeMethods : true } ] ,
330
+ parserOptions : { ecmaVersion : 2020 }
331
+ } ,
332
+ {
333
+ code : "if ((function (){}).call()) {}" ,
334
+ options : [ "outside" , { functionPrototypeMethods : true } ]
335
+ } ,
336
+ {
337
+ code : "while (((function (){}).call())) {}" ,
338
+ options : [ "outside" , { functionPrototypeMethods : true } ]
339
+ } ,
340
+ {
341
+ code : "if ((function (){}).call()) {}" ,
342
+ options : [ "inside" , { functionPrototypeMethods : true } ]
343
+ } ,
344
+ {
345
+ code : "while ((function (){}).call()) {}" ,
346
+ options : [ "inside" , { functionPrototypeMethods : true } ]
347
+ } ,
348
+ {
349
+ code : "do {} while ((function (){}).call())" ,
350
+ options : [ "inside" , { functionPrototypeMethods : true } ]
351
+ } ,
352
+ {
353
+ code : "switch ((function (){}).call()) {}" ,
354
+ options : [ "inside" , { functionPrototypeMethods : true } ]
355
+ } ,
356
+ {
357
+ code : "with ((function (){}).call()) {}" ,
358
+ options : [ "inside" , { functionPrototypeMethods : true } ]
359
+ } ,
360
+ {
361
+ code : "foo((function (){}).call())" ,
362
+ options : [ "inside" , { functionPrototypeMethods : true } ]
363
+ } ,
364
+ {
365
+ code : "new foo((function (){}).call())" ,
366
+ options : [ "inside" , { functionPrototypeMethods : true } ]
367
+ } ,
368
+ {
369
+ code : "import((function (){}).call())" ,
370
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
371
+ parserOptions : { ecmaVersion : 2020 }
372
+ } ,
373
+ {
374
+ code : "if (((function (){}).call())) {}" ,
375
+ options : [ "inside" , { functionPrototypeMethods : true } ]
110
376
}
111
377
] ,
112
378
invalid : [
@@ -142,6 +408,79 @@ ruleTester.run("wrap-iife", rule, {
142
408
options : [ "inside" ] ,
143
409
errors : [ wrapExpressionError ]
144
410
} ,
411
+ {
412
+ code : "new foo((function (){}()))" ,
413
+ output : "new foo((function (){})())" ,
414
+ options : [ "inside" ] ,
415
+ errors : [ wrapExpressionError ]
416
+ } ,
417
+ {
418
+ code : "new (function (){}())" ,
419
+ output : "new ((function (){})())" , // wrap function expression, but don't remove necessary grouping parens
420
+ options : [ "inside" ] ,
421
+ errors : [ wrapExpressionError ]
422
+ } ,
423
+ {
424
+ code : "new (function (){}())()" ,
425
+ output : "new ((function (){})())()" , // wrap function expression, but don't remove necessary grouping parens
426
+ options : [ "inside" ] ,
427
+ errors : [ wrapExpressionError ]
428
+ } ,
429
+ {
430
+ code : "if (function (){}()) {}" ,
431
+ output : "if ((function (){})()) {}" , // wrap function expression, but don't remove mandatory parens
432
+ options : [ "inside" ] ,
433
+ errors : [ wrapExpressionError ]
434
+ } ,
435
+ {
436
+ code : "if ((function (){}())) {}" ,
437
+ output : "if ((function (){})()) {}" , // wrap function expression and remove unnecessary grouping parens aroung the call expression
438
+ options : [ "inside" ] ,
439
+ errors : [ wrapExpressionError ]
440
+ } ,
441
+ {
442
+ code : "while (function (){}()) {}" ,
443
+ output : "while ((function (){})()) {}" , // wrap function expression, but don't remove mandatory parens
444
+ options : [ "inside" ] ,
445
+ errors : [ wrapExpressionError ]
446
+ } ,
447
+ {
448
+ code : "do {} while (function (){}())" ,
449
+ output : "do {} while ((function (){})())" , // wrap function expression, but don't remove mandatory parens
450
+ options : [ "inside" ] ,
451
+ errors : [ wrapExpressionError ]
452
+ } ,
453
+ {
454
+ code : "switch (function (){}()) {}" ,
455
+ output : "switch ((function (){})()) {}" , // wrap function expression, but don't remove mandatory parens
456
+ options : [ "inside" ] ,
457
+ errors : [ wrapExpressionError ]
458
+ } ,
459
+ {
460
+ code : "with (function (){}()) {}" ,
461
+ output : "with ((function (){})()) {}" , // wrap function expression, but don't remove mandatory parens
462
+ options : [ "inside" ] ,
463
+ errors : [ wrapExpressionError ]
464
+ } ,
465
+ {
466
+ code : "foo(function (){}())" ,
467
+ output : "foo((function (){})())" , // wrap function expression, but don't remove mandatory parens
468
+ options : [ "inside" ] ,
469
+ errors : [ wrapExpressionError ]
470
+ } ,
471
+ {
472
+ code : "new foo(function (){}())" ,
473
+ output : "new foo((function (){})())" , // wrap function expression, but don't remove mandatory parens
474
+ options : [ "inside" ] ,
475
+ errors : [ wrapExpressionError ]
476
+ } ,
477
+ {
478
+ code : "import(function (){}())" ,
479
+ output : "import((function (){})())" , // wrap function expression, but don't remove mandatory parens
480
+ options : [ "inside" ] ,
481
+ parserOptions : { ecmaVersion : 2020 } ,
482
+ errors : [ wrapExpressionError ]
483
+ } ,
145
484
{
146
485
147
486
// Ensure all comments get preserved when autofixing.
@@ -197,6 +536,73 @@ ruleTester.run("wrap-iife", rule, {
197
536
output : "window.bar = (function() { return 3; }.call(this, arg1));" ,
198
537
options : [ "outside" , { functionPrototypeMethods : true } ] ,
199
538
errors : [ moveInvocationError ]
539
+ } ,
540
+ {
541
+ code : "new (function (){}.call())" ,
542
+ output : "new ((function (){}).call())" , // wrap function expression, but don't remove necessary grouping parens
543
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
544
+ errors : [ wrapExpressionError ]
545
+ } ,
546
+ {
547
+ code : "new (function (){}.call())()" ,
548
+ output : "new ((function (){}).call())()" , // wrap function expression, but don't remove necessary grouping parens
549
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
550
+ errors : [ wrapExpressionError ]
551
+ } ,
552
+ {
553
+ code : "if (function (){}.call()) {}" ,
554
+ output : "if ((function (){}).call()) {}" , // wrap function expression, but don't remove mandatory parens
555
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
556
+ errors : [ wrapExpressionError ]
557
+ } ,
558
+ {
559
+ code : "if ((function (){}.call())) {}" ,
560
+ output : "if ((function (){}).call()) {}" , // wrap function expression and remove unnecessary grouping parens aroung the call expression
561
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
562
+ errors : [ wrapExpressionError ]
563
+ } ,
564
+ {
565
+ code : "while (function (){}.call()) {}" ,
566
+ output : "while ((function (){}).call()) {}" , // wrap function expression, but don't remove mandatory parens
567
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
568
+ errors : [ wrapExpressionError ]
569
+ } ,
570
+ {
571
+ code : "do {} while (function (){}.call())" ,
572
+ output : "do {} while ((function (){}).call())" , // wrap function expression, but don't remove mandatory parens
573
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
574
+ errors : [ wrapExpressionError ]
575
+ } ,
576
+ {
577
+ code : "switch (function (){}.call()) {}" ,
578
+ output : "switch ((function (){}).call()) {}" , // wrap function expression, but don't remove mandatory parens
579
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
580
+ errors : [ wrapExpressionError ]
581
+ } ,
582
+ {
583
+ code : "with (function (){}.call()) {}" ,
584
+ output : "with ((function (){}).call()) {}" , // wrap function expression, but don't remove mandatory parens
585
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
586
+ errors : [ wrapExpressionError ]
587
+ } ,
588
+ {
589
+ code : "foo(function (){}.call())" ,
590
+ output : "foo((function (){}).call())" , // wrap function expression, but don't remove mandatory parens
591
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
592
+ errors : [ wrapExpressionError ]
593
+ } ,
594
+ {
595
+ code : "new foo(function (){}.call())" ,
596
+ output : "new foo((function (){}).call())" , // wrap function expression, but don't remove mandatory parens
597
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
598
+ errors : [ wrapExpressionError ]
599
+ } ,
600
+ {
601
+ code : "import(function (){}.call())" ,
602
+ output : "import((function (){}).call())" , // wrap function expression, but don't remove mandatory parens
603
+ options : [ "inside" , { functionPrototypeMethods : true } ] ,
604
+ parserOptions : { ecmaVersion : 2020 } ,
605
+ errors : [ wrapExpressionError ]
200
606
}
201
607
]
202
608
} ) ;
0 commit comments