mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
hadle nullable params in generated objcpp
Summary: Param of function can be optional and it should have impact on native code. Inspired by old codegen Reviewed By: RSNara Differential Revision: D16763884 fbshipit-source-id: dab50275f902dbe4af25824bb6128d3b37fc43cd
This commit is contained in:
committed by
Facebook Github Bot
parent
a5aaca7d4f
commit
4a9035aa65
+5
-5
@@ -14,11 +14,11 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
|
||||
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
+getBool: () => ?boolean;
|
||||
+getNumber: () => ?number;
|
||||
+getString: () => ?string;
|
||||
+getArray: () => ?Array<any>;
|
||||
+getObject: () => ?Object;
|
||||
+getBool: (a: ?boolean) => ?boolean;
|
||||
+getNumber: (a: ?number) => ?number;
|
||||
+getString: (a: ?number) => ?string;
|
||||
+getArray: (a: ?Array<any>) => ?Array<any>;
|
||||
+getObject: (a: ?Object) => ?Object;
|
||||
+getValueWithPromise: () => ?Promise<string>;
|
||||
}
|
||||
|
||||
|
||||
+13
-10
@@ -12,7 +12,7 @@
|
||||
|
||||
import type {
|
||||
SchemaType,
|
||||
FunctionTypeAnnotationParamTypeAnnotation,
|
||||
FunctionTypeAnnotationParam,
|
||||
FunctionTypeAnnotationReturn,
|
||||
ObjectParamTypeAnnotation,
|
||||
} from '../../CodegenSchema';
|
||||
@@ -91,26 +91,29 @@ const constants = `- (facebook::react::ModuleConstants<JS::Native::_MODULE_NAME_
|
||||
- (facebook::react::ModuleConstants<JS::Native::_MODULE_NAME_::::Constants::Builder>)getConstants;`;
|
||||
|
||||
function translatePrimitiveJSTypeToObjCType(
|
||||
type: FunctionTypeAnnotationParamTypeAnnotation,
|
||||
param: FunctionTypeAnnotationParam,
|
||||
error: string,
|
||||
) {
|
||||
switch (type.type) {
|
||||
function wrapIntoNullableIfNeeded(generatedType: string) {
|
||||
return param.nullable ? `${generatedType} _Nullable` : generatedType;
|
||||
}
|
||||
switch (param.typeAnnotation.type) {
|
||||
case 'StringTypeAnnotation':
|
||||
return 'NSString *';
|
||||
return wrapIntoNullableIfNeeded('NSString *');
|
||||
case 'NumberTypeAnnotation':
|
||||
case 'FloatTypeAnnotation':
|
||||
case 'Int32TypeAnnotation':
|
||||
return 'NSNumber *';
|
||||
return param.nullable ? 'NSNumber *' : 'double';
|
||||
case 'BooleanTypeAnnotation':
|
||||
return 'BOOL';
|
||||
return param.nullable ? 'NSNumber * _Nullable' : 'BOOL';
|
||||
case 'GenericObjectTypeAnnotation':
|
||||
return 'NSDictionary *';
|
||||
return wrapIntoNullableIfNeeded('NSDictionary *');
|
||||
case 'ArrayTypeAnnotation':
|
||||
return 'NSArray<id<NSObject>> *';
|
||||
return wrapIntoNullableIfNeeded('NSArray *');
|
||||
case 'FunctionTypeAnnotation':
|
||||
return 'RCTResponseSenderBlock';
|
||||
case 'ObjectTypeAnnotation':
|
||||
return 'NSDictionary *';
|
||||
return wrapIntoNullableIfNeeded('NSDictionary *');
|
||||
default:
|
||||
throw new Error(error);
|
||||
}
|
||||
@@ -193,7 +196,7 @@ module.exports = {
|
||||
});
|
||||
}
|
||||
const paramObjCType = translatePrimitiveJSTypeToObjCType(
|
||||
param.typeAnnotation,
|
||||
param,
|
||||
`Unspopported type for param "${param.name}" in ${
|
||||
prop.name
|
||||
}. Found: ${param.typeAnnotation.type}`,
|
||||
|
||||
+3
-3
@@ -294,11 +294,11 @@ inline JS::NativeSampleTurboModule::Constants::Builder::Builder(Constants i) : _
|
||||
- (facebook::react::ModuleConstants<JS::NativeSampleTurboModule::Constants::Builder>)getConstants;
|
||||
- (void) voidFunc;
|
||||
- (BOOL) getBool:(BOOL)arg;
|
||||
- (NSNumber *) getNumber:(NSNumber *)arg;
|
||||
- (NSNumber *) getNumber:(double)arg;
|
||||
- (NSString *) getString:(NSString *)arg;
|
||||
- (NSArray<id<NSObject>> *) getArray:(NSArray<id<NSObject>> *)arg;
|
||||
- (NSArray<id<NSObject>> *) getArray:(NSArray *)arg;
|
||||
- (NSDictionary *) getObject:(NSDictionary *)arg;
|
||||
- (NSDictionary *) getValue:(NSNumber *)x
|
||||
- (NSDictionary *) getValue:(double)x
|
||||
y:(NSString *)y
|
||||
z:(NSDictionary *)z;
|
||||
- (void) getValueWithCallback:(RCTResponseSenderBlock)callback;
|
||||
|
||||
Reference in New Issue
Block a user