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

Turbo module codegen support interface with inheritance in module #36011

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
3 changes: 3 additions & 0 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export type VoidTypeAnnotation = $ReadOnly<{
export type ObjectTypeAnnotation<+T> = $ReadOnly<{
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<NamedShape<T>>,

// metadata for objects that generated from interfaces
baseTypes?: $ReadOnlyArray<string>,
}>;

type FunctionTypeAnnotation<+P, +R> = $ReadOnly<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,56 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_NE
}
]
},
'Base1': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'bar1',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
}
]
},
'Base2': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'bar2',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
}
]
},
'Base3': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'bar2',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
},
{
'name': 'bar3',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
}
],
'baseTypes': [
'Base2'
]
},
'Foo': {
'type': 'ObjectTypeAnnotation',
'properties': [
Expand Down Expand Up @@ -1556,6 +1606,10 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_NE
'name': 'Bar'
}
}
],
'baseTypes': [
'Base1',
'Base3'
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,26 @@ function translateTypeAnnotation(
}
}
case 'TSInterfaceDeclaration': {
const objectTypeAnnotation = {
const baseTypes = (typeAnnotation.extends ?? []).map(
extend => extend.expression.name,
);
for (const baseType of baseTypes) {
// ensure base types exist and appear in aliasMap
translateTypeAnnotation(
hasteModuleName,
{
type: 'TSTypeReference',
typeName: {type: 'Identifier', name: baseType},
},
types,
aliasMap,
tryParse,
cxxOnly,
parser,
);
}

let objectTypeAnnotation = {
type: 'ObjectTypeAnnotation',
// $FlowFixMe[missing-type-arg]
properties: (flattenProperties(
Expand All @@ -246,8 +265,15 @@ function translateTypeAnnotation(
},
)
.filter(Boolean),
baseTypes,
};

if (objectTypeAnnotation.baseTypes.length === 0) {
// The flow checker does not allow adding a member after an object literal is created
// so here I do it in a reverse way
delete objectTypeAnnotation.baseTypes;
}

return typeAliasResolution(
typeAliasResolutionStatus,
objectTypeAnnotation,
Expand Down