From cc311ff01ae2fd03b42d3adbfac9f41f5db5412c Mon Sep 17 00:00:00 2001 From: matiassalles99 Date: Tue, 22 Nov 2022 06:16:24 -0800 Subject: [PATCH] =?UTF-8?q?Extract=20the=20UnsupportedArrayElementTypeAnno?= =?UTF-8?q?tationParserError=20in=20its=20o=E2=80=A6=20(#35167)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This PR is part of https://github.com/facebook/react-native/issues/34872 This PR extracts the [UnsupportedArrayElementTypeAnnotationParserError](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L132) in its own throwing function. ## Changelog [Internal] [Changed] - Extract the UnsupportedArrayElementTypeAnnotationParserError in its own throwing function Pull Request resolved: https://github.com/facebook/react-native/pull/35167 Test Plan: Screen Shot 2022-11-02 at 15 21 15 Reviewed By: cipolleschi Differential Revision: D41437971 Pulled By: rshest fbshipit-source-id: 14a6e09297d96f3b57568e0303e5cafff76e6f32 --- .../src/parsers/__tests__/error-utils-test.js | 57 ++++++++++++++++++ .../src/parsers/error-utils.js | 29 +++++++++ .../src/parsers/flow/modules/index.js | 48 ++++----------- .../src/parsers/typescript/modules/index.js | 59 ++++--------------- 4 files changed, 108 insertions(+), 85 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js index ad40475d417..59facf53640 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -23,6 +23,7 @@ const { throwIfModuleTypeIsUnsupported, throwIfUntypedModule, throwIfUnsupportedFunctionParamTypeAnnotationParserError, + throwIfArrayElementTypeAnnotationIsUnsupported, } = require('../error-utils'); const { UnsupportedModulePropertyParserError, @@ -637,3 +638,59 @@ describe('throwIfUnsupportedFunctionParamTypeAnnotationParserError', () => { }).toThrow(UnsupportedFunctionParamTypeAnnotationParserError); }); }); + +describe('throwIfArrayElementTypeAnnotationIsUnsupported', () => { + const { + UnsupportedArrayElementTypeAnnotationParserError, + } = require('../errors.js'); + const moduleName = 'moduleName'; + const language = 'Flow'; + + it('throws the error if it is the type is void type annotation', () => { + expect(() => { + throwIfArrayElementTypeAnnotationIsUnsupported( + moduleName, + undefined, + 'Array', + 'VoidTypeAnnotation', + language, + ); + }).toThrow(UnsupportedArrayElementTypeAnnotationParserError); + }); + + it('throws the error if it is the type is promise type annotation', () => { + expect(() => { + throwIfArrayElementTypeAnnotationIsUnsupported( + moduleName, + undefined, + 'Array', + 'PromiseTypeAnnotation', + language, + ); + }).toThrow(UnsupportedArrayElementTypeAnnotationParserError); + }); + + it('throws the error if it is the type is function type annotation', () => { + expect(() => { + throwIfArrayElementTypeAnnotationIsUnsupported( + moduleName, + undefined, + 'Array', + 'FunctionTypeAnnotation', + language, + ); + }).toThrow(UnsupportedArrayElementTypeAnnotationParserError); + }); + + it('does not throw the error if the type is NativeModuleTypeAnnotation', () => { + expect(() => { + throwIfArrayElementTypeAnnotationIsUnsupported( + moduleName, + undefined, + 'Array', + 'StringTypeAnnotation', + language, + ); + }).not.toThrow(UnsupportedArrayElementTypeAnnotationParserError); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 31e81e5f852..d835d05773e 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -27,6 +27,7 @@ const { UnsupportedModulePropertyParserError, MoreThanOneModuleInterfaceParserError, UnsupportedFunctionParamTypeAnnotationParserError, + UnsupportedArrayElementTypeAnnotationParserError, } = require('./errors.js'); function throwIfModuleInterfaceIsMisnamed( @@ -250,6 +251,33 @@ function throwIfUnsupportedFunctionParamTypeAnnotationParserError( ); } +function throwIfArrayElementTypeAnnotationIsUnsupported( + hasteModuleName: string, + flowElementType: $FlowFixMe, + flowArrayType: 'Array' | '$ReadOnlyArray' | 'ReadonlyArray', + type: string, + language: ParserType, +) { + const TypeMap = { + FunctionTypeAnnotation: 'FunctionTypeAnnotation', + VoidTypeAnnotation: 'void', + PromiseTypeAnnotation: 'Promise', + // TODO: Added as a work-around for now until TupleTypeAnnotation are fully supported in both flow and TS + // Right now they are partially treated as UnionTypeAnnotation + UnionTypeAnnotation: 'UnionTypeAnnotation', + }; + + if (type in TypeMap) { + throw new UnsupportedArrayElementTypeAnnotationParserError( + hasteModuleName, + flowElementType, + flowArrayType, + TypeMap[type], + language, + ); + } +} + module.exports = { throwIfModuleInterfaceIsMisnamed, throwIfUnsupportedFunctionReturnTypeAnnotationParserError, @@ -263,4 +291,5 @@ module.exports = { throwIfModuleTypeIsUnsupported, throwIfMoreThanOneModuleInterfaceParserError, throwIfUnsupportedFunctionParamTypeAnnotationParserError, + throwIfArrayElementTypeAnnotationIsUnsupported, }; 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 23678b216f7..90f3568c406 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -13,7 +13,6 @@ import type { NamedShape, NativeModuleAliasMap, - NativeModuleArrayTypeAnnotation, NativeModuleBaseTypeAnnotation, NativeModuleTypeAnnotation, NativeModulePropertyShape, @@ -55,7 +54,6 @@ const { } = require('../../parsers-primitives'); const { - UnsupportedArrayElementTypeAnnotationParserError, UnsupportedTypeAnnotationParserError, IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); @@ -63,6 +61,7 @@ const { const { throwIfModuleInterfaceNotFound, throwIfModuleInterfaceIsMisnamed, + throwIfArrayElementTypeAnnotationIsUnsupported, throwIfUnusedModuleInterfaceParserError, throwIfWrongNumberOfCallExpressionArgs, throwIfMoreThanOneModuleRegistryCalls, @@ -110,44 +109,19 @@ function translateArrayTypeAnnotation( ), ); - if (elementType.type === 'VoidTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - flowElementType, - flowArrayType, - 'void', - language, - ); - } + throwIfArrayElementTypeAnnotationIsUnsupported( + hasteModuleName, + flowElementType, + flowArrayType, + elementType.type, + 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, - > = { + return wrapNullable(nullable, { type: 'ArrayTypeAnnotation', + // $FlowFixMe[incompatible-call] elementType: wrapNullable(isElementTypeNullable, elementType), - }; - - return wrapNullable(nullable, finalTypeAnnotation); + }); } catch (ex) { return wrapNullable(nullable, { type: 'ArrayTypeAnnotation', 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 f122081f93f..c184e715479 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -13,7 +13,6 @@ import type { NamedShape, NativeModuleAliasMap, - NativeModuleArrayTypeAnnotation, NativeModuleBaseTypeAnnotation, NativeModulePropertyShape, NativeModuleTypeAnnotation, @@ -70,6 +69,7 @@ const { throwIfMoreThanOneModuleRegistryCalls, throwIfMoreThanOneModuleInterfaceParserError, throwIfIncorrectModuleRegistryCallTypeParameterParserError, + throwIfArrayElementTypeAnnotationIsUnsupported, } = require('../../error-utils'); const {TypeScriptParser} = require('../parser'); @@ -111,56 +111,19 @@ function translateArrayTypeAnnotation( ), ); - if (elementType.type === 'VoidTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - tsElementType, - tsArrayType, - 'void', - language, - ); - } + throwIfArrayElementTypeAnnotationIsUnsupported( + hasteModuleName, + tsElementType, + tsArrayType, + elementType.type, + language, + ); - if (elementType.type === 'PromiseTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - tsElementType, - tsArrayType, - 'Promise', - language, - ); - } - - if (elementType.type === 'FunctionTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - tsElementType, - tsArrayType, - 'FunctionTypeAnnotation', - language, - ); - } - - // TODO: Added as a work-around for now until TupleTypeAnnotation are fully supported in both flow and TS - // Right now they are partially treated as UnionTypeAnnotation - if (elementType.type === 'UnionTypeAnnotation') { - throw new UnsupportedArrayElementTypeAnnotationParserError( - hasteModuleName, - tsElementType, - tsArrayType, - 'UnionTypeAnnotation', - language, - ); - } - - const finalTypeAnnotation: NativeModuleArrayTypeAnnotation< - Nullable, - > = { + return wrapNullable(nullable, { type: 'ArrayTypeAnnotation', + // $FlowFixMe[incompatible-call] elementType: wrapNullable(isElementTypeNullable, elementType), - }; - - return wrapNullable(nullable, finalTypeAnnotation); + }); } catch (ex) { return wrapNullable(nullable, { type: 'ArrayTypeAnnotation',