diff --git a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js index c597a2d1f5dfc9..f54f2bced88744 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/components/__test_fixtures__/fixtures.js @@ -871,13 +871,13 @@ type NativeType = HostComponent; interface NativeCommands { +handleRootTag: (viewRef: React.ElementRef, rootTag: RootTag) => void; +hotspotUpdate: (viewRef: React.ElementRef, x: Int32, y: Int32) => void; - +scrollTo: ( + scrollTo( viewRef: React.ElementRef, x: Float, y: Int32, z: Double, animated: boolean, - ) => void; + ): void; } export const Commands = codegenNativeCommands({ diff --git a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap index abfa4a1f7e36a2..696834f40f79c2 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap @@ -1280,6 +1280,7 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_T 'params': [ { 'name': 'rootTag', + 'optional': false, 'typeAnnotation': { 'type': 'ReservedTypeAnnotation', 'name': 'RootTag' @@ -1299,12 +1300,14 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_T 'params': [ { 'name': 'x', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } @@ -1323,24 +1326,28 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_DEFINED_WITH_ALL_T 'params': [ { 'name': 'x', + 'optional': false, 'typeAnnotation': { 'type': 'FloatTypeAnnotation' } }, { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'z', + 'optional': false, 'typeAnnotation': { 'type': 'DoubleTypeAnnotation' } }, { 'name': 'animated', + 'optional': false, 'typeAnnotation': { 'type': 'BooleanTypeAnnotation' } @@ -2898,12 +2905,14 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_EVENTS_TYPES_EXPOR 'params': [ { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'animated', + 'optional': false, 'typeAnnotation': { 'type': 'BooleanTypeAnnotation' } @@ -2946,12 +2955,14 @@ exports[`RN Codegen Flow Parser can generate fixture COMMANDS_WITH_EXTERNAL_TYPE 'params': [ { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'animated', + 'optional': false, 'typeAnnotation': { 'type': 'BooleanTypeAnnotation' } diff --git a/packages/react-native-codegen/src/parsers/flow/components/commands.js b/packages/react-native-codegen/src/parsers/flow/components/commands.js index e829582ce3116d..4d0b8a62fa6010 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/commands.js +++ b/packages/react-native-codegen/src/parsers/flow/components/commands.js @@ -90,6 +90,7 @@ function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) { return { name: paramName, + optional: false, typeAnnotation: returnType, }; }); diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js index e8668947ce28c1..151bb480daabfe 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js @@ -591,7 +591,7 @@ export enum StringOptions { } export interface Spec extends TurboModule { - +getEnums: (quality: Quality, resolution?: Resolution, floppy: Floppy, stringOptions: StringOptions) => string; + getEnums(quality: Quality, resolution?: Resolution, floppy: Floppy, stringOptions: StringOptions): string; } export default TurboModuleRegistry.getEnforcing('SampleTurboModuleIOS'); diff --git a/packages/react-native-codegen/src/parsers/typescript/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/typescript/components/__test_fixtures__/fixtures.js index 0b15b6cf334f60..de01e0afb4f465 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/__test_fixtures__/fixtures.js @@ -981,13 +981,13 @@ type NativeType = HostComponent; interface NativeCommands { readonly handleRootTag: (viewRef: React.ElementRef, rootTag: RootTag) => void; readonly hotspotUpdate: (viewRef: React.ElementRef, x: Int32, y: Int32) => void; - readonly scrollTo: ( + scrollTo( viewRef: React.ElementRef, x: Float, y: Int32, z: Double, animated: boolean, - ) => void; + ): void; } export const Commands = codegenNativeCommands({ diff --git a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap index 9a31424cba2afa..cbc00e37fc9bac 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap @@ -1985,6 +1985,7 @@ exports[`RN Codegen TypeScript Parser can generate fixture COMMANDS_DEFINED_WITH 'params': [ { 'name': 'rootTag', + 'optional': false, 'typeAnnotation': { 'type': 'ReservedTypeAnnotation', 'name': 'RootTag' @@ -2004,12 +2005,14 @@ exports[`RN Codegen TypeScript Parser can generate fixture COMMANDS_DEFINED_WITH 'params': [ { 'name': 'x', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } @@ -2028,24 +2031,28 @@ exports[`RN Codegen TypeScript Parser can generate fixture COMMANDS_DEFINED_WITH 'params': [ { 'name': 'x', + 'optional': false, 'typeAnnotation': { 'type': 'FloatTypeAnnotation' } }, { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'z', + 'optional': false, 'typeAnnotation': { 'type': 'DoubleTypeAnnotation' } }, { 'name': 'animated', + 'optional': false, 'typeAnnotation': { 'type': 'BooleanTypeAnnotation' } @@ -3603,12 +3610,14 @@ exports[`RN Codegen TypeScript Parser can generate fixture COMMANDS_EVENTS_TYPES 'params': [ { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'animated', + 'optional': false, 'typeAnnotation': { 'type': 'BooleanTypeAnnotation' } @@ -3651,12 +3660,14 @@ exports[`RN Codegen TypeScript Parser can generate fixture COMMANDS_WITH_EXTERNA 'params': [ { 'name': 'y', + 'optional': false, 'typeAnnotation': { 'type': 'Int32TypeAnnotation' } }, { 'name': 'animated', + 'optional': false, 'typeAnnotation': { 'type': 'BooleanTypeAnnotation' } diff --git a/packages/react-native-codegen/src/parsers/typescript/components/commands.js b/packages/react-native-codegen/src/parsers/typescript/components/commands.js index 23e300c39e2f06..5e6d38de9c5531 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/commands.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/commands.js @@ -19,16 +19,13 @@ const {parseTopLevelType} = require('../parseTopLevelType'); type EventTypeAST = Object; -function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) { - const topLevelType = parseTopLevelType( - property.typeAnnotation.typeAnnotation, - types, - ); - const name = property.key.name; - const optional = property.optional || topLevelType.optional; - const value = topLevelType.type; - const firstParam = value.parameters[0].typeAnnotation; - +function buildCommandSchemaInternal( + name: string, + optional: boolean, + parameters: Array<$FlowFixMe>, + types: TypeDeclarationMap, +): NamedShape { + const firstParam = parameters[0].typeAnnotation; if ( !( firstParam.typeAnnotation != null && @@ -42,7 +39,7 @@ function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) { ); } - const params = value.parameters.slice(1).map(param => { + const params = parameters.slice(1).map(param => { const paramName = param.name; const paramValue = parseTopLevelType( param.typeAnnotation.typeAnnotation, @@ -96,6 +93,7 @@ function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) { return { name: paramName, + optional: false, typeAnnotation: returnType, }; }); @@ -113,12 +111,37 @@ function buildCommandSchema(property: EventTypeAST, types: TypeDeclarationMap) { }; } +function buildCommandSchema( + property: EventTypeAST, + types: TypeDeclarationMap, +): NamedShape { + if (property.type === 'TSPropertySignature') { + const topLevelType = parseTopLevelType( + property.typeAnnotation.typeAnnotation, + types, + ); + const name = property.key.name; + const optional = property.optional || topLevelType.optional; + const parameters = topLevelType.type.parameters || topLevelType.type.params; + return buildCommandSchemaInternal(name, optional, parameters, types); + } else { + const name = property.key.name; + const optional = property.optional || false; + const parameters = property.parameters || property.params; + return buildCommandSchemaInternal(name, optional, parameters, types); + } +} + function getCommands( commandTypeAST: $ReadOnlyArray, types: TypeDeclarationMap, ): $ReadOnlyArray> { return commandTypeAST - .filter(property => property.type === 'TSPropertySignature') + .filter( + property => + property.type === 'TSPropertySignature' || + property.type === 'TSMethodSignature', + ) .map(property => buildCommandSchema(property, types)) .filter(Boolean); } diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js index 83483075d5ba49..4c2f48f5d2fecc 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/__test_fixtures__/fixtures.js @@ -412,9 +412,9 @@ 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: ( + getArray( arg: [string, string][], - ) => (string | number | boolean)[]; + ): (string | number | boolean)[]; } export default TurboModuleRegistry.getEnforcing('SampleTurboModule');