@@ -624,3 +624,180 @@ const obj = {
624624 } ) ;
625625 } ) ;
626626} ) ;
627+
628+ describe ( 'issue #37 - arrow function precedence in operator expressions' , ( ) => {
629+ describe ( 'when function expressions need parentheses due to operator precedence' , ( ) => {
630+ ruleTester . run ( 'prefer-arrow-functions' , rule , {
631+ valid : [ ] ,
632+ invalid : [
633+ // Cases that should be transformed but need parentheses for correct precedence
634+ {
635+ code : `const result = f || function() { return 'default'; };` ,
636+ output : `const result = f || (() => 'default');` ,
637+ } ,
638+ {
639+ code : `const result = f && function() { return 'value'; };` ,
640+ output : `const result = f && (() => 'value');` ,
641+ } ,
642+ {
643+ code : `const result = f + function() { return 1; };` ,
644+ output : `const result = f + (() => 1);` ,
645+ } ,
646+ {
647+ code : `const result = f - function() { return 1; };` ,
648+ output : `const result = f - (() => 1);` ,
649+ } ,
650+ {
651+ code : `const result = f * function() { return 2; };` ,
652+ output : `const result = f * (() => 2);` ,
653+ } ,
654+ {
655+ code : `const result = f / function() { return 2; };` ,
656+ output : `const result = f / (() => 2);` ,
657+ } ,
658+ {
659+ code : `const result = f % function() { return 3; };` ,
660+ output : `const result = f % (() => 3);` ,
661+ } ,
662+ {
663+ code : `const result = f ** function() { return 2; };` ,
664+ output : `const result = f ** (() => 2);` ,
665+ } ,
666+ {
667+ code : `const result = f | function() { return 1; };` ,
668+ output : `const result = f | (() => 1);` ,
669+ } ,
670+ {
671+ code : `const result = f & function() { return 1; };` ,
672+ output : `const result = f & (() => 1);` ,
673+ } ,
674+ {
675+ code : `const result = f ^ function() { return 1; };` ,
676+ output : `const result = f ^ (() => 1);` ,
677+ } ,
678+ {
679+ code : `const result = f << function() { return 1; };` ,
680+ output : `const result = f << (() => 1);` ,
681+ } ,
682+ {
683+ code : `const result = f >> function() { return 1; };` ,
684+ output : `const result = f >> (() => 1);` ,
685+ } ,
686+ {
687+ code : `const result = f >>> function() { return 1; };` ,
688+ output : `const result = f >>> (() => 1);` ,
689+ } ,
690+ {
691+ code : `const result = f < function() { return 1; };` ,
692+ output : `const result = f < (() => 1);` ,
693+ } ,
694+ {
695+ code : `const result = f > function() { return 1; };` ,
696+ output : `const result = f > (() => 1);` ,
697+ } ,
698+ {
699+ code : `const result = f <= function() { return 1; };` ,
700+ output : `const result = f <= (() => 1);` ,
701+ } ,
702+ {
703+ code : `const result = f >= function() { return 1; };` ,
704+ output : `const result = f >= (() => 1);` ,
705+ } ,
706+ {
707+ code : `const result = f == function() { return 1; };` ,
708+ output : `const result = f == (() => 1);` ,
709+ } ,
710+ {
711+ code : `const result = f != function() { return 1; };` ,
712+ output : `const result = f != (() => 1);` ,
713+ } ,
714+ {
715+ code : `const result = f === function() { return 1; };` ,
716+ output : `const result = f === (() => 1);` ,
717+ } ,
718+ {
719+ code : `const result = f !== function() { return 1; };` ,
720+ output : `const result = f !== (() => 1);` ,
721+ } ,
722+ {
723+ code : `const result = f instanceof function() { return Object; };` ,
724+ output : `const result = f instanceof (() => Object);` ,
725+ } ,
726+ {
727+ code : `const result = f in function() { return {}; };` ,
728+ output : `const result = f in (() => ({}));` ,
729+ } ,
730+ ] . map ( withErrors ( [ 'USE_ARROW_WHEN_FUNCTION' ] ) ) ,
731+ } ) ;
732+ } ) ;
733+
734+ describe ( 'when function expressions can be safely transformed without parentheses' , ( ) => {
735+ ruleTester . run ( 'prefer-arrow-functions' , rule , {
736+ valid : [ ] ,
737+ invalid : [
738+ // Assignment operators - don't need parentheses
739+ {
740+ code : `let result = function() { return 'value'; };` ,
741+ output : `let result = () => 'value';` ,
742+ } ,
743+ {
744+ code : `result += function() { return 1; };` ,
745+ output : `result += () => 1;` ,
746+ } ,
747+ {
748+ code : `result -= function() { return 1; };` ,
749+ output : `result -= () => 1;` ,
750+ } ,
751+ {
752+ code : `result *= function() { return 2; };` ,
753+ output : `result *= () => 2;` ,
754+ } ,
755+ {
756+ code : `result /= function() { return 2; };` ,
757+ output : `result /= () => 2;` ,
758+ } ,
759+ // Arrow function as right operand of arrow function
760+ {
761+ code : `const fn = () => function() { return 'nested'; };` ,
762+ output : `const fn = () => () => 'nested';` ,
763+ } ,
764+ // Ternary operators - right side doesn't need parentheses
765+ {
766+ code : `const result = condition ? 'yes' : function() { return 'no'; };` ,
767+ output : `const result = condition ? 'yes' : () => 'no';` ,
768+ } ,
769+ {
770+ code : `const result = condition ? function() { return 'yes'; } : 'no';` ,
771+ output : `const result = condition ? () => 'yes' : 'no';` ,
772+ } ,
773+ // Comma operator - doesn't need parentheses
774+ {
775+ code : `const result = (doSomething(), function() { return 'value'; });` ,
776+ output : `const result = (doSomething(), () => 'value');` ,
777+ } ,
778+ {
779+ code : `const result = (function() { return 'first'; }, doSomething());` ,
780+ output : `const result = (() => 'first', doSomething());` ,
781+ } ,
782+ {
783+ code : `const result = (doSomething(), function() { return 'second'; });` ,
784+ output : `const result = (doSomething(), () => 'second');` ,
785+ } ,
786+ // Spread operator - doesn't need parentheses
787+ {
788+ code : `const result = [...items, function() { return 'last'; }];` ,
789+ output : `const result = [...items, () => 'last'];` ,
790+ } ,
791+ // Yield - doesn't need parentheses
792+ {
793+ code : `function* gen() { yield function() { return 'value'; }; }` ,
794+ output : `function* gen() { yield () => 'value'; }` ,
795+ } ,
796+ {
797+ code : `function* gen() { yield* function() { return [1, 2, 3]; }; }` ,
798+ output : `function* gen() { yield* () => [1, 2, 3]; }` ,
799+ } ,
800+ ] . map ( withErrors ( [ 'USE_ARROW_WHEN_FUNCTION' ] ) ) ,
801+ } ) ;
802+ } ) ;
803+ } ) ;
0 commit comments