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 9c0b44d9396abc..00e13afcf81446 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, /** @@ -178,6 +120,8 @@ function translateTypeAnnotation( typeAnnotation.type, typeAnnotation.typeParameters.params[0], nullable, + language, + translateTypeAnnotation, ); } 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 c6404dbfabb5a4..3e482cdf6247a7 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,9 +38,14 @@ import type { TypeDeclarationMap, } from './utils'; +const { + throwIfArrayElementTypeAnnotationIsUnsupported, +} = require('./error-utils'); +const {nullGuard} = require('./parsers-utils'); const { assertGenericTypeAnnotationHasExactlyOneTypeParameter, wrapNullable, + unwrapNullable, translateFunctionTypeAnnotation, } = require('./parsers-commons'); @@ -243,6 +249,62 @@ function emitFloat( }); } +function translateArrayTypeAnnotation( + hasteModuleName: string, + types: TypeDeclarationMap, + aliasMap: {...NativeModuleAliasMap}, + cxxOnly: boolean, + arrayType: 'Array' | 'ReadonlyArray', + elementType: $FlowFixMe, + nullable: boolean, + language: ParserType, + translateTypeAnnotation: $FlowFixMe, +): 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, + 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, + ), + ); + + 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, @@ -258,4 +320,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 f9d8a8b226e1ae..7157a09e32aaac 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,8 @@ function translateTypeAnnotation( 'Array', typeAnnotation.elementType, nullable, + language, + translateTypeAnnotation, ); } case 'TSTypeOperator': { @@ -170,6 +114,8 @@ function translateTypeAnnotation( 'ReadonlyArray', typeAnnotation.typeAnnotation.elementType, nullable, + language, + translateTypeAnnotation, ); } else { throw new UnsupportedGenericParserError( @@ -213,6 +159,8 @@ function translateTypeAnnotation( typeAnnotation.type, typeAnnotation.typeParameters.params[0], nullable, + language, + translateTypeAnnotation, ); } case 'Stringish': { diff --git a/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js b/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js index 79b7ed30ebb6a3..eb43a78a0de9d1 100644 --- a/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.js +++ b/packages/react-native-codegen/src/parsers/typescript/parseTopLevelType.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/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 */