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 90f3568c406f2d..2ba9a1a9d02078 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ @@ -19,13 +19,10 @@ import type { NativeModuleSchema, Nullable, } from '../../../CodegenSchema.js'; - import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; -const {nullGuard} = require('../../parsers-utils'); const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils'); const {resolveTypeAnnotation, getTypes} = require('../utils.js'); - const { unwrapNullable, wrapNullable, @@ -35,7 +32,6 @@ const { translateDefault, buildPropertySchema, } = require('../../parsers-commons'); - const { emitBoolean, emitDouble, @@ -51,6 +47,7 @@ const { emitStringish, emitMixedTypeAnnotation, typeAliasResolution, + translateArrayTypeAnnotation, } = require('../../parsers-primitives'); const { @@ -61,7 +58,6 @@ const { const { throwIfModuleInterfaceNotFound, throwIfModuleInterfaceIsMisnamed, - throwIfArrayElementTypeAnnotationIsUnsupported, throwIfUnusedModuleInterfaceParserError, throwIfWrongNumberOfCallExpressionArgs, throwIfMoreThanOneModuleRegistryCalls, @@ -75,60 +71,6 @@ const {FlowParser} = require('../parser.js'); const language = 'Flow'; const parser = new FlowParser(); -function translateArrayTypeAnnotation( - hasteModuleName: string, - types: TypeDeclarationMap, - aliasMap: {...NativeModuleAliasMap}, - cxxOnly: boolean, - flowArrayType: 'Array' | '$ReadOnlyArray', - flowElementType: $FlowFixMe, - nullable: boolean, -): Nullable { - try { - /** - * TODO(T72031674): Migrate all our NativeModule specs to not use - * invalid Array ElementTypes. Then, make the elementType a required - * parameter. - */ - const [elementType, isElementTypeNullable] = unwrapNullable( - translateTypeAnnotation( - hasteModuleName, - flowElementType, - types, - aliasMap, - /** - * TODO(T72031674): Ensure that all ParsingErrors that are thrown - * while parsing the array element don't get captured and collected. - * Why? If we detect any parsing error while parsing the element, - * we should default it to null down the line, here. This is - * the correct behaviour until we migrate all our NativeModule specs - * to be parseable. - */ - nullGuard, - cxxOnly, - ), - ); - - throwIfArrayElementTypeAnnotationIsUnsupported( - hasteModuleName, - flowElementType, - flowArrayType, - elementType.type, - language, - ); - - return wrapNullable(nullable, { - type: 'ArrayTypeAnnotation', - // $FlowFixMe[incompatible-call] - elementType: wrapNullable(isElementTypeNullable, elementType), - }); - } catch (ex) { - return wrapNullable(nullable, { - type: 'ArrayTypeAnnotation', - }); - } -} - function translateTypeAnnotation( hasteModuleName: string, /** @@ -168,6 +110,7 @@ function translateTypeAnnotation( typeAnnotation.type, typeAnnotation.typeParameters.params[0], nullable, + language, ); } case '$ReadOnly': { diff --git a/packages/react-native-codegen/src/parsers/flow/utils.js b/packages/react-native-codegen/src/parsers/flow/utils.js index 86c663db5bd23e..f172d8ec4c8283 100644 --- a/packages/react-native-codegen/src/parsers/flow/utils.js +++ b/packages/react-native-codegen/src/parsers/flow/utils.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 5a3f4b04314f2f..879718cb4691af 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -11,11 +11,13 @@ 'use strict'; import type { + Nullable, BooleanTypeAnnotation, DoubleTypeAnnotation, Int32TypeAnnotation, NativeModuleAliasMap, NativeModuleBaseTypeAnnotation, + NativeModuleTypeAnnotation, NativeModuleFloatTypeAnnotation, NativeModuleFunctionTypeAnnotation, NativeModuleGenericObjectTypeAnnotation, @@ -23,7 +25,6 @@ import type { NativeModuleNumberTypeAnnotation, NativeModulePromiseTypeAnnotation, NativeModuleTypeAliasTypeAnnotation, - Nullable, ObjectTypeAnnotation, ReservedTypeAnnotation, StringTypeAnnotation, @@ -37,11 +38,18 @@ import type { TypeDeclarationMap, } from './utils'; +import {throwIfArrayElementTypeAnnotationIsUnsupported} from './error-utils'; +const {nullGuard} = require('./parsers-utils'); const { assertGenericTypeAnnotationHasExactlyOneTypeParameter, wrapNullable, + unwrapNullable, translateFunctionTypeAnnotation, } = require('./parsers-commons'); +const { + typeScriptTranslateTypeAnnotation, +} = require('./typescript/modules/index'); +const {flowTranslateTypeAnnotation} = require('./flow/modules/index'); function emitBoolean(nullable: boolean): Nullable { return wrapNullable(nullable, { @@ -214,6 +222,70 @@ function emitFloat( }); } +function translateArrayTypeAnnotation( + hasteModuleName: string, + types: TypeDeclarationMap, + aliasMap: {...NativeModuleAliasMap}, + cxxOnly: boolean, + arrayType: 'Array' | 'ReadonlyArray', + elementType: $FlowFixMe, + nullable: boolean, + language: ParserType, +): Nullable { + try { + /** + * TODO(T72031674): Migrate all our NativeModule specs to not use + * invalid Array ElementTypes. Then, make the elementType a required + * parameter. + */ + const [_elementType, isElementTypeNullable] = unwrapNullable( + language === 'TypeScript' + ? typeScriptTranslateTypeAnnotation( + hasteModuleName, + elementType, + types, + aliasMap, + /** + * TODO(T72031674): Ensure that all ParsingErrors that are thrown + * while parsing the array element don't get captured and collected. + * Why? If we detect any parsing error while parsing the element, + * we should default it to null down the line, here. This is + * the correct behaviour until we migrate all our NativeModule specs + * to be parseable. + */ + nullGuard, + cxxOnly, + ) + : flowTranslateTypeAnnotation( + hasteModuleName, + elementType, + types, + aliasMap, + nullGuard, + cxxOnly, + ), + ); + + throwIfArrayElementTypeAnnotationIsUnsupported( + hasteModuleName, + elementType, + arrayType, + _elementType.type, + language, + ); + + return wrapNullable(nullable, { + type: 'ArrayTypeAnnotation', + // $FlowFixMe[incompatible-call] + elementType: wrapNullable(isElementTypeNullable, _elementType), + }); + } catch (ex) { + return wrapNullable(nullable, { + type: 'ArrayTypeAnnotation', + }); + } +} + module.exports = { emitBoolean, emitDouble, @@ -229,4 +301,5 @@ module.exports = { emitStringish, emitMixedTypeAnnotation, typeAliasResolution, + translateArrayTypeAnnotation, }; 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 c184e715479ae5..b7a813aaebe86d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */ @@ -22,13 +22,10 @@ import type { import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils'; -const {nullGuard} = require('../../parsers-utils'); const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils'); const {resolveTypeAnnotation, getTypes} = require('../utils.js'); const { - unwrapNullable, - wrapNullable, assertGenericTypeAnnotationHasExactlyOneTypeParameter, parseObjectProperty, emitUnionTypeAnnotation, @@ -51,10 +48,10 @@ const { emitStringish, emitMixedTypeAnnotation, typeAliasResolution, + translateArrayTypeAnnotation, } = require('../../parsers-primitives'); const { - UnsupportedArrayElementTypeAnnotationParserError, UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, IncorrectModuleRegistryCallArgumentTypeParserError, @@ -69,7 +66,6 @@ const { throwIfMoreThanOneModuleRegistryCalls, throwIfMoreThanOneModuleInterfaceParserError, throwIfIncorrectModuleRegistryCallTypeParameterParserError, - throwIfArrayElementTypeAnnotationIsUnsupported, } = require('../../error-utils'); const {TypeScriptParser} = require('../parser'); @@ -77,60 +73,6 @@ const {TypeScriptParser} = require('../parser'); const language = 'TypeScript'; const parser = new TypeScriptParser(); -function translateArrayTypeAnnotation( - hasteModuleName: string, - types: TypeDeclarationMap, - aliasMap: {...NativeModuleAliasMap}, - cxxOnly: boolean, - tsArrayType: 'Array' | 'ReadonlyArray', - tsElementType: $FlowFixMe, - nullable: boolean, -): Nullable { - try { - /** - * TODO(T72031674): Migrate all our NativeModule specs to not use - * invalid Array ElementTypes. Then, make the elementType a required - * parameter. - */ - const [elementType, isElementTypeNullable] = unwrapNullable( - translateTypeAnnotation( - hasteModuleName, - tsElementType, - types, - aliasMap, - /** - * TODO(T72031674): Ensure that all ParsingErrors that are thrown - * while parsing the array element don't get captured and collected. - * Why? If we detect any parsing error while parsing the element, - * we should default it to null down the line, here. This is - * the correct behaviour until we migrate all our NativeModule specs - * to be parseable. - */ - nullGuard, - cxxOnly, - ), - ); - - throwIfArrayElementTypeAnnotationIsUnsupported( - hasteModuleName, - tsElementType, - tsArrayType, - elementType.type, - language, - ); - - return wrapNullable(nullable, { - type: 'ArrayTypeAnnotation', - // $FlowFixMe[incompatible-call] - elementType: wrapNullable(isElementTypeNullable, elementType), - }); - } catch (ex) { - return wrapNullable(nullable, { - type: 'ArrayTypeAnnotation', - }); - } -} - function translateTypeAnnotation( hasteModuleName: string, /** @@ -155,6 +97,7 @@ function translateTypeAnnotation( 'Array', typeAnnotation.elementType, nullable, + language, ); } case 'TSTypeOperator': { @@ -170,6 +113,7 @@ function translateTypeAnnotation( 'ReadonlyArray', typeAnnotation.typeAnnotation.elementType, nullable, + language, ); } else { throw new UnsupportedGenericParserError( @@ -203,6 +147,7 @@ function translateTypeAnnotation( typeAnnotation.type, typeAnnotation.typeParameters.params[0], nullable, + language, ); } case 'Stringish': { diff --git a/packages/react-native-codegen/src/parsers/typescript/utils.js b/packages/react-native-codegen/src/parsers/typescript/utils.js index 73728847e12b8b..0bc07592f1d252 100644 --- a/packages/react-native-codegen/src/parsers/typescript/utils.js +++ b/packages/react-native-codegen/src/parsers/typescript/utils.js @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow strict-local + * @flow strict * @format */