@@ -670,6 +670,51 @@ describe("functionRegex", () => {
670670 expect ( funcRE . test ( code ) ) . toBe ( true ) ;
671671 expect ( code . match ( funcRE ) ! [ 0 ] ) . toBe ( "const naomi = (love) => " ) ;
672672 } ) ;
673+
674+ it ( "matches named anonymous function" , ( ) => {
675+ const funcName = "myFunc" ;
676+ const regEx = functionRegex ( funcName , [ "arg1" ] ) ;
677+ expect ( regEx . test ( "myFunc = function(arg1){}" ) ) . toBe ( true ) ;
678+ } ) ;
679+ it ( "matches named anonymous function without parameters" , ( ) => {
680+ const funcName = "myFunc" ;
681+ const regEx = functionRegex ( funcName ) ;
682+ expect ( regEx . test ( "myFunc = function(){ }" ) ) . toBe ( true ) ;
683+ } ) ;
684+ it ( "does not match named anonymous function with different name" , ( ) => {
685+ const funcName = "myFunc" ;
686+ const regEx = functionRegex ( funcName ) ;
687+ expect ( regEx . test ( "notMyFunc = function(arg1){ }" ) ) . toBe ( false ) ;
688+ } ) ;
689+
690+ it ( "matches named anonymous function with arguments and a body" , ( ) => {
691+ const funcName = "myFunc" ;
692+ const regEx = functionRegex ( funcName , [ "arg1" , "arg2" ] ) ;
693+ expect (
694+ regEx . test ( "myFunc = function(arg1, arg2){\n console.log()\n}" ) ,
695+ ) . toBe ( true ) ;
696+ } ) ;
697+
698+ it ( "does not match named anonymous function with different arguments" , ( ) => {
699+ const funcName = "myFunc" ;
700+ const regEx = functionRegex ( funcName , [ "arg1" , "arg2" ] ) ;
701+ expect ( regEx . test ( "myFunc = function(arg1, arg3){}" ) ) . toBe ( false ) ;
702+ } ) ;
703+ it ( "matches const named anonymous function declarations if they are present" , ( ) => {
704+ const regEx = functionRegex ( "myFunc" , [ "arg1" , "arg2" ] ) ;
705+ const func = "const myFunc = function(arg1, arg2) {}" ;
706+ const match = func . match ( regEx ) ;
707+ expect ( match ) . not . toBeNull ( ) ;
708+ expect ( match ! [ 0 ] ) . toBe ( func ) ;
709+ } ) ;
710+ it ( "can capture named anonymous function" , ( ) => {
711+ const funcName = "myFunc" ;
712+ const regEx = functionRegex ( funcName , [ "arg1" , "arg2" ] , { capture : true } ) ;
713+ const func = "myFunc = function(arg1, arg2){return arg1 + arg2}" ;
714+ const match = func . match ( regEx ) ;
715+ expect ( match ) . not . toBeNull ( ) ;
716+ expect ( match ! [ 1 ] ) . toBe ( func ) ;
717+ } ) ;
673718} ) ;
674719
675720describe ( "prepTestComponent" , ( ) => {
0 commit comments