Skip to content

Commit

Permalink
Recognize dictionary type in codegen (#37206)
Browse files Browse the repository at this point in the history
Summary:
Previously we allow `{[key:string]:Something}` in codegen, `Something` is type-checked but thrown away, generating a `GenericObjectTypeAnnotation`.
In this change, `Something` is added to `GenericObjectTypeAnnotation` as an optional field.
For downstream code such as C++ codegen, this change is **backward compatible**. It allows C++ codegen to produce a more precious type optionally.

## Changelog:

[General] [Added] - Recognize dictionary type in codegen

Pull Request resolved: #37206

Test Plan:
```
yarn jest react-native-codegen
yarn jest react-native-codegen-typescript-test
```

Reviewed By: cipolleschi

Differential Revision: D45563340

Pulled By: dmytrorykun

fbshipit-source-id: 9a9ce36df6ded6d42d35c3dcb6fb0eaca16c4458
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed May 4, 2023
1 parent 8c8f7a5 commit 4fd8f40
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ export interface NativeModuleEnumDeclarationWithMembers {

export interface NativeModuleGenericObjectTypeAnnotation {
readonly type: 'GenericObjectTypeAnnotation';
readonly dictionaryValueType?: Nullable<NativeModuleTypeAnnotation> | undefined;
}

export interface NativeModuleTypeAliasTypeAnnotation {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ export type NativeModuleEnumDeclarationWithMembers = {

export type NativeModuleGenericObjectTypeAnnotation = $ReadOnly<{
type: 'GenericObjectTypeAnnotation',

// a dictionary type is codegen as "Object"
// but we know all its members are in the same type
// when it happens, the following field is non-null
dictionaryValueType?: Nullable<NativeModuleTypeAnnotation>,
}>;

export type NativeModuleTypeAliasTypeAnnotation = $ReadOnly<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,26 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'NullableTypeAnnotation',
'typeAnnotation': {
'type': 'NumberTypeAnnotation'
}
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'NullableTypeAnnotation',
'typeAnnotation': {
'type': 'NumberTypeAnnotation'
}
}
}
}
]
Expand All @@ -220,14 +232,20 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'StringTypeAnnotation'
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'StringTypeAnnotation'
}
}
}
]
Expand Down Expand Up @@ -2165,7 +2183,11 @@ exports[`RN Codegen Flow Parser can generate fixture PROMISE_WITH_COMMONLY_USED_
'returnTypeAnnotation': {
'type': 'PromiseTypeAnnotation',
'elementType': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'TypeAliasTypeAnnotation',
'name': 'CustomObject'
}
}
},
'params': []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const {
const {
emitArrayType,
emitFunction,
emitGenericObject,
emitDictionary,
emitPromise,
emitRootTag,
emitUnion,
Expand Down Expand Up @@ -152,7 +152,7 @@ function translateTypeAnnotation(
// check the property type to prevent developers from using unsupported types
// the return value from `translateTypeAnnotation` is unused
const propertyType = indexers[0].value;
translateTypeAnnotation(
const valueType = translateTypeAnnotation(
hasteModuleName,
propertyType,
types,
Expand All @@ -163,7 +163,7 @@ function translateTypeAnnotation(
parser,
);
// no need to do further checking
return emitGenericObject(nullable);
return emitDictionary(nullable, valueType);
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ function emitGenericObject(
});
}

function emitDictionary(
nullable: boolean,
valueType: Nullable<NativeModuleTypeAnnotation>,
): Nullable<NativeModuleGenericObjectTypeAnnotation> {
return wrapNullable(nullable, {
type: 'GenericObjectTypeAnnotation',
dictionaryValueType: valueType,
});
}

function emitObject(
nullable: boolean,
properties: Array<$FlowFixMe>,
Expand Down Expand Up @@ -576,6 +586,7 @@ module.exports = {
emitInt32,
emitNumber,
emitGenericObject,
emitDictionary,
emitObject,
emitPromise,
emitRootTag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,26 @@ exports[`RN Codegen TypeScript Parser can generate fixture CXX_ONLY_NATIVE_MODUL
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'NullableTypeAnnotation',
'typeAnnotation': {
'type': 'NumberTypeAnnotation'
}
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'NullableTypeAnnotation',
'typeAnnotation': {
'type': 'NumberTypeAnnotation'
}
}
}
}
]
Expand All @@ -218,14 +230,20 @@ exports[`RN Codegen TypeScript Parser can generate fixture CXX_ONLY_NATIVE_MODUL
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'StringTypeAnnotation'
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'GenericObjectTypeAnnotation'
'type': 'GenericObjectTypeAnnotation',
'dictionaryValueType': {
'type': 'StringTypeAnnotation'
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {parseObjectProperty} = require('../../parsers-commons');
const {
emitArrayType,
emitFunction,
emitGenericObject,
emitDictionary,
emitPromise,
emitRootTag,
emitUnion,
Expand Down Expand Up @@ -309,7 +309,7 @@ function translateTypeAnnotation(
// check the property type to prevent developers from using unsupported types
// the return value from `translateTypeAnnotation` is unused
const propertyType = indexSignatures[0].typeAnnotation;
translateTypeAnnotation(
const valueType = translateTypeAnnotation(
hasteModuleName,
propertyType,
types,
Expand All @@ -320,7 +320,7 @@ function translateTypeAnnotation(
parser,
);
// no need to do further checking
return emitGenericObject(nullable);
return emitDictionary(nullable, valueType);
}
}

Expand Down

0 comments on commit 4fd8f40

Please sign in to comment.