Skip to content

Commit 756dae4

Browse files
committed
Merge branch 'antonis/4625-expo-useNativeInit' into kw-expo-plugin-ref
2 parents d466145 + 1918baf commit 756dae4

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

packages/core/plugin/src/withSentryAndroid.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ export function modifyMainApplication(config: ExpoConfig): ExpoConfig {
105105
);
106106
}
107107
} else {
108-
warnOnce(`Unrecognized language detected in '${fileName}', the native code won't be updated.`);
108+
warnOnce(
109+
`Unsupported language '${config.modResults.language}' detected in '${fileName}', the native code won't be updated.`,
110+
);
109111
}
110112

111113
return config;

packages/core/plugin/src/withSentryIOS.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ export function modifyAppDelegate(config: ExpoConfig): ExpoConfig {
104104
// Add RNSentrySDK.start() at the beginning of application method
105105
const originalContents = config.modResults.contents;
106106
config.modResults.contents = config.modResults.contents.replace(
107-
/(func application\([^)]*\) -> Bool \{)/s,
108-
`$1\n RNSentrySDK.start()`,
107+
/(func application\([^)]*\) -> Bool \{)\s*\n(\s*)/s,
108+
`$1\n$2RNSentrySDK.start()\n$2`,
109109
);
110110
if (config.modResults.contents === originalContents) {
111111
warnOnce(`Failed to insert 'RNSentrySDK.start()' in '${fileName}'.`);
112112
} else if (!config.modResults.contents.includes('import RNSentry')) {
113113
// Insert import statement after UIKit import
114114
config.modResults.contents = config.modResults.contents.replace(/(import UIKit\n)/, `$1import RNSentry\n`);
115115
}
116-
} else if (config.modResults.language === 'objc') {
116+
} else if (['objcpp', 'objc'].includes(config.modResults.language)) {
117117
if (config.modResults.contents.includes('[RNSentrySDK start]')) {
118118
warnOnce(`Your '${fileName}' already contains '[RNSentrySDK start]'.`);
119119
return config;
@@ -134,7 +134,9 @@ export function modifyAppDelegate(config: ExpoConfig): ExpoConfig {
134134
);
135135
}
136136
} else {
137-
warnOnce(`Unsupported language detected in '${fileName}', the native code won't be updated.`);
137+
warnOnce(
138+
`Unsupported language '${config.modResults.language}' detected in '${fileName}', the native code won't be updated.`,
139+
);
138140
}
139141

140142
return config;

packages/core/test/expo-plugin/modifyAppDelegate.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface MockedExpoConfig extends ExpoConfig {
1717
modResults: {
1818
path: string;
1919
contents: string;
20-
language: 'swift' | 'objc';
20+
language: 'swift' | 'objc' | 'objcpp' | string;
2121
};
2222
}
2323

@@ -148,6 +148,30 @@ describe('modifyAppDelegate', () => {
148148
expect(result.modResults.contents).toBe(objcExpected);
149149
});
150150

151+
it('should modify an Objective-C++ file by adding the RNSentrySDK import and start', async () => {
152+
config.modResults.language = 'objcpp';
153+
config.modResults.contents = objcContents;
154+
155+
const result = (await modifyAppDelegate(config)) as MockedExpoConfig;
156+
157+
expect(result.modResults.contents).toContain('#import <RNSentry/RNSentry.h>');
158+
expect(result.modResults.contents).toContain('[RNSentrySDK start];');
159+
expect(result.modResults.contents).toBe(objcExpected);
160+
});
161+
162+
it('should not modify a source file if the language is not supported', async () => {
163+
config.modResults.language = 'cpp';
164+
config.modResults.contents = objcContents;
165+
config.modResults.path = 'samples/react-native/ios/AppDelegate.cpp';
166+
167+
const result = (await modifyAppDelegate(config)) as MockedExpoConfig;
168+
169+
expect(warnOnce).toHaveBeenCalledWith(
170+
`Unsupported language 'cpp' detected in 'AppDelegate.cpp', the native code won't be updated.`,
171+
);
172+
expect(result).toBe(config); // No modification
173+
});
174+
151175
it('should insert import statements only once in an Swift project', async () => {
152176
config.modResults.contents =
153177
'import UIKit\nimport RNSentrySDK\n\noverride func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {';

0 commit comments

Comments
 (0)