@@ -471,4 +471,77 @@ describe('utils', () => {
471
471
}
472
472
} ) ;
473
473
} ) ;
474
+
475
+ describe ( 'isAutoFixerFunction' , ( ) => {
476
+ const CASES = {
477
+ 'context.report({ fix(fixer) {} });' ( ast ) {
478
+ return { expected : true , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
479
+ } ,
480
+ 'context.notReport({ fix(fixer) {} });' ( ast ) {
481
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
482
+ } ,
483
+ 'context.report({ notFix(fixer) {} });' ( ast ) {
484
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
485
+ } ,
486
+ 'notContext.report({ notFix(fixer) {} });' ( ast ) {
487
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value , context : undefined } ;
488
+ } ,
489
+ } ;
490
+
491
+ Object . keys ( CASES ) . forEach ( ruleSource => {
492
+ it ( ruleSource , ( ) => {
493
+ const ast = espree . parse ( ruleSource , { ecmaVersion : 6 , range : true } ) ;
494
+
495
+ // Add parent to each node.
496
+ estraverse . traverse ( ast , {
497
+ enter ( node , parent ) {
498
+ node . parent = parent ;
499
+ } ,
500
+ } ) ;
501
+
502
+ const testCase = CASES [ ruleSource ] ( ast ) ;
503
+ const contextIdentifiers = new Set ( [ testCase . context ] ) ;
504
+ const result = utils . isAutoFixerFunction ( testCase . node , contextIdentifiers ) ;
505
+ assert . strictEqual ( result , testCase . expected ) ;
506
+ } ) ;
507
+ } ) ;
508
+ } ) ;
509
+
510
+ describe ( 'isSuggestionFixerFunction' , ( ) => {
511
+ const CASES = {
512
+ 'context.report({ suggest: [{ fix(fixer) {} }] });' ( ast ) {
513
+ return { expected : true , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value . elements [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
514
+ } ,
515
+ 'context.notReport({ suggest: [{ fix(fixer) {} }] });' ( ast ) {
516
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value . elements [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
517
+ } ,
518
+ 'context.report({ notSuggest: [{ fix(fixer) {} }] });' ( ast ) {
519
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value . elements [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
520
+ } ,
521
+ 'context.report({ suggest: [{ notFix(fixer) {} }] });' ( ast ) {
522
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value . elements [ 0 ] . properties [ 0 ] . value , context : ast . body [ 0 ] . expression . callee . object } ;
523
+ } ,
524
+ 'notContext.report({ suggest: [{ fix(fixer) {} }] });' ( ast ) {
525
+ return { expected : false , node : ast . body [ 0 ] . expression . arguments [ 0 ] . properties [ 0 ] . value , context : undefined } ;
526
+ } ,
527
+ } ;
528
+
529
+ Object . keys ( CASES ) . forEach ( ruleSource => {
530
+ it ( ruleSource , ( ) => {
531
+ const ast = espree . parse ( ruleSource , { ecmaVersion : 6 , range : true } ) ;
532
+
533
+ // Add parent to each node.
534
+ estraverse . traverse ( ast , {
535
+ enter ( node , parent ) {
536
+ node . parent = parent ;
537
+ } ,
538
+ } ) ;
539
+
540
+ const testCase = CASES [ ruleSource ] ( ast ) ;
541
+ const contextIdentifiers = new Set ( [ testCase . context ] ) ;
542
+ const result = utils . isSuggestionFixerFunction ( testCase . node , contextIdentifiers ) ;
543
+ assert . strictEqual ( result , testCase . expected ) ;
544
+ } ) ;
545
+ } ) ;
546
+ } ) ;
474
547
} ) ;
0 commit comments