Skip to content

Commit f0c4c29

Browse files
ZihanChen-MSFTfacebook-github-bot
authored andcommitted
Support TypeScript array types for turbo module (module only) (facebook#34183)
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: facebook#34183 Test Plan: `yarn jest` passed in `packages/react-native-codegen` Reviewed By: lunaleaps Differential Revision: D37812638 Pulled By: cipolleschi fbshipit-source-id: d63b0585497a43c274d50e1877baab5d1cc3f8fa
1 parent 89f0900 commit f0c4c29

File tree

3 files changed

+417
-65
lines changed

3 files changed

+417
-65
lines changed

packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,27 @@ export interface Spec extends TurboModule {
322322
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
323323
`;
324324

325+
const NATIVE_MODULE_WITH_BASIC_ARRAY2 = `
326+
/**
327+
* Copyright (c) Meta Platforms, Inc. and affiliates.
328+
*
329+
* This source code is licensed under the MIT license found in the
330+
* LICENSE file in the root directory of this source tree.
331+
*
332+
* @format
333+
*/
334+
335+
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
336+
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
337+
338+
export interface Spec extends TurboModule {
339+
readonly getArray: (arg: string[]) => string[];
340+
readonly getArray: (arg: readonly string[]) => readonly string[];
341+
}
342+
343+
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
344+
`;
345+
325346
const NATIVE_MODULE_WITH_OBJECT_WITH_OBJECT_DEFINED_IN_FILE_AS_PROPERTY = `
326347
/**
327348
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -377,6 +398,28 @@ export interface Spec extends TurboModule {
377398
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
378399
`;
379400

401+
const NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE = `
402+
/**
403+
* Copyright (c) Meta Platforms, Inc. and affiliates.
404+
*
405+
* This source code is licensed under the MIT license found in the
406+
* LICENSE file in the root directory of this source tree.
407+
*
408+
* @format
409+
*/
410+
411+
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
412+
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
413+
414+
export interface Spec extends TurboModule {
415+
readonly getArray: (
416+
arg: [string, string][],
417+
) => (string | number | boolean)[];
418+
}
419+
420+
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
421+
`;
422+
380423
const NATIVE_MODULE_WITH_ARRAY_WITH_ALIAS = `
381424
/**
382425
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -399,6 +442,28 @@ export interface Spec extends TurboModule {
399442
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
400443
`;
401444

445+
const NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS = `
446+
/**
447+
* Copyright (c) Meta Platforms, Inc. and affiliates.
448+
*
449+
* This source code is licensed under the MIT license found in the
450+
* LICENSE file in the root directory of this source tree.
451+
*
452+
* @format
453+
*/
454+
455+
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
456+
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
457+
458+
export type SomeString = string;
459+
460+
export interface Spec extends TurboModule {
461+
readonly getArray: (arg: SomeString[]) => string[];
462+
}
463+
464+
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
465+
`;
466+
402467
const NATIVE_MODULE_WITH_COMPLEX_ARRAY = `
403468
/**
404469
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -421,6 +486,28 @@ export interface Spec extends TurboModule {
421486
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
422487
`;
423488

489+
const NATIVE_MODULE_WITH_COMPLEX_ARRAY2 = `
490+
/**
491+
* Copyright (c) Meta Platforms, Inc. and affiliates.
492+
*
493+
* This source code is licensed under the MIT license found in the
494+
* LICENSE file in the root directory of this source tree.
495+
*
496+
* @format
497+
*/
498+
499+
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
500+
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
501+
502+
export interface Spec extends TurboModule {
503+
readonly getArray: (
504+
arg: string[][][][][],
505+
) => string[][][];
506+
}
507+
508+
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
509+
`;
510+
424511
const NATIVE_MODULE_WITH_PROMISE = `
425512
/**
426513
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -534,6 +621,7 @@ export default TurboModuleRegistry.getEnforcing<Spec>(
534621
module.exports = {
535622
NATIVE_MODULE_WITH_OBJECT_WITH_OBJECT_DEFINED_IN_FILE_AS_PROPERTY,
536623
NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE,
624+
NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE,
537625
NATIVE_MODULE_WITH_FLOAT_AND_INT32,
538626
NATIVE_MODULE_WITH_ALIASES,
539627
NATIVE_MODULE_WITH_NESTED_ALIASES,
@@ -545,8 +633,11 @@ module.exports = {
545633
NATIVE_MODULE_WITH_ROOT_TAG,
546634
NATIVE_MODULE_WITH_NULLABLE_PARAM,
547635
NATIVE_MODULE_WITH_BASIC_ARRAY,
636+
NATIVE_MODULE_WITH_BASIC_ARRAY2,
548637
NATIVE_MODULE_WITH_COMPLEX_ARRAY,
638+
NATIVE_MODULE_WITH_COMPLEX_ARRAY2,
549639
NATIVE_MODULE_WITH_ARRAY_WITH_ALIAS,
640+
NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS,
550641
NATIVE_MODULE_WITH_BASIC_PARAM_TYPES,
551642
NATIVE_MODULE_WITH_CALLBACK,
552643
EMPTY_NATIVE_MODULE,

packages/react-native-codegen/src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,49 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_AR
369369
}"
370370
`;
371371

372+
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_ARRAY2_WITH_ALIAS 1`] = `
373+
"{
374+
'modules': {
375+
'NativeSampleTurboModule': {
376+
'type': 'NativeModule',
377+
'aliases': {},
378+
'spec': {
379+
'properties': [
380+
{
381+
'name': 'getArray',
382+
'optional': false,
383+
'typeAnnotation': {
384+
'type': 'FunctionTypeAnnotation',
385+
'returnTypeAnnotation': {
386+
'type': 'ArrayTypeAnnotation',
387+
'elementType': {
388+
'type': 'StringTypeAnnotation'
389+
}
390+
},
391+
'params': [
392+
{
393+
'name': 'arg',
394+
'optional': false,
395+
'typeAnnotation': {
396+
'type': 'ArrayTypeAnnotation',
397+
'elementType': {
398+
'type': 'StringTypeAnnotation'
399+
}
400+
}
401+
}
402+
]
403+
}
404+
}
405+
]
406+
},
407+
'moduleNames': [
408+
'SampleTurboModule'
409+
]
410+
}
411+
}
412+
}"
413+
`;
414+
372415
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE 1`] = `
373416
"{
374417
'modules': {
@@ -406,6 +449,43 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_AR
406449
}"
407450
`;
408451

452+
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_ARRAY2_WITH_UNION_AND_TOUPLE 1`] = `
453+
"{
454+
'modules': {
455+
'NativeSampleTurboModule': {
456+
'type': 'NativeModule',
457+
'aliases': {},
458+
'spec': {
459+
'properties': [
460+
{
461+
'name': 'getArray',
462+
'optional': false,
463+
'typeAnnotation': {
464+
'type': 'FunctionTypeAnnotation',
465+
'returnTypeAnnotation': {
466+
'type': 'ArrayTypeAnnotation'
467+
},
468+
'params': [
469+
{
470+
'name': 'arg',
471+
'optional': false,
472+
'typeAnnotation': {
473+
'type': 'ArrayTypeAnnotation'
474+
}
475+
}
476+
]
477+
}
478+
}
479+
]
480+
},
481+
'moduleNames': [
482+
'SampleTurboModule'
483+
]
484+
}
485+
}
486+
}"
487+
`;
488+
409489
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BASIC_ARRAY 1`] = `
410490
"{
411491
'modules': {
@@ -474,6 +554,74 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BA
474554
}"
475555
`;
476556

557+
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BASIC_ARRAY2 1`] = `
558+
"{
559+
'modules': {
560+
'NativeSampleTurboModule': {
561+
'type': 'NativeModule',
562+
'aliases': {},
563+
'spec': {
564+
'properties': [
565+
{
566+
'name': 'getArray',
567+
'optional': false,
568+
'typeAnnotation': {
569+
'type': 'FunctionTypeAnnotation',
570+
'returnTypeAnnotation': {
571+
'type': 'ArrayTypeAnnotation',
572+
'elementType': {
573+
'type': 'StringTypeAnnotation'
574+
}
575+
},
576+
'params': [
577+
{
578+
'name': 'arg',
579+
'optional': false,
580+
'typeAnnotation': {
581+
'type': 'ArrayTypeAnnotation',
582+
'elementType': {
583+
'type': 'StringTypeAnnotation'
584+
}
585+
}
586+
}
587+
]
588+
}
589+
},
590+
{
591+
'name': 'getArray',
592+
'optional': false,
593+
'typeAnnotation': {
594+
'type': 'FunctionTypeAnnotation',
595+
'returnTypeAnnotation': {
596+
'type': 'ArrayTypeAnnotation',
597+
'elementType': {
598+
'type': 'StringTypeAnnotation'
599+
}
600+
},
601+
'params': [
602+
{
603+
'name': 'arg',
604+
'optional': false,
605+
'typeAnnotation': {
606+
'type': 'ArrayTypeAnnotation',
607+
'elementType': {
608+
'type': 'StringTypeAnnotation'
609+
}
610+
}
611+
}
612+
]
613+
}
614+
}
615+
]
616+
},
617+
'moduleNames': [
618+
'SampleTurboModule'
619+
]
620+
}
621+
}
622+
}"
623+
`;
624+
477625
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_BASIC_PARAM_TYPES 1`] = `
478626
"{
479627
'modules': {
@@ -691,6 +839,67 @@ exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_CO
691839
}"
692840
`;
693841

842+
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_COMPLEX_ARRAY2 1`] = `
843+
"{
844+
'modules': {
845+
'NativeSampleTurboModule': {
846+
'type': 'NativeModule',
847+
'aliases': {},
848+
'spec': {
849+
'properties': [
850+
{
851+
'name': 'getArray',
852+
'optional': false,
853+
'typeAnnotation': {
854+
'type': 'FunctionTypeAnnotation',
855+
'returnTypeAnnotation': {
856+
'type': 'ArrayTypeAnnotation',
857+
'elementType': {
858+
'type': 'ArrayTypeAnnotation',
859+
'elementType': {
860+
'type': 'ArrayTypeAnnotation',
861+
'elementType': {
862+
'type': 'StringTypeAnnotation'
863+
}
864+
}
865+
}
866+
},
867+
'params': [
868+
{
869+
'name': 'arg',
870+
'optional': false,
871+
'typeAnnotation': {
872+
'type': 'ArrayTypeAnnotation',
873+
'elementType': {
874+
'type': 'ArrayTypeAnnotation',
875+
'elementType': {
876+
'type': 'ArrayTypeAnnotation',
877+
'elementType': {
878+
'type': 'ArrayTypeAnnotation',
879+
'elementType': {
880+
'type': 'ArrayTypeAnnotation',
881+
'elementType': {
882+
'type': 'StringTypeAnnotation'
883+
}
884+
}
885+
}
886+
}
887+
}
888+
}
889+
}
890+
]
891+
}
892+
}
893+
]
894+
},
895+
'moduleNames': [
896+
'SampleTurboModule'
897+
]
898+
}
899+
}
900+
}"
901+
`;
902+
694903
exports[`RN Codegen TypeScript Parser can generate fixture NATIVE_MODULE_WITH_COMPLEX_OBJECTS 1`] = `
695904
"{
696905
'modules': {

0 commit comments

Comments
 (0)