File tree Expand file tree Collapse file tree 3 files changed +51
-4
lines changed Expand file tree Collapse file tree 3 files changed +51
-4
lines changed Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ export default class ObjcPatcher {
17
17
return this . _content . indexOf ( this . _patchSig ) >= 0 ;
18
18
}
19
19
20
- addImport ( file : string ) : ObjcPatcher {
21
- const lastImportBegin = this . _content . lastIndexOf ( '\n#import' ) ;
20
+ addImport ( file : string , searchPattern = '\n#import' ) : ObjcPatcher {
21
+ const lastImportBegin = this . _content . lastIndexOf ( searchPattern ) ;
22
22
const lastImportEnd = this . _content . indexOf ( '\n' , lastImportBegin + 1 ) ;
23
23
const headPart = this . _content . substring ( 0 , lastImportEnd ) ;
24
24
const tailPart = this . _content . substring ( lastImportEnd ) ;
Original file line number Diff line number Diff line change @@ -42,6 +42,50 @@ describe('ObjcPatcher', () => {
42
42
expect ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) . toBe ( afterPatch ) ;
43
43
} ) ;
44
44
45
+ test ( 'addImport() will append imports for specified searchPattern' , ( ) => {
46
+ const beforePatch = `\
47
+ #import <UIKit/UIKit.h>
48
+ #import <React/RCTRootView.h>
49
+
50
+ #if DEBUG
51
+ #import <FlipperKit/FlipperClient.h>
52
+ #endif
53
+
54
+ @implementation AppDelegate
55
+ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
56
+ {
57
+ return NO;
58
+ }
59
+ ` ;
60
+
61
+ const afterPatch = `\
62
+ /* Patched by ObjcPatcher: foo */
63
+ #import <UIKit/UIKit.h>
64
+ #import <React/RCTRootView.h>
65
+ #import <React/Foo.h>
66
+
67
+ #if DEBUG
68
+ #import <FlipperKit/FlipperClient.h>
69
+ #endif
70
+
71
+ @implementation AppDelegate
72
+ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
73
+ {
74
+ return NO;
75
+ }
76
+ ` ;
77
+ fs . readFileSync = jest . fn ( ) . mockReturnValueOnce ( beforePatch ) ;
78
+
79
+ fs . writeFileSync = jest . fn ( ) ;
80
+
81
+ const patcher = new ObjcPatcher ( '/foo' , 'foo' ) ;
82
+ patcher
83
+ . addImport ( '<React/Foo.h>' , '\n#import <React/RCTRootView.h>' )
84
+ . write ( '/bar' ) ;
85
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/bar' ) ;
86
+ expect ( fs . writeFileSync . mock . calls [ 0 ] [ 1 ] ) . toBe ( afterPatch ) ;
87
+ } ) ;
88
+
45
89
test ( 'isPatched() returns false if the file has not been patched' , ( ) => {
46
90
const beforePatch = `\
47
91
#import <UIKit/UIKit.h>
Original file line number Diff line number Diff line change @@ -79,9 +79,12 @@ static vm_size_t RCTGetResidentMemorySize(void)
79
79
continue ;
80
80
}
81
81
patcher
82
- . addImport ( '<mach/mach.h>' )
82
+ . addImport ( '<mach/mach.h>' , '\n#import <React/RCTRootView.h>' )
83
83
. addFunction ( addFunctionGetRssMemory )
84
- . addImport ( '<React/RCTPerformanceLogger.h>' )
84
+ . addImport (
85
+ '<React/RCTPerformanceLogger.h>' ,
86
+ '\n#import <React/RCTRootView.h>' ,
87
+ )
85
88
. replace (
86
89
searchPatternWithinDidFinishLaunchingWithOptions ,
87
90
`$1\n${ addCodeMeasureAfterFiveSeconds } ` ,
You can’t perform that action at this time.
0 commit comments