From 12e4a5745d54a7be86c005e4ed954c42171541b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Thu, 8 Feb 2024 07:56:05 -0800 Subject: [PATCH] feat: add nullable annotations to RCTBundleURLProvider (#42293) Summary: This PR adds nullable annotations to `RCTBundleURLProvider` also allowing to return optional (the default that will be returned when metro is not running). Not having this may lead to crashes because Swift will try to unwrap optional with nil when metro is not running. ## Changelog: [iOS] [Added] - add nullable annotations to RCTBundleURLProvider Pull Request resolved: https://github.com/facebook/react-native/pull/42293 Test Plan: CI Green Reviewed By: sammy-SC Differential Revision: D52797676 Pulled By: cipolleschi fbshipit-source-id: 98b4f99aa71828f5397276d22f35d24e48657dc8 --- .../Libraries/AppDelegate/RCTAppDelegate.h | 2 +- .../React/Base/RCTBridgeDelegate.h | 2 +- .../React/Base/RCTBundleURLProvider.h | 61 ++++++++++--------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h index e0edbef47a2..dd234fb7b16 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h +++ b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.h @@ -158,7 +158,7 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)bridgelessEnabled; /// Return the bundle URL for the main bundle. -- (NSURL *)bundleURL; +- (NSURL *__nullable)bundleURL; @end diff --git a/packages/react-native/React/Base/RCTBridgeDelegate.h b/packages/react-native/React/Base/RCTBridgeDelegate.h index 0d9ec0a2b13..1bc3723ea9e 100644 --- a/packages/react-native/React/Base/RCTBridgeDelegate.h +++ b/packages/react-native/React/Base/RCTBridgeDelegate.h @@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN * When running from a locally bundled JS file, this should be a `file://` url * pointing to a path inside the app resources, e.g. `file://.../main.jsbundle`. */ -- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge; +- (NSURL *__nullable)sourceURLForBridge:(RCTBridge *)bridge; @optional diff --git a/packages/react-native/React/Base/RCTBundleURLProvider.h b/packages/react-native/React/Base/RCTBundleURLProvider.h index bd627816167..3c96903ee76 100644 --- a/packages/react-native/React/Base/RCTBundleURLProvider.h +++ b/packages/react-native/React/Base/RCTBundleURLProvider.h @@ -9,7 +9,7 @@ #import "RCTDefines.h" -RCT_EXTERN NSString *const RCTBundleURLProviderUpdatedNotification; +RCT_EXTERN NSString *_Nonnull const RCTBundleURLProviderUpdatedNotification; RCT_EXTERN const NSUInteger kRCTBundleURLProviderDefaultPort; #if RCT_DEV_MENU | RCT_PACKAGER_LOADING_FUNCTIONALITY @@ -22,6 +22,8 @@ RCT_EXTERN const NSUInteger kRCTBundleURLProviderDefaultPort; RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed); #endif +NS_ASSUME_NONNULL_BEGIN + @interface RCTBundleURLProvider : NSObject /** @@ -52,52 +54,53 @@ RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed); * The port is optional, if not specified, kRCTBundleURLProviderDefaultPort will be used * The scheme is also optional, if not specified, a default http protocol will be used */ -+ (BOOL)isPackagerRunning:(NSString *)hostPort scheme:(NSString *)scheme; ++ (BOOL)isPackagerRunning:(NSString *)hostPort scheme:(NSString *__nullable)scheme; /** * Returns the jsBundleURL for a given bundle entrypoint and * the fallback offline JS bundle if the packager is not running. */ -- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackURLProvider:(NSURL * (^)(void))fallbackURLProvider; +- (NSURL *__nullable)jsBundleURLForBundleRoot:(NSString *)bundleRoot + fallbackURLProvider:(NSURL *__nullable (^)(void))fallbackURLProvider; /** * Returns the jsBundleURL for a given split bundle entrypoint in development */ -- (NSURL *)jsBundleURLForSplitBundleRoot:(NSString *)bundleRoot; +- (NSURL *__nullable)jsBundleURLForSplitBundleRoot:(NSString *)bundleRoot; /** * Returns the jsBundleURL for a given bundle entrypoint and * the fallback offline JS bundle if the packager is not running. * if extension is nil, "jsbundle" will be used. */ -- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackExtension:(NSString *)extension; +- (NSURL *__nullable)jsBundleURLForBundleRoot:(NSString *)bundleRoot fallbackExtension:(NSString *__nullable)extension; /** * Returns the jsBundleURL for a given bundle entrypoint and * the fallback offline JS bundle if the packager is not running. */ -- (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot; +- (NSURL *__nullable)jsBundleURLForBundleRoot:(NSString *)bundleRoot; /** * Returns the jsBundleURL for a given bundle entrypoint and * the fallback offline JS bundle. If extension is nil, * "jsbundle" will be used. */ -- (NSURL *)jsBundleURLForFallbackExtension:(NSString *)extension; +- (NSURL *__nullable)jsBundleURLForFallbackExtension:(NSString *__nullable)extension; /** * Returns the resourceURL for a given bundle entrypoint and * the fallback offline resource file if the packager is not running. */ -- (NSURL *)resourceURLForResourceRoot:(NSString *)root - resourceName:(NSString *)name - resourceExtension:(NSString *)extension - offlineBundle:(NSBundle *)offlineBundle; +- (NSURL *__nullable)resourceURLForResourceRoot:(NSString *)root + resourceName:(NSString *)name + resourceExtension:(NSString *)extension + offlineBundle:(NSBundle *)offlineBundle; /** * The IP address or hostname of the packager. */ -@property (nonatomic, copy) NSString *jsLocation; +@property (nonatomic, copy, nullable) NSString *jsLocation; @property (nonatomic, assign) BOOL enableMinification; @property (nonatomic, assign) BOOL enableDev; @@ -124,20 +127,20 @@ RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed); * - runModule: When true, will run the main module after defining all modules. This is used in the main bundle but not * in split bundles. */ -+ (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot - packagerHost:(NSString *)packagerHost - enableDev:(BOOL)enableDev - enableMinification:(BOOL)enableMinification - inlineSourceMap:(BOOL)inlineSourceMap; ++ (NSURL *__nullable)jsBundleURLForBundleRoot:(NSString *)bundleRoot + packagerHost:(NSString *)packagerHost + enableDev:(BOOL)enableDev + enableMinification:(BOOL)enableMinification + inlineSourceMap:(BOOL)inlineSourceMap; -+ (NSURL *)jsBundleURLForBundleRoot:(NSString *)bundleRoot - packagerHost:(NSString *)packagerHost - packagerScheme:(NSString *)scheme - enableDev:(BOOL)enableDev - enableMinification:(BOOL)enableMinification - inlineSourceMap:(BOOL)inlineSourceMap - modulesOnly:(BOOL)modulesOnly - runModule:(BOOL)runModule; ++ (NSURL *__nullable)jsBundleURLForBundleRoot:(NSString *)bundleRoot + packagerHost:(NSString *)packagerHost + packagerScheme:(NSString *__nullable)scheme + enableDev:(BOOL)enableDev + enableMinification:(BOOL)enableMinification + inlineSourceMap:(BOOL)inlineSourceMap + modulesOnly:(BOOL)modulesOnly + runModule:(BOOL)runModule; /** * Given a hostname for the packager and a resource path (including "/"), return the URL to the resource. * In general, please use the instance method to decide if the packager is running and fallback to the pre-packaged @@ -145,8 +148,8 @@ RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed); */ + (NSURL *)resourceURLForResourcePath:(NSString *)path packagerHost:(NSString *)packagerHost - scheme:(NSString *)scheme - query:(NSString *)query + scheme:(NSString *__nullable)scheme + query:(NSString *__nullable)query __deprecated_msg("Use version with queryItems parameter instead"); /** @@ -157,6 +160,8 @@ RCT_EXTERN void RCTBundleURLProviderAllowPackagerServerAccess(BOOL allowed); + (NSURL *)resourceURLForResourcePath:(NSString *)path packagerHost:(NSString *)packagerHost scheme:(NSString *)scheme - queryItems:(NSArray *)queryItems; + queryItems:(NSArray *__nullable)queryItems; @end + +NS_ASSUME_NONNULL_END