diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js index f2aaa32f41f..420b6f6a33f 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js @@ -10,15 +10,10 @@ 'use strict'; -import type { - SchemaType, - FunctionTypeAnnotationParamTypeAnnotation, - FunctionTypeAnnotationReturn, - TypeAliasTypeAnnotation, - ObjectTypeAliasTypeShape, -} from '../../CodegenSchema'; +import type {SchemaType, NativeModuleTypeAnnotation} from '../../CodegenSchema'; -const {getTypeAliasTypeAnnotation} = require('./Utils'); +import type {AliasResolver} from './Utils'; +const {createAliasResolver, getModules} = require('./Utils'); type FilesOutput = Map; @@ -32,8 +27,9 @@ public: };`; -const template = `/** - * Copyright (c) Facebook, Inc. and its affiliates. +const template = ` +/** + * ${'C'}opyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. @@ -54,17 +50,15 @@ namespace react { `; function translatePrimitiveJSTypeToCpp( - typeAnnotation: - | FunctionTypeAnnotationParamTypeAnnotation - | FunctionTypeAnnotationReturn - | TypeAliasTypeAnnotation, + typeAnnotation: NativeModuleTypeAnnotation, createErrorMessage: (typeName: string) => string, - aliases: $ReadOnly<{[aliasName: string]: ObjectTypeAliasTypeShape, ...}>, + resolveAlias: AliasResolver, ) { - const realTypeAnnotation = - typeAnnotation.type === 'TypeAliasTypeAnnotation' - ? getTypeAliasTypeAnnotation(typeAnnotation.name, aliases) - : typeAnnotation; + let realTypeAnnotation = typeAnnotation; + if (realTypeAnnotation.type === 'TypeAliasTypeAnnotation') { + realTypeAnnotation = resolveAlias(realTypeAnnotation.name); + } + switch (realTypeAnnotation.type) { case 'ReservedFunctionValueTypeAnnotation': switch (realTypeAnnotation.name) { @@ -79,6 +73,9 @@ function translatePrimitiveJSTypeToCpp( case 'StringTypeAnnotation': return 'jsi::String'; case 'NumberTypeAnnotation': + return 'double'; + case 'DoubleTypeAnnotation': + return 'double'; case 'FloatTypeAnnotation': return 'double'; case 'Int32TypeAnnotation': @@ -86,6 +83,7 @@ function translatePrimitiveJSTypeToCpp( case 'BooleanTypeAnnotation': return 'bool'; case 'GenericObjectTypeAnnotation': + return 'jsi::Object'; case 'ObjectTypeAnnotation': return 'jsi::Object'; case 'ArrayTypeAnnotation': @@ -95,8 +93,7 @@ function translatePrimitiveJSTypeToCpp( case 'PromiseTypeAnnotation': return 'jsi::Value'; default: - // TODO (T65847278): Figure out why this does not work. - // (type: empty); + (realTypeAnnotation.type: empty); throw new Error(createErrorMessage(realTypeAnnotation.type)); } } @@ -110,21 +107,13 @@ module.exports = { schema: SchemaType, moduleSpecName: string, ): FilesOutput { - const nativeModules = Object.keys(schema.modules) - .map(moduleName => { - const modules = schema.modules[moduleName].nativeModules; - if (modules == null) { - return null; - } - - return modules; - }) - .filter(Boolean) - .reduce((acc, components) => Object.assign(acc, components), {}); + const nativeModules = getModules(schema); const modules = Object.keys(nativeModules) .map(name => { const {aliases, properties} = nativeModules[name]; + const resolveAlias = createAliasResolver(aliases); + const traversedProperties = properties .map(prop => { const traversedArgs = prop.typeAnnotation.params @@ -133,7 +122,7 @@ module.exports = { param.typeAnnotation, typeName => `Unsupported type for param "${param.name}" in ${prop.name}. Found: ${typeName}`, - aliases, + resolveAlias, ); const isObject = translatedParam.startsWith('jsi::'); return ( @@ -151,7 +140,7 @@ module.exports = { prop.typeAnnotation.returnTypeAnnotation, typeName => `Unsupported return type for ${prop.name}. Found: ${typeName}`, - aliases, + resolveAlias, ), ) .replace( diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap index fbdcb15bae2..4e55a634312 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap @@ -2,7 +2,8 @@ exports[`GenerateModuleH can generate fixture COMPLEX_OBJECTS 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -38,7 +39,8 @@ virtual void getArrays(jsi::Runtime &rt, const jsi::Object &options) = 0; exports[`GenerateModuleH can generate fixture EMPTY_NATIVE_MODULES 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -71,7 +73,8 @@ public: exports[`GenerateModuleH can generate fixture NATIVE_MODULES_WITH_TYPE_ALIASES 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -105,7 +108,8 @@ virtual void cropImage(jsi::Runtime &rt, const jsi::Object &cropData) = 0; exports[`GenerateModuleH can generate fixture REAL_MODULE_EXAMPLE 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -163,7 +167,8 @@ virtual void dismissRedbox(jsi::Runtime &rt) = 0; exports[`GenerateModuleH can generate fixture SIMPLE_NATIVE_MODULES 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -206,7 +211,8 @@ virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0; exports[`GenerateModuleH can generate fixture TWO_MODULES_DIFFERENT_FILES 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the @@ -249,7 +255,8 @@ virtual void voidFunc(jsi::Runtime &rt) = 0; exports[`GenerateModuleH can generate fixture TWO_MODULES_SAME_FILE 1`] = ` Map { - "NativeModules.h" => "/** + "NativeModules.h" => " +/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the