mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Fix ObjC structs with id properties
Summary:
## Problem
Suppose we have this NativeModule spec, that uses Object literals that contain a property called "id":
```
export interface Spec extends TurboModule {
// Exported methods.
+getConstants: () => {|
id: string,
|};
+getObject: (arg: {id: Object}) => Object;
}
```
For both object literals, we'll generate C++ structs, backed by NSDictionaries. The method in each struct will be named "id_" to avoid a name clash with ObjC's `id` identifier. However, calling that id_ method should still access the "id" property in the corresponding NSDictionary.
## Expected Output
```
inline id<NSObject> JS::NativeSampleTurboModule::SpecGetObjectArg::id_() const
{
id const p = _v[@"id"];
return p;
}
inline JS::NativeSampleTurboModule::Constants::Builder::Builder(const Input i) : _factory(^{
NSMutableDictionary *d = [NSMutableDictionary new];
auto id_ = i.id_.get();
d[@"id"] = id_;
return d;
}) {}
```
## Actual Output
```
inline id<NSObject> JS::NativeSampleTurboModule::SpecGetObjectArg::id_() const
{
id const p = _v[@"id_"]; // <-- HERE!
return p;
}
inline JS::NativeSampleTurboModule::Constants::Builder::Builder(const Input i) : _factory(^{
NSMutableDictionary *d = [NSMutableDictionary new];
auto id_ = i.id_.get();
d[@"id_"] = id_; // <-- HERE!
return d;
}) {}
```
NOTE: This code was generated by running `jf get --version 119805822 && buck build //xplat/js:FBReactNativeSpec_Sample-flow-types-ios --show-output`
This diff fixes this mistake.
Changelog: [Internal]
Reviewed By: fkgozali
Differential Revision: D25907493
fbshipit-source-id: cb37cbf49db4f871b3f4046f7397a7b1b7df0357
This commit is contained in:
committed by
Facebook GitHub Bot
parent
1433ed6333
commit
fc0b3faf96
+7
-7
@@ -222,15 +222,15 @@ function serializeConstantsStruct(
|
||||
builderInputProps: struct.properties
|
||||
.map(property => {
|
||||
const {typeAnnotation, optional} = property;
|
||||
const propName = getSafePropertyName(property);
|
||||
const safePropName = getSafePropertyName(property);
|
||||
const objCType = toObjCType(hasteModuleName, typeAnnotation, optional);
|
||||
|
||||
if (!optional) {
|
||||
return `RCTRequired<${objCType}> ${propName};`;
|
||||
return `RCTRequired<${objCType}> ${safePropName};`;
|
||||
}
|
||||
|
||||
const space = ' '.repeat(objCType.endsWith('*') ? 0 : 1);
|
||||
return `${objCType}${space}${propName};`;
|
||||
return `${objCType}${space}${safePropName};`;
|
||||
})
|
||||
.join('\n '),
|
||||
});
|
||||
@@ -240,17 +240,17 @@ function serializeConstantsStruct(
|
||||
structName: struct.name,
|
||||
properties: struct.properties
|
||||
.map(property => {
|
||||
const {typeAnnotation, optional} = property;
|
||||
const propName = getSafePropertyName(property);
|
||||
const {typeAnnotation, optional, name: propName} = property;
|
||||
const safePropName = getSafePropertyName(property);
|
||||
const objCValue = toObjCValue(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
propName,
|
||||
safePropName,
|
||||
0,
|
||||
optional,
|
||||
);
|
||||
|
||||
let varDecl = `auto ${propName} = i.${propName}`;
|
||||
let varDecl = `auto ${safePropName} = i.${safePropName}`;
|
||||
if (!optional) {
|
||||
varDecl += '.get()';
|
||||
}
|
||||
|
||||
+8
-5
@@ -52,13 +52,15 @@ const MethodTemplate = ({
|
||||
hasteModuleName,
|
||||
structName,
|
||||
propertyName,
|
||||
safePropertyName,
|
||||
}: $ReadOnly<{
|
||||
returnType: string,
|
||||
returnValue: string,
|
||||
hasteModuleName: string,
|
||||
structName: string,
|
||||
propertyName: string,
|
||||
}>) => `inline ${returnType}JS::${hasteModuleName}::${structName}::${propertyName}() const
|
||||
safePropertyName: string,
|
||||
}>) => `inline ${returnType}JS::${hasteModuleName}::${structName}::${safePropertyName}() const
|
||||
{
|
||||
id const p = _v[@"${propertyName}"];
|
||||
return ${returnValue};
|
||||
@@ -211,7 +213,7 @@ function serializeRegularStruct(
|
||||
structProperties: struct.properties
|
||||
.map(property => {
|
||||
const {typeAnnotation, optional} = property;
|
||||
const propName = getSafePropertyName(property);
|
||||
const safePropName = getSafePropertyName(property);
|
||||
const returnType = toObjCType(
|
||||
hasteModuleName,
|
||||
typeAnnotation,
|
||||
@@ -219,15 +221,15 @@ function serializeRegularStruct(
|
||||
);
|
||||
|
||||
const padding = ' '.repeat(returnType.endsWith('*') ? 0 : 1);
|
||||
return `${returnType}${padding}${propName}() const;`;
|
||||
return `${returnType}${padding}${safePropName}() const;`;
|
||||
})
|
||||
.join('\n '),
|
||||
});
|
||||
|
||||
const methods = struct.properties
|
||||
.map<string>(property => {
|
||||
const {typeAnnotation, optional} = property;
|
||||
const propName = getSafePropertyName(property);
|
||||
const {typeAnnotation, optional, name: propName} = property;
|
||||
const safePropertyName = getSafePropertyName(property);
|
||||
const returnType = toObjCType(hasteModuleName, typeAnnotation, optional);
|
||||
const returnValue = toObjCValue(
|
||||
hasteModuleName,
|
||||
@@ -244,6 +246,7 @@ function serializeRegularStruct(
|
||||
returnType: returnType + padding,
|
||||
returnValue: returnValue,
|
||||
propertyName: propName,
|
||||
safePropertyName,
|
||||
});
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
Reference in New Issue
Block a user