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 like alias in module #35812

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
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