Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize dictionary type in codegen #37206

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -291,6 +291,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 @@ -324,6 +324,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