Skip to content

Commit

Permalink
Turbo module codegen support interface like alias in module (#35812)
Browse files Browse the repository at this point in the history
Summary:
Turbo module codegen support interface like alias in module.

In typescript, interface allows inheritance, but the codegen schema doesn't have such information. In this PR the codegen schema is not changed, interfaces are flattened.

In the next change, I will update the codegen schema so that it allows inheritance, in order to generate more precise C++ code.

## Changelog

[GENERAL] [CHANGED] - Turbo module codegen support interface like alias in module

Pull Request resolved: #35812

Test Plan: `yarn jest react-native-codegen` passed

Reviewed By: cortinico

Differential Revision: D42475368

Pulled By: cipolleschi

fbshipit-source-id: 26025f8a55b600dfea27026c998cd8b5fe752af4
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed Jan 12, 2023
1 parent 0f8dc06 commit 8befb74
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const tsExtraCases = [
'NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE',
'NATIVE_MODULE_WITH_BASIC_ARRAY2',
'NATIVE_MODULE_WITH_COMPLEX_ARRAY2',
'NATIVE_MODULE_WITH_NESTED_INTERFACES',
];
const ignoredCases = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,50 @@ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_NESTED_INTERFACES = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type {TurboModule} from '../RCTExport';
import * as TurboModuleRegistry from '../TurboModuleRegistry';
interface Bar {
z: number
};
interface Base1 {
bar1: Bar,
}
interface Base2 {
bar2: Bar,
}
interface Base3 extends Base2 {
bar3: Bar,
}
interface Foo extends Base1, Base3 {
bar4: Bar,
};
export interface Spec extends TurboModule {
// Exported methods.
foo1: (x: Foo) => Foo;
foo2: (x: Foo) => void;
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_FLOAT_AND_INT32 = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand Down Expand Up @@ -704,6 +748,7 @@ module.exports = {
NATIVE_MODULE_WITH_FLOAT_AND_INT32,
NATIVE_MODULE_WITH_ALIASES,
NATIVE_MODULE_WITH_NESTED_ALIASES,
NATIVE_MODULE_WITH_NESTED_INTERFACES,
NATIVE_MODULE_WITH_PROMISE,
NATIVE_MODULE_WITH_COMPLEX_OBJECTS,
NATIVE_MODULE_WITH_COMPLEX_OBJECTS_WITH_NULLABLE_KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,113 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_NE
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_NESTED_INTERFACES 1`] = `
"{
'modules': {
'NativeSampleTurboModule': {
'type': 'NativeModule',
'aliases': {
'Bar': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'z',
'optional': false,
'typeAnnotation': {
'type': 'NumberTypeAnnotation'
}
}
]
},
'Foo': {
'type': 'ObjectTypeAnnotation',
'properties': [
{
'name': 'bar1',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
},
{
'name': 'bar2',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
},
{
'name': 'bar3',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
},
{
'name': 'bar4',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Bar'
}
}
]
}
},
'spec': {
'properties': [
{
'name': 'foo1',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Foo'
},
'params': [
{
'name': 'x',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Foo'
}
}
]
}
},
{
'name': 'foo2',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'VoidTypeAnnotation'
},
'params': [
{
'name': 'x',
'optional': false,
'typeAnnotation': {
'type': 'TypeAliasTypeAnnotation',
'name': 'Foo'
}
}
]
}
}
]
},
'moduleName': 'SampleTurboModule'
}
}
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_NULLABLE_PARAM 1`] = `
"{
'modules': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type {

import type {Parser} from '../../parser';
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
const {flattenProperties} = require('../components/componentsUtils');

const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils');
Expand Down Expand Up @@ -182,6 +183,41 @@ function translateTypeAnnotation(
}
}
}
case 'TSInterfaceDeclaration': {
const objectTypeAnnotation = {
type: 'ObjectTypeAnnotation',
// $FlowFixMe[missing-type-arg]
properties: (flattenProperties(
[typeAnnotation],
types,
): $ReadOnlyArray<$FlowFixMe>)
.map<?NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>>(
property => {
return tryParse(() => {
return parseObjectProperty(
property,
hasteModuleName,
types,
aliasMap,
tryParse,
cxxOnly,
nullable,
translateTypeAnnotation,
parser,
);
});
},
)
.filter(Boolean),
};

return typeAliasResolution(
typeAliasResolutionStatus,
objectTypeAnnotation,
aliasMap,
nullable,
);
}
case 'TSTypeLiteral': {
// if there is TSIndexSignature, then it is a dictionary
if (typeAnnotation.members) {
Expand Down
18 changes: 12 additions & 6 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,18 @@ function resolveTypeAnnotation(
break;
}

invariant(
resolvedTypeAnnotation.type === 'TSTypeAliasDeclaration',
`GenericTypeAnnotation '${node.typeName.name}' must resolve to a TSTypeAliasDeclaration. Instead, it resolved to a '${resolvedTypeAnnotation.type}'`,
);

node = resolvedTypeAnnotation.typeAnnotation;
switch (resolvedTypeAnnotation.type) {
case 'TSTypeAliasDeclaration':
node = resolvedTypeAnnotation.typeAnnotation;
break;
case 'TSInterfaceDeclaration':
node = resolvedTypeAnnotation;
break;
default:
throw new Error(
`GenericTypeAnnotation '${node.typeName.name}' must resolve to a TSTypeAliasDeclaration or a TSInterfaceDeclaration. Instead, it resolved to a '${resolvedTypeAnnotation.type}'`,
);
}
} else {
break;
}
Expand Down

0 comments on commit 8befb74

Please sign in to comment.