diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 2fea383f97..23678b216f 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -15,17 +15,18 @@ import type { NativeModuleAliasMap, NativeModuleArrayTypeAnnotation, NativeModuleBaseTypeAnnotation, + NativeModuleTypeAnnotation, NativeModulePropertyShape, NativeModuleSchema, Nullable, } from '../../../CodegenSchema.js'; import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; -import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js'; -const {nullGuard} = require('../../parsers-utils'); -const {visit, verifyPlatforms, isModuleRegistryCall} = require('../../utils'); +const {nullGuard} = require('../../parsers-utils'); +const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils'); const {resolveTypeAnnotation, getTypes} = require('../utils.js'); + const { unwrapNullable, wrapNullable, @@ -33,8 +34,9 @@ const { parseObjectProperty, emitUnionTypeAnnotation, translateDefault, - translateFunctionTypeAnnotation, + buildPropertySchema, } = require('../../parsers-commons'); + const { emitBoolean, emitDouble, @@ -66,7 +68,6 @@ const { throwIfMoreThanOneModuleRegistryCalls, throwIfIncorrectModuleRegistryCallTypeParameterParserError, throwIfUntypedModule, - throwIfModuleTypeIsUnsupported, throwIfMoreThanOneModuleInterfaceParserError, } = require('../../error-utils'); @@ -326,52 +327,6 @@ function translateTypeAnnotation( } } -function buildPropertySchema( - hasteModuleName: string, - // TODO(T71778680): This is an ObjectTypeProperty containing either: - // - a FunctionTypeAnnotation or GenericTypeAnnotation - // - a NullableTypeAnnoation containing a FunctionTypeAnnotation or GenericTypeAnnotation - // Flow type this node - property: $FlowFixMe, - types: TypeDeclarationMap, - aliasMap: {...NativeModuleAliasMap}, - tryParse: ParserErrorCapturer, - cxxOnly: boolean, -): NativeModulePropertyShape { - let nullable = false; - let {key, value} = property; - - const methodName: string = key.name; - - ({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types)); - - throwIfModuleTypeIsUnsupported( - hasteModuleName, - property.value, - property.key.name, - value.type, - language, - ); - - return { - name: methodName, - optional: property.optional, - typeAnnotation: wrapNullable( - nullable, - translateFunctionTypeAnnotation( - hasteModuleName, - value, - types, - aliasMap, - tryParse, - cxxOnly, - translateTypeAnnotation, - language, - ), - ), - }; -} - function isModuleInterface(node: $FlowFixMe) { return ( node.type === 'InterfaceDeclaration' && @@ -510,6 +465,9 @@ function buildModuleSchema( aliasMap, tryParse, cxxOnly, + language, + resolveTypeAnnotation, + translateTypeAnnotation, ), })); }) diff --git a/packages/react-native-codegen/src/parsers/parsers-commons.js b/packages/react-native-codegen/src/parsers/parsers-commons.js index c63e9e123f..b62c1029fc 100644 --- a/packages/react-native-codegen/src/parsers/parsers-commons.js +++ b/packages/react-native-codegen/src/parsers/parsers-commons.js @@ -11,6 +11,7 @@ 'use strict'; import type { + Nullable, NamedShape, NativeModuleAliasMap, NativeModuleBaseTypeAnnotation, @@ -19,19 +20,22 @@ import type { NativeModuleTypeAnnotation, NativeModuleFunctionTypeAnnotation, NativeModuleParamTypeAnnotation, + NativeModulePropertyShape, NativeModuleUnionTypeAnnotation, - Nullable, SchemaType, } from '../CodegenSchema.js'; -import type {ParserType} from './errors'; + import type {Parser} from './parser'; +import type {ParserType} from './errors'; import type {ParserErrorCapturer, TypeDeclarationMap} from './utils'; const { throwIfPropertyValueTypeIsUnsupported, throwIfUnsupportedFunctionParamTypeAnnotationParserError, throwIfUnsupportedFunctionReturnTypeAnnotationParserError, + throwIfModuleTypeIsUnsupported, } = require('./error-utils'); + const { MissingTypeParameterGenericParserError, MoreThanOneTypeParameterGenericParserError, @@ -402,6 +406,62 @@ function translateFunctionTypeAnnotation( }; } +function buildPropertySchema( + hasteModuleName: string, + // TODO(T108222691): [TS] Use flow-types for @babel/parser + // TODO(T71778680): [Flow] This is an ObjectTypeProperty containing either: + // - a FunctionTypeAnnotation or GenericTypeAnnotation + // - a NullableTypeAnnoation containing a FunctionTypeAnnotation or GenericTypeAnnotation + // Flow type this node + property: $FlowFixMe, + types: TypeDeclarationMap, + aliasMap: {...NativeModuleAliasMap}, + tryParse: ParserErrorCapturer, + cxxOnly: boolean, + language: ParserType, + resolveTypeAnnotation: $FlowFixMe, + translateTypeAnnotation: $FlowFixMe, +): NativeModulePropertyShape { + let nullable: boolean = false; + let {key, value} = property; + const methodName: string = key.name; + + if (language === 'TypeScript') { + value = + property.type === 'TSMethodSignature' + ? property + : property.typeAnnotation; + } + + ({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types)); + + throwIfModuleTypeIsUnsupported( + hasteModuleName, + property.value, + key.name, + value.type, + language, + ); + + return { + name: methodName, + optional: Boolean(property.optional), + typeAnnotation: wrapNullable( + nullable, + translateFunctionTypeAnnotation( + hasteModuleName, + value, + types, + aliasMap, + tryParse, + cxxOnly, + translateTypeAnnotation, + language, + ), + ), + }; +} + module.exports = { wrapModuleSchema, unwrapNullable, @@ -412,4 +472,5 @@ module.exports = { emitUnionTypeAnnotation, translateDefault, translateFunctionTypeAnnotation, + buildPropertySchema, }; diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index b73b3f8b67..f122081f93 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -22,9 +22,11 @@ import type { } from '../../../CodegenSchema.js'; import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; + const {nullGuard} = require('../../parsers-utils'); -const {visit, isModuleRegistryCall} = require('../../utils'); +const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils'); const {resolveTypeAnnotation, getTypes} = require('../utils.js'); + const { unwrapNullable, wrapNullable, @@ -32,8 +34,9 @@ const { parseObjectProperty, emitUnionTypeAnnotation, translateDefault, - translateFunctionTypeAnnotation, + buildPropertySchema, } = require('../../parsers-commons'); + const { emitBoolean, emitDouble, @@ -50,6 +53,7 @@ const { emitMixedTypeAnnotation, typeAliasResolution, } = require('../../parsers-primitives'); + const { UnsupportedArrayElementTypeAnnotationParserError, UnsupportedGenericParserError, @@ -57,11 +61,8 @@ const { IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); -const {verifyPlatforms} = require('../../utils'); - const { throwIfUntypedModule, - throwIfModuleTypeIsUnsupported, throwIfUnusedModuleInterfaceParserError, throwIfModuleInterfaceNotFound, throwIfModuleInterfaceIsMisnamed, @@ -349,50 +350,6 @@ function translateTypeAnnotation( } } -function buildPropertySchema( - hasteModuleName: string, - // TODO(T108222691): Use flow-types for @babel/parser - property: $FlowFixMe, - types: TypeDeclarationMap, - aliasMap: {...NativeModuleAliasMap}, - tryParse: ParserErrorCapturer, - cxxOnly: boolean, -): NativeModulePropertyShape { - let nullable = false; - let {key} = property; - let value = - property.type === 'TSMethodSignature' ? property : property.typeAnnotation; - - const methodName: string = key.name; - - ({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types)); - throwIfModuleTypeIsUnsupported( - hasteModuleName, - property.value, - property.key.name, - value.type, - language, - ); - - return { - name: methodName, - optional: Boolean(property.optional), - typeAnnotation: wrapNullable( - nullable, - translateFunctionTypeAnnotation( - hasteModuleName, - value, - types, - aliasMap, - tryParse, - cxxOnly, - translateTypeAnnotation, - language, - ), - ), - }; -} - function isModuleInterface(node: $FlowFixMe) { return ( node.type === 'TSInterfaceDeclaration' && @@ -535,6 +492,9 @@ function buildModuleSchema( aliasMap, tryParse, cxxOnly, + language, + resolveTypeAnnotation, + translateTypeAnnotation, ), })); })