Skip to content

Commit

Permalink
Support TypeScript array types for turbo module (module only) (#34183)
Browse files Browse the repository at this point in the history
Summary:
Turbo module codegen supports arrays in both Flow and TypeScript, but it only recognize `Array<T>` and `ReadonlyArray<T>` in TypeScript.

In this change, `T[]` and `readonly T[]` are made recognizable in codegen.

## Changelog

[General] [Added] - Support TypeScript array types for turbo module (module only)

Pull Request resolved: #34183

Test Plan: `yarn jest` passed in `packages/react-native-codegen`

Reviewed By: lunaleaps

Differential Revision: D37812638

Pulled By: cipolleschi

fbshipit-source-id: d63b0585497a43c274d50e1877baab5d1cc3f8fa
  • Loading branch information
ZihanChen-MSFT authored and facebook-github-bot committed Jul 13, 2022
1 parent 89f0900 commit f0c4c29
Show file tree
Hide file tree
Showing 3 changed files with 417 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,27 @@ export interface Spec extends TurboModule {
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_BASIC_ARRAY2 = `
/**
* 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 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly getArray: (arg: string[]) => string[];
readonly getArray: (arg: readonly string[]) => readonly string[];
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_OBJECT_WITH_OBJECT_DEFINED_IN_FILE_AS_PROPERTY = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand Down Expand Up @@ -377,6 +398,28 @@ export interface Spec extends TurboModule {
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE = `
/**
* 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 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly getArray: (
arg: [string, string][],
) => (string | number | boolean)[];
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_ARRAY_WITH_ALIAS = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand All @@ -399,6 +442,28 @@ export interface Spec extends TurboModule {
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS = `
/**
* 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 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
export type SomeString = string;
export interface Spec extends TurboModule {
readonly getArray: (arg: SomeString[]) => string[];
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_COMPLEX_ARRAY = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand All @@ -421,6 +486,28 @@ export interface Spec extends TurboModule {
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_COMPLEX_ARRAY2 = `
/**
* 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 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
readonly getArray: (
arg: string[][][][][],
) => string[][][];
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;

const NATIVE_MODULE_WITH_PROMISE = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand Down Expand Up @@ -534,6 +621,7 @@ export default TurboModuleRegistry.getEnforcing<Spec>(
module.exports = {
NATIVE_MODULE_WITH_OBJECT_WITH_OBJECT_DEFINED_IN_FILE_AS_PROPERTY,
NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE,
NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE,
NATIVE_MODULE_WITH_FLOAT_AND_INT32,
NATIVE_MODULE_WITH_ALIASES,
NATIVE_MODULE_WITH_NESTED_ALIASES,
Expand All @@ -545,8 +633,11 @@ module.exports = {
NATIVE_MODULE_WITH_ROOT_TAG,
NATIVE_MODULE_WITH_NULLABLE_PARAM,
NATIVE_MODULE_WITH_BASIC_ARRAY,
NATIVE_MODULE_WITH_BASIC_ARRAY2,
NATIVE_MODULE_WITH_COMPLEX_ARRAY,
NATIVE_MODULE_WITH_COMPLEX_ARRAY2,
NATIVE_MODULE_WITH_ARRAY_WITH_ALIAS,
NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS,
NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
NATIVE_MODULE_WITH_CALLBACK,
EMPTY_NATIVE_MODULE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,49 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_AR
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS 1`] = `
"{
'modules': {
'NativeSampleTurboModule': {
'type': 'NativeModule',
'aliases': {},
'spec': {
'properties': [
{
'name': 'getArray',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
}
}
]
}
}
]
},
'moduleNames': [
'SampleTurboModule'
]
}
}
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE 1`] = `
"{
'modules': {
Expand Down Expand Up @@ -406,6 +449,43 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_AR
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE 1`] = `
"{
'modules': {
'NativeSampleTurboModule': {
'type': 'NativeModule',
'aliases': {},
'spec': {
'properties': [
{
'name': 'getArray',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'ArrayTypeAnnotation'
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation'
}
}
]
}
}
]
},
'moduleNames': [
'SampleTurboModule'
]
}
}
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BASIC_ARRAY 1`] = `
"{
'modules': {
Expand Down Expand Up @@ -474,6 +554,74 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BA
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BASIC_ARRAY2 1`] = `
"{
'modules': {
'NativeSampleTurboModule': {
'type': 'NativeModule',
'aliases': {},
'spec': {
'properties': [
{
'name': 'getArray',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
}
}
]
}
},
{
'name': 'getArray',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
}
}
]
}
}
]
},
'moduleNames': [
'SampleTurboModule'
]
}
}
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BASIC_PARAM_TYPES 1`] = `
"{
'modules': {
Expand Down Expand Up @@ -691,6 +839,67 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_CO
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_COMPLEX_ARRAY2 1`] = `
"{
'modules': {
'NativeSampleTurboModule': {
'type': 'NativeModule',
'aliases': {},
'spec': {
'properties': [
{
'name': 'getArray',
'optional': false,
'typeAnnotation': {
'type': 'FunctionTypeAnnotation',
'returnTypeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
}
}
},
'params': [
{
'name': 'arg',
'optional': false,
'typeAnnotation': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'ArrayTypeAnnotation',
'elementType': {
'type': 'StringTypeAnnotation'
}
}
}
}
}
}
}
]
}
}
]
},
'moduleNames': [
'SampleTurboModule'
]
}
}
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_COMPLEX_OBJECTS 1`] = `
"{
'modules': {
Expand Down
Loading

0 comments on commit f0c4c29

Please sign in to comment.