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 supports intersection type for TypeScript #36037

Closed
wants to merge 8 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_INTERSECTION_TYPES',
'NATIVE_MODULE_WITH_NESTED_INTERFACES',
];
const ignoredCases = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,50 @@ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');

`;

const NATIVE_MODULE_WITH_INTERSECTION_TYPES = `
/**
* 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';

type Bar = {
z: number
};

type Base1 = {
bar1: Bar,
}

type Base2 = {
bar2: Bar,
}

type Base3 = Base2 & {
bar3: Bar,
}

type Foo = 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 @@ -803,6 +847,7 @@ module.exports = {
NATIVE_MODULE_WITH_ALIASES,
NATIVE_MODULE_WITH_NESTED_ALIASES,
NATIVE_MODULE_WITH_NESTED_INTERFACES,
NATIVE_MODULE_WITH_INTERSECTION_TYPES,
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 @@ -1572,6 +1572,114 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_FL
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_INTERSECTION_TYPES 1`] = `
"{
'modules': {
'NativeSampleTurboModule': {
'type': 'NativeModule',
'aliasMap': {
'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'
}
}
]
}
},
'enumMap': {},
'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_NESTED_ALIASES 1`] = `
"{
'modules': {
Expand Down
167 changes: 101 additions & 66 deletions packages/react-native-codegen/src/parsers/typescript/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import type {
} from '../../../CodegenSchema';

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

const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
Expand Down Expand Up @@ -74,6 +79,64 @@ const {

const language = 'TypeScript';

function translateObjectTypeAnnotation(
hasteModuleName: string,
/**
* TODO(T108222691): Use flow-types for @babel/parser
*/
nullable: boolean,
objectMembers: $ReadOnlyArray<$FlowFixMe>,
typeResolutionStatus: TypeResolutionStatus,
baseTypes: $ReadOnlyArray<string>,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
// $FlowFixMe[missing-type-arg]
const properties = objectMembers
.map<?NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>>(property => {
return tryParse(() => {
return parseObjectProperty(
property,
hasteModuleName,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
nullable,
translateTypeAnnotation,
parser,
);
});
})
.filter(Boolean);

let objectTypeAnnotation;
if (baseTypes.length === 0) {
ZihanChen-MSFT marked this conversation as resolved.
Show resolved Hide resolved
objectTypeAnnotation = {
type: 'ObjectTypeAnnotation',
properties,
};
} else {
objectTypeAnnotation = {
type: 'ObjectTypeAnnotation',
properties,
baseTypes,
};
}

return typeAliasResolution(
typeResolutionStatus,
objectTypeAnnotation,
aliasMap,
nullable,
);
}

function translateTypeAnnotation(
hasteModuleName: string,
/**
Expand Down Expand Up @@ -246,46 +309,36 @@ function translateTypeAnnotation(
);
}

let 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,
enumMap,
tryParse,
cxxOnly,
nullable,
translateTypeAnnotation,
parser,
);
});
},
)
.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(
return translateObjectTypeAnnotation(
hasteModuleName,
nullable,
flattenProperties([typeAnnotation], types),
typeResolutionStatus,
objectTypeAnnotation,
baseTypes,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
);
}
case 'TSIntersectionType': {
return translateObjectTypeAnnotation(
hasteModuleName,
nullable,
flattenProperties(
flattenIntersectionType(typeAnnotation, types),
types,
),
typeResolutionStatus,
[],
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
);
}
case 'TSTypeLiteral': {
Expand Down Expand Up @@ -313,36 +366,18 @@ function translateTypeAnnotation(
}
}

const objectTypeAnnotation = {
type: 'ObjectTypeAnnotation',
// $FlowFixMe[missing-type-arg]
properties: (typeAnnotation.members: Array<$FlowFixMe>)
.map<?NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>>(
property => {
return tryParse(() => {
return parseObjectProperty(
property,
hasteModuleName,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
nullable,
translateTypeAnnotation,
parser,
);
});
},
)
.filter(Boolean),
};

return typeAliasResolution(
return translateObjectTypeAnnotation(
hasteModuleName,
nullable,
typeAnnotation.members,
typeResolutionStatus,
objectTypeAnnotation,
[],
types,
aliasMap,
nullable,
enumMap,
tryParse,
cxxOnly,
parser,
);
}
case 'TSEnumDeclaration': {
Expand Down