Recognize dictionary type in codegen (#37206)

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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Zihan Chen (MSFT)
2023-05-04 05:04:12 -07:00
committed by Facebook GitHub Bot
parent 8c8f7a510f
commit 4fd8f405be
7 changed files with 72 additions and 15 deletions
+1
View File
@@ -295,6 +295,7 @@ export interface NativeModuleEnumDeclarationWithMembers {
export interface NativeModuleGenericObjectTypeAnnotation {
readonly type: 'GenericObjectTypeAnnotation';
readonly dictionaryValueType?: Nullable<NativeModuleTypeAnnotation> | undefined;
}
export interface NativeModuleTypeAliasTypeAnnotation {
+5
View File
@@ -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<{
@@ -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'
}
}
}
}
]
@@ -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'
}
}
}
]
@@ -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': []
@@ -32,7 +32,7 @@ const {
const {
emitArrayType,
emitFunction,
emitGenericObject,
emitDictionary,
emitPromise,
emitRootTag,
emitUnion,
@@ -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,
@@ -163,7 +163,7 @@ function translateTypeAnnotation(
parser,
);
// no need to do further checking
return emitGenericObject(nullable);
return emitDictionary(nullable, valueType);
}
}
@@ -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>,
@@ -576,6 +586,7 @@ module.exports = {
emitInt32,
emitNumber,
emitGenericObject,
emitDictionary,
emitObject,
emitPromise,
emitRootTag,
@@ -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'
}
}
}
}
]
@@ -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'
}
}
}
]
@@ -35,7 +35,7 @@ const {parseObjectProperty} = require('../../parsers-commons');
const {
emitArrayType,
emitFunction,
emitGenericObject,
emitDictionary,
emitPromise,
emitRootTag,
emitUnion,
@@ -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,
@@ -320,7 +320,7 @@ function translateTypeAnnotation(
parser,
);
// no need to do further checking
return emitGenericObject(nullable);
return emitDictionary(nullable, valueType);
}
}