From c388e6c6897d4f1690dea92f01355a953bb779d1 Mon Sep 17 00:00:00 2001 From: MaeIg Date: Fri, 14 Oct 2022 03:09:51 -0700 Subject: [PATCH] Wrap the content of the case Array and case $ReadOnlyArray in Flow into a separate function (#34948) Summary: This PR aims to extract translateArrayTypeAnnotation logic into a separate function as it is done in typescript folder. This will enable us to extract translateArrayTypeAnnotation function into a shared folder in a later step. It is a task of https://github.com/facebook/react-native/issues/34872: > Wrap the content of the case Array: and case ReadOnlyArray in [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L106-L107) into a separate function, as it is for the [TypeScript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L218-L234) parser. This will enable us to unify the two parsers in a later step. ## Changelog [Internal] [Changed] - Wrap the content of the case Array and case $ReadOnlyArray in Flow into a separate function Pull Request resolved: https://github.com/facebook/react-native/pull/34948 Test Plan: yarn flow: image yarn lint: image yarn jest react-native-codegen: image Reviewed By: dmytrorykun Differential Revision: D40296730 Pulled By: cipolleschi fbshipit-source-id: ad2d965046e25ee000ae6e07075976e5a0c78f1a --- .../src/parsers/flow/modules/index.js | 156 ++++++++++-------- .../src/parsers/typescript/modules/index.js | 2 +- 2 files changed, 89 insertions(+), 69 deletions(-) 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 773c49d62ce526..7a59a5d70d66e8 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -80,6 +80,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, /** @@ -116,74 +195,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 c75013154e8bce..c13077aab9fcb7 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -87,7 +87,7 @@ function translateArrayTypeAnnotation( cxxOnly: boolean, tsArrayType: 'Array' | 'ReadonlyArray', tsElementType: $FlowFixMe, - nullable: $FlowFixMe, + nullable: boolean, ): Nullable { try { /**