@@ -143,10 +143,13 @@ function baz() {
143
143
} )
144
144
145
145
describe ( "invalid" , ( ) => {
146
- for ( const { code , errors , reportUnusedDisableDirectives } of [
146
+ for ( const testCase of [
147
147
{
148
+ title : "Generic same line" ,
148
149
code : `/*eslint no-undef:off*/
149
150
var a = b //eslint-disable-line` ,
151
+ output : `/*eslint no-undef:off*/
152
+ var a = b ` ,
150
153
errors : [
151
154
{
152
155
message :
@@ -159,8 +162,11 @@ var a = b //eslint-disable-line`,
159
162
] ,
160
163
} ,
161
164
{
165
+ title : "Specific same line" ,
162
166
code : `/*eslint no-undef:off*/
163
167
var a = b //eslint-disable-line no-undef` ,
168
+ output : `/*eslint no-undef:off*/
169
+ var a = b ` ,
164
170
errors : [
165
171
{
166
172
message :
@@ -173,6 +179,7 @@ var a = b //eslint-disable-line no-undef`,
173
179
] ,
174
180
} ,
175
181
{
182
+ title : "Multiple in a same line" ,
176
183
code : `/*eslint no-undef:off, no-unused-vars:off*/
177
184
var a = b //eslint-disable-line no-undef,no-unused-vars` ,
178
185
errors : [
@@ -195,8 +202,12 @@ var a = b //eslint-disable-line no-undef,no-unused-vars`,
195
202
] ,
196
203
} ,
197
204
{
205
+ title : "Generic next line" ,
198
206
code : `/*eslint no-undef:off*/
199
207
//eslint-disable-next-line
208
+ var a = b` ,
209
+ output : `/*eslint no-undef:off*/
210
+
200
211
var a = b` ,
201
212
errors : [
202
213
{
@@ -210,8 +221,12 @@ var a = b`,
210
221
] ,
211
222
} ,
212
223
{
224
+ title : "Specific next line" ,
213
225
code : `/*eslint no-undef:off*/
214
226
//eslint-disable-next-line no-undef
227
+ var a = b` ,
228
+ output : `/*eslint no-undef:off*/
229
+
215
230
var a = b` ,
216
231
errors : [
217
232
{
@@ -225,6 +240,7 @@ var a = b`,
225
240
] ,
226
241
} ,
227
242
{
243
+ title : "Multiple next line" ,
228
244
code : `/*eslint no-undef:off, no-unused-vars:off*/
229
245
//eslint-disable-next-line no-undef,no-unused-vars
230
246
var a = b` ,
@@ -248,8 +264,12 @@ var a = b`,
248
264
] ,
249
265
} ,
250
266
{
267
+ title : "Generic block" ,
251
268
code : `/*eslint no-undef:off*/
252
269
/*eslint-disable*/
270
+ var a = b` ,
271
+ output : `/*eslint no-undef:off*/
272
+
253
273
var a = b` ,
254
274
errors : [
255
275
{
@@ -263,8 +283,29 @@ var a = b`,
263
283
] ,
264
284
} ,
265
285
{
286
+ title : "Replaces multi-line block comments with a newline" ,
287
+ code : `foo/* eslint-disable
288
+ */ bar` ,
289
+ output : `foo
290
+ bar` ,
291
+ errors : [
292
+ {
293
+ message :
294
+ "ESLint rules are disabled but never reported." ,
295
+ line : 1 ,
296
+ column : 4 ,
297
+ endLine : 2 ,
298
+ endColumn : 3 ,
299
+ } ,
300
+ ] ,
301
+ } ,
302
+ {
303
+ title : "Specific block" ,
266
304
code : `/*eslint no-undef:off*/
267
305
/*eslint-disable no-undef*/
306
+ var a = b` ,
307
+ output : `/*eslint no-undef:off*/
308
+
268
309
var a = b` ,
269
310
errors : [
270
311
{
@@ -278,6 +319,7 @@ var a = b`,
278
319
] ,
279
320
} ,
280
321
{
322
+ title : "Multiple block" ,
281
323
code : `/*eslint no-undef:off, no-unused-vars:off*/
282
324
/*eslint-disable no-undef,no-unused-vars*/
283
325
var a = b` ,
@@ -301,8 +343,13 @@ var a = b`,
301
343
] ,
302
344
} ,
303
345
{
346
+ title : "Generic block with enable after" ,
304
347
code : `/*eslint no-undef:off*/
305
348
/*eslint-disable*/
349
+ var a = b
350
+ /*eslint-enable*/` ,
351
+ output : `/*eslint no-undef:off*/
352
+
306
353
var a = b
307
354
/*eslint-enable*/` ,
308
355
errors : [
@@ -317,8 +364,13 @@ var a = b
317
364
] ,
318
365
} ,
319
366
{
367
+ title : "Specific block with enable after" ,
320
368
code : `/*eslint no-undef:off*/
321
369
/*eslint-disable no-undef*/
370
+ var a = b
371
+ /*eslint-enable*/` ,
372
+ output : `/*eslint no-undef:off*/
373
+
322
374
var a = b
323
375
/*eslint-enable*/` ,
324
376
errors : [
@@ -333,6 +385,7 @@ var a = b
333
385
] ,
334
386
} ,
335
387
{
388
+ title : "Multiple block with enable after" ,
336
389
code : `/*eslint no-undef:off, no-unused-vars:off*/
337
390
/*eslint-disable no-undef,no-unused-vars*/
338
391
var a = b
@@ -357,8 +410,13 @@ var a = b
357
410
] ,
358
411
} ,
359
412
{
413
+ title : "Generic block disable with no error inside" ,
360
414
code : `/*eslint no-undef:error*/
361
415
/*eslint-disable*/
416
+ /*eslint-enable*/
417
+ var a = b//eslint-disable-line no-undef` ,
418
+ output : `/*eslint no-undef:error*/
419
+
362
420
/*eslint-enable*/
363
421
var a = b//eslint-disable-line no-undef` ,
364
422
errors : [
@@ -373,8 +431,13 @@ var a = b//eslint-disable-line no-undef`,
373
431
] ,
374
432
} ,
375
433
{
434
+ title : "Specific block disable with no error inside" ,
376
435
code : `/*eslint no-undef:error*/
377
436
/*eslint-disable no-undef*/
437
+ /*eslint-enable no-undef*/
438
+ var a = b//eslint-disable-line no-undef` ,
439
+ output : `/*eslint no-undef:error*/
440
+
378
441
/*eslint-enable no-undef*/
379
442
var a = b//eslint-disable-line no-undef` ,
380
443
errors : [
@@ -389,6 +452,7 @@ var a = b//eslint-disable-line no-undef`,
389
452
] ,
390
453
} ,
391
454
{
455
+ title : "Multiple specific block disable with no error inside" ,
392
456
code : `/*eslint no-undef:error, no-unused-vars:error*/
393
457
/*eslint-disable no-undef,no-unused-vars*/
394
458
/*eslint-enable no-undef*/
@@ -405,6 +469,8 @@ var a = b//eslint-disable-line no-undef`,
405
469
] ,
406
470
} ,
407
471
{
472
+ title :
473
+ "Multiple specific block disable with only one error inside" ,
408
474
code : `/*eslint no-undef:error, no-unused-vars:error*/
409
475
/*eslint-disable
410
476
no-undef,
@@ -424,8 +490,10 @@ var a = b
424
490
] ,
425
491
} ,
426
492
{
493
+ title : "Specific block disable at end of input" ,
427
494
code :
428
495
"/* eslint new-parens:error*/ /*eslint-disable new-parens*/" ,
496
+ output : "/* eslint new-parens:error*/ " ,
429
497
errors : [
430
498
{
431
499
message :
@@ -438,8 +506,11 @@ var a = b
438
506
] ,
439
507
} ,
440
508
{
509
+ title : "Generic same line with rule off" ,
441
510
code : `/*eslint no-undef:off*/
442
511
var a = b //eslint-disable-line` ,
512
+ output : `/*eslint no-undef:off*/
513
+ var a = b ` ,
443
514
errors : [
444
515
{
445
516
message :
@@ -457,8 +528,11 @@ var a = b //eslint-disable-line`,
457
528
reportUnusedDisableDirectives : true ,
458
529
} ,
459
530
{
531
+ title : "Specific same line with rule off" ,
460
532
code : `/*eslint no-undef:off*/
461
533
var a = b //eslint-disable-line no-undef` ,
534
+ output : `/*eslint no-undef:off*/
535
+ var a = b ` ,
462
536
errors : [
463
537
{
464
538
message :
@@ -476,8 +550,8 @@ var a = b //eslint-disable-line no-undef`,
476
550
reportUnusedDisableDirectives : true ,
477
551
} ,
478
552
479
- // Don't crash even if the source code has a parse error.
480
553
{
554
+ title : "Don't crash even if the source code has a parse error" ,
481
555
code :
482
556
"/*eslint no-undef:error*/\nvar a = b c //eslint-disable-line no-undef" ,
483
557
errors : [
@@ -487,24 +561,50 @@ var a = b //eslint-disable-line no-undef`,
487
561
] ,
488
562
} ,
489
563
] ) {
490
- it ( code , ( ) =>
491
- runESLint ( code , reportUnusedDisableDirectives ) . then (
492
- actualMessages => {
493
- assert . strictEqual ( actualMessages . length , errors . length )
494
- for ( let i = 0 ; i < errors . length ; ++ i ) {
495
- const actual = actualMessages [ i ]
496
- const expected = errors [ i ]
564
+ it ( testCase . title || testCase . code , ( ) =>
565
+ runESLint (
566
+ testCase . code ,
567
+ testCase . reportUnusedDisableDirectives
568
+ ) . then ( actualMessages => {
569
+ assert . strictEqual (
570
+ actualMessages . length ,
571
+ testCase . errors . length
572
+ )
573
+
574
+ let actualOutput = testCase . code
497
575
498
- for ( const key of Object . keys ( expected ) ) {
499
- assert . strictEqual (
500
- actual [ key ] ,
501
- expected [ key ] ,
502
- `'${ key } ' is not expected.`
503
- )
504
- }
576
+ for ( let i = 0 ; i < testCase . errors . length ; ++ i ) {
577
+ const actual = actualMessages [ i ]
578
+ const expected = testCase . errors [ i ]
579
+
580
+ // We need to duplicate the simple logic in ESLint's
581
+ // source-code-fixer.js to apply the fix. If we run
582
+ // ESLint with --fix-dry-run, it won't report any
583
+ // errors since it would have fixed them.
584
+ if ( actual . fix ) {
585
+ actualOutput =
586
+ actualOutput . slice ( 0 , actual . fix . range [ 0 ] ) +
587
+ actual . fix . text +
588
+ actualOutput . slice ( actual . fix . range [ 1 ] )
505
589
}
590
+
591
+ for ( const key of Object . keys ( expected ) ) {
592
+ assert . strictEqual (
593
+ actual [ key ] ,
594
+ expected [ key ] ,
595
+ `'${ key } ' is not expected.`
596
+ )
597
+ }
598
+ }
599
+
600
+ if ( testCase . output ) {
601
+ assert . strictEqual (
602
+ actualOutput ,
603
+ testCase . output ,
604
+ "output is not expected"
605
+ )
506
606
}
507
- )
607
+ } )
508
608
)
509
609
}
510
610
} )
0 commit comments