Skip to content

Commit 2a7adfb

Browse files
committed
[ReactNative] Use RCTNullIfNill and (id)kCFNull
Summary: @public Use consistent `null` handling: `value || null` -> `RCTNullIfNil(value)` `value == null ? nil : value` -> `RCTNilIfNull(value)` `[NSNull null]` -> `(id)kCFNull` Test Plan: The tests should be enough.
1 parent 696b31f commit 2a7adfb

File tree

13 files changed

+41
-37
lines changed

13 files changed

+41
-37
lines changed

Examples/UIExplorer/UIExplorerUnitTests/RCTContextExecutorTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ - (void)testDeserializationPerf
5757

5858
JSContextGroupRef group = JSContextGroupCreate();
5959
JSGlobalContextRef context = JSGlobalContextCreateInGroup(group, NULL);
60-
id message = @[@[@1, @2, @3, @4], @[@{@"a": @1}, @{@"b": @2}], [NSNull null]];
60+
id message = @[@[@1, @2, @3, @4], @[@{@"a": @1}, @{@"b": @2}], (id)kCFNull];
6161
NSString *code = RCTJSONStringify(message, NULL);
6262
JSStringRef script = JSStringCreateWithCFString((__bridge CFStringRef)code);
6363
JSValueRef error = NULL;

Examples/UIExplorer/UIExplorerUnitTests/RCTConvert_NSURLTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ - (void)test_##name { \
2828

2929
// Basic tests
3030
TEST_URL(basic, @"http://example.com", @"http://example.com")
31-
TEST_URL(null, [NSNull null], nil)
31+
TEST_URL(null, (id)kCFNull, nil)
3232

3333
// Local files
3434
TEST_PATH(fileURL, @"file:///blah/hello.jsbundle", @"/blah/hello.jsbundle")

Libraries/ActionSheetIOS/RCTActionSheetManager.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "RCTActionSheetManager.h"
1111

1212
#import "RCTLog.h"
13+
#import "RCTUtils.h"
1314

1415
@interface RCTActionSheetManager () <UIActionSheetDelegate>
1516

@@ -90,7 +91,7 @@ - (dispatch_queue_t)methodQueue
9091
if (activityError) {
9192
failureCallback(@[[activityError localizedDescription]]);
9293
} else {
93-
successCallback(@[@(completed), (activityType ?: [NSNull null])]);
94+
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
9495
}
9596
};
9697
} else {
@@ -100,7 +101,7 @@ - (dispatch_queue_t)methodQueue
100101
if (![UIActivityViewController instancesRespondToSelector:@selector(completionWithItemsHandler)]) {
101102
// Legacy iOS 7 implementation
102103
share.completionHandler = ^(NSString *activityType, BOOL completed) {
103-
successCallback(@[@(completed), (activityType ?: [NSNull null])]);
104+
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
104105
};
105106
} else
106107

@@ -109,7 +110,7 @@ - (dispatch_queue_t)methodQueue
109110
{
110111
// iOS 8 version
111112
share.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
112-
successCallback(@[@(completed), (activityType ?: [NSNull null])]);
113+
successCallback(@[@(completed), RCTNullIfNil(activityType)]);
113114
};
114115
}
115116
}

Libraries/LinkingIOS/RCTLinkingManager.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#import "RCTBridge.h"
1313
#import "RCTEventDispatcher.h"
14+
#import "RCTUtils.h"
1415

1516
NSString *const RCTOpenURLNotification = @"RCTOpenURLNotification";
1617

@@ -76,7 +77,7 @@ - (void)handleOpenURLNotification:(NSNotification *)notification
7677
- (NSDictionary *)constantsToExport
7778
{
7879
NSURL *initialURL = _bridge.launchOptions[UIApplicationLaunchOptionsURLKey];
79-
return @{@"initialURL": [initialURL absoluteString] ?: [NSNull null]};
80+
return @{@"initialURL": RCTNullIfNil([initialURL absoluteString])};
8081
}
8182

8283
@end

Libraries/Network/RCTDataManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ - (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
436436

437437
NSArray *responseJSON = @[
438438
request.requestID,
439-
error.localizedDescription ?: [NSNull null],
439+
RCTNullIfNil(error.localizedDescription),
440440
];
441441

442442
[_bridge.eventDispatcher sendDeviceEventWithName:@"didCompleteNetworkResponse"

Libraries/PushNotificationIOS/RCTPushNotificationManager.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#import "RCTBridge.h"
1313
#import "RCTEventDispatcher.h"
14+
#import "RCTUtils.h"
1415

1516
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
1617

@@ -172,7 +173,7 @@ - (void)handleRemoteNotificationsRegistered:(NSNotification *)notification
172173
- (NSDictionary *)constantsToExport
173174
{
174175
return @{
175-
@"initialNotification": _initialNotification ?: [NSNull null]
176+
@"initialNotification": RCTNullIfNil(_initialNotification),
176177
};
177178
}
178179

Libraries/WebSocket/RCTWebSocketManager.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "RCTEventDispatcher.h"
1414
#import "RCTSRWebSocket.h"
1515
#import "RCTSparseArray.h"
16+
#import "RCTUtils.h"
1617

1718
@implementation RCTSRWebSocket (React)
1819

@@ -107,7 +108,7 @@ - (void)webSocket:(RCTSRWebSocket *)webSocket didCloseWithCode:(NSInteger)code
107108
{
108109
[_bridge.eventDispatcher sendDeviceEventWithName:@"websocketClosed" body:@{
109110
@"code": @(code),
110-
@"reason": reason ? reason : [NSNull null],
111+
@"reason": RCTNullIfNil(reason),
111112
@"clean": @(wasClean),
112113
@"id": webSocket.reactTag
113114
}];

React/Base/RCTBridge.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ - (void)invokeWithBridge:(RCTBridge *)bridge
449449
// Set arguments
450450
NSUInteger index = 0;
451451
for (id json in arguments) {
452-
id arg = (json == [NSNull null]) ? nil : json;
452+
id arg = RCTNilIfNull(json);
453453
void (^block)(RCTBridge *, NSNumber *, NSInvocation *, NSUInteger, id) = _argumentBlocks[index];
454454
block(bridge, context, invocation, index + 2, arg);
455455
index++;
@@ -1012,7 +1012,7 @@ - (void)registerModules
10121012
if (queue) {
10131013
_queuesByID[moduleID] = queue;
10141014
} else {
1015-
_queuesByID[moduleID] = [NSNull null];
1015+
_queuesByID[moduleID] = (id)kCFNull;
10161016
}
10171017
}
10181018

@@ -1266,8 +1266,8 @@ - (void)enqueueApplicationScript:(NSString *)script url:(NSURL *)url onComplete:
12661266
context:context
12671267
callback:^(id json, NSError *error) {
12681268
RCTProfileEndEvent(@"FetchApplicationScriptCallbacks", @"js_call,init", @{
1269-
@"json": json ?: [NSNull null],
1270-
@"error": error ?: [NSNull null],
1269+
@"json": RCTNullIfNil(json),
1270+
@"error": RCTNullIfNil(error),
12711271
});
12721272

12731273
[self _handleBuffer:json context:context];
@@ -1293,7 +1293,7 @@ - (void)dispatchBlock:(dispatch_block_t)block forModuleID:(NSNumber *)moduleID
12931293
queue = _queuesByID[moduleID];
12941294
}
12951295

1296-
if (queue == [NSNull null]) {
1296+
if (queue == (id)kCFNull) {
12971297
[_javaScriptExecutor executeBlockOnJavaScriptQueue:block];
12981298
} else {
12991299
dispatch_async(queue ?: _methodQueue, block);
@@ -1509,7 +1509,7 @@ - (BOOL)_handleRequestNumber:(NSUInteger)i
15091509
@"module": method.moduleClassName,
15101510
@"method": method.JSMethodName,
15111511
@"selector": NSStringFromSelector(method.selector),
1512-
@"args": RCTJSONStringify(params ?: [NSNull null], NULL),
1512+
@"args": RCTJSONStringify(RCTNullIfNil(params), NULL),
15131513
});
15141514

15151515
return YES;

React/Base/RCTConvert.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ + (NSNumber *)NSNumber:(id)json
5454
RCTLogConvertError(json, "a number");
5555
}
5656
return number;
57-
} else if (json && json != [NSNull null]) {
57+
} else if (json && json != (id)kCFNull) {
5858
RCTLogConvertError(json, "a number");
5959
}
6060
return nil;
@@ -141,7 +141,7 @@ + (NSDate *)NSDate:(id)json
141141
"Expected format: YYYY-MM-DD'T'HH:mm:ss.sssZ", json);
142142
}
143143
return date;
144-
} else if (json && json != [NSNull null]) {
144+
} else if (json && json != (id)kCFNull) {
145145
RCTLogConvertError(json, "a date");
146146
}
147147
return nil;

React/Base/RCTTouchHandler.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ - (void)_recordNewTouches:(NSSet *)touches
107107
NSMutableDictionary *reactTouch = [[NSMutableDictionary alloc] initWithCapacity:9];
108108
reactTouch[@"target"] = reactTag;
109109
reactTouch[@"identifier"] = @(touchID);
110-
reactTouch[@"touches"] = [NSNull null]; // We hijack this touchObj to serve both as an event
111-
reactTouch[@"changedTouches"] = [NSNull null]; // and as a Touch object, so making this JIT friendly.
110+
reactTouch[@"touches"] = (id)kCFNull; // We hijack this touchObj to serve both as an event
111+
reactTouch[@"changedTouches"] = (id)kCFNull; // and as a Touch object, so making this JIT friendly.
112112

113113
// Add to arrays
114114
[_touchViews addObject:targetView];

0 commit comments

Comments
 (0)