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 3597f261498d..0cf31e3e80d9 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -75,6 +75,85 @@ function nullGuard(fn: () => T): ?T { return fn(); } +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, + ), + ); + + if (elementType.type === 'VoidTypeAnnotation') { + throw new UnsupportedArrayElementTypeAnnotationParserError( + hasteModuleName, + flowElementType, + flowArrayType, + 'void', + language, + ); + } + + if (elementType.type === 'PromiseTypeAnnotation') { + throw new UnsupportedArrayElementTypeAnnotationParserError( + hasteModuleName, + flowElementType, + flowArrayType, + 'Promise', + language, + ); + } + + if (elementType.type === 'FunctionTypeAnnotation') { + throw new UnsupportedArrayElementTypeAnnotationParserError( + hasteModuleName, + flowElementType, + flowArrayType, + 'FunctionTypeAnnotation', + language, + ); + } + + const finalTypeAnnotation: NativeModuleArrayTypeAnnotation< + Nullable, + > = { + type: 'ArrayTypeAnnotation', + elementType: wrapNullable(isElementTypeNullable, elementType), + }; + + return wrapNullable(nullable, finalTypeAnnotation); + } catch (ex) { + return wrapNullable(nullable, { + type: 'ArrayTypeAnnotation', + }); + } +} + function translateTypeAnnotation( hasteModuleName: string, /** @@ -111,74 +190,15 @@ function translateTypeAnnotation( language, ); - 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, - typeAnnotation.typeParameters.params[0], - 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, - ), - ); - - if (elementType.type === 'VoidTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - typeAnnotation.typeParameters.params[0], - typeAnnotation.type, - 'void', - language, - ); - } - - if (elementType.type === 'PromiseTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - typeAnnotation.typeParameters.params[0], - typeAnnotation.type, - 'Promise', - language, - ); - } - - if (elementType.type === 'FunctionTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - typeAnnotation.typeParameters.params[0], - typeAnnotation.type, - 'FunctionTypeAnnotation', - language, - ); - } - - const finalTypeAnnotation: NativeModuleArrayTypeAnnotation< - Nullable, - > = { - type: 'ArrayTypeAnnotation', - elementType: wrapNullable(isElementTypeNullable, elementType), - }; - - return wrapNullable(nullable, finalTypeAnnotation); - } catch (ex) { - return wrapNullable(nullable, { - type: 'ArrayTypeAnnotation', - }); - } + return translateArrayTypeAnnotation( + hasteModuleName, + types, + aliasMap, + cxxOnly, + typeAnnotation.type, + typeAnnotation.typeParameters.params[0], + nullable, + ); } case '$ReadOnly': { assertGenericTypeAnnotationHasExactlyOneTypeParameter( 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 5d73f31d5f61..922f23ca7c83 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -82,7 +82,7 @@ function translateArrayTypeAnnotation( cxxOnly: boolean, tsArrayType: 'Array' | 'ReadonlyArray', tsElementType: $FlowFixMe, - nullable: $FlowFixMe, + nullable: boolean, ): Nullable { try { /**