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:
Ramanpreet Nara 2021-01-13 19:17:05 -08:00 committed by Facebook GitHub Bot
parent 1433ed6333
commit fc0b3faf96
4 changed files with 18 additions and 15 deletions

View File

@ -1111,7 +1111,7 @@ inline JS::NativeSampleTurboModuleArrays::Constants::Builder::Builder(const Inpu
auto const3 = i.const3.get();
d[@\\"const3\\"] = RCTConvertVecToArray(const3, ^id(NSString * el_) { return el_; });
auto id_ = i.id_;
d[@\\"id_\\"] = RCTConvertOptionalVecToArray(id_, ^id(folly::Optional<JS::NativeSampleTurboModuleArrays::ConstantsIdElement::Builder> el_) { return el_.hasValue() ? el_.value().buildUnsafeRawValue() : nil; });
d[@\\"id\\"] = RCTConvertOptionalVecToArray(id_, ^id(folly::Optional<JS::NativeSampleTurboModuleArrays::ConstantsIdElement::Builder> el_) { return el_.hasValue() ? el_.value().buildUnsafeRawValue() : nil; });
return d;
}) {}
inline JS::NativeSampleTurboModuleArrays::Constants::Builder::Builder(Constants i) : _factory(^{

View File

@ -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()';
}

View File

@ -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');

View File

@ -171,7 +171,7 @@ inline NSString *JS::NativeSampleTurboModule::SpecDifficultAE::F() const
}
inline double JS::NativeSampleTurboModule::SpecDifficultAE::id_() const
{
id const p = _v[@\\"id_\\"];
id const p = _v[@\\"id\\"];
return RCTBridgingToDouble(p);
}
inline bool JS::NativeSampleTurboModule::SpecDifficultA::D() const
@ -743,7 +743,7 @@ inline facebook::react::LazyVector<JS::NativeExceptionsManager::StackFrame> JS::
}
inline double JS::NativeExceptionsManager::ExceptionData::id_() const
{
id const p = _v[@\\"id_\\"];
id const p = _v[@\\"id\\"];
return RCTBridgingToDouble(p);
}
inline bool JS::NativeExceptionsManager::ExceptionData::isFatal() const