From bb519ecccbae48b66062d4cbef0bdb9641e7039f Mon Sep 17 00:00:00 2001 From: harshsiriah Date: Fri, 14 Oct 2022 03:09:51 -0700 Subject: [PATCH] Extracted IncorrectModuleRegistryCallTypeParameterParserError to throwIfIncorrectModuleRegistryCallTypeParameterParserError (#34941) Summary: This PR is part of https://github.com/facebook/react-native/issues/34872. This PR extracts `IncorrectModuleRegistryCallTypeParameterParserError` exception to a separate function inside an `error-utils.js` file ## Changelog [Internal] [Changed] - Extract `IncorrectModuleRegistryCallTypeParameterParserError` to a seperate function inside `error-utils.js` Pull Request resolved: https://github.com/facebook/react-native/pull/34941 Test Plan: ```sh yarn jest react-native-codegen ``` Added unit case in `error-utils-test.js` file Screenshot 2022-10-11 at 4 42 03 PM Reviewed By: dmytrorykun Differential Revision: D40296642 Pulled By: cipolleschi fbshipit-source-id: 7c7bba6a4f68e9b8fa4729a7651f22cce6d7ca6e --- .../src/parsers/__tests__/error-utils-test.js | 253 ++++++++++++++++++ .../src/parsers/error-utils.js | 40 +++ .../src/parsers/flow/modules/index.js | 23 +- .../src/parsers/typescript/modules/index.js | 23 +- 4 files changed, 309 insertions(+), 30 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 9faae0e30f3..1f0ee9a7185 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 @@ -16,12 +16,14 @@ const { throwIfMoreThanOneModuleRegistryCalls, throwIfUnusedModuleInterfaceParserError, throwIfWrongNumberOfCallExpressionArgs, + throwIfIncorrectModuleRegistryCallTypeParameterParserError, } = require('../error-utils'); const { ModuleInterfaceNotFoundParserError, MoreThanOneModuleRegistryCallsParserError, UnusedModuleInterfaceParserError, IncorrectModuleRegistryCallArityParserError, + IncorrectModuleRegistryCallTypeParameterParserError, } = require('../errors'); describe('throwIfModuleInterfaceNotFound', () => { @@ -147,3 +149,254 @@ describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => { }).not.toThrow(IncorrectModuleRegistryCallArityParserError); }); }); + +describe('throwIfIncorrectModuleRegistryCallTypeParameterParserError', () => { + const nativeModuleName = 'moduleName'; + const methodName = 'methodName'; + const moduleName = 'moduleName'; + it('throw error if flowTypeArguments type is incorrect', () => { + const flowTypeArguments = { + type: '', + params: [ + { + type: 'GenericTypeAnnotation', + id: { + name: 'Spec', + }, + }, + ], + }; + + const parserType = 'Flow'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + flowTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if flowTypeArguments params length is not 1', () => { + const flowTypeArguments = { + type: 'TypeParameterInstantiation', + params: [], + }; + + const parserType = 'Flow'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + flowTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if flowTypeArguments params type is not GenericTypeAnnotation', () => { + const flowTypeArguments = { + type: 'TypeParameterInstantiation', + params: [ + { + type: '', + id: { + name: 'Spec', + }, + }, + ], + }; + + const parserType = 'Flow'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + flowTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if flowTypeArguments params id name is not Spec', () => { + const flowTypeArguments = { + type: 'TypeParameterInstantiation', + params: [ + { + type: 'GenericTypeAnnotation', + id: { + name: '', + }, + }, + ], + }; + + const parserType = 'Flow'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + flowTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('do not throw error if flowTypeArguments are correct', () => { + const flowTypeArguments = { + type: 'TypeParameterInstantiation', + params: [ + { + type: 'GenericTypeAnnotation', + id: { + name: 'Spec', + }, + }, + ], + }; + + const parserType = 'Flow'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + flowTypeArguments, + methodName, + moduleName, + parserType, + ); + }).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if typeScriptTypeArguments type not correct', () => { + const typeScriptTypeArguments = { + type: '', + params: [ + { + type: 'TSTypeReference', + typeName: { + name: 'Spec', + }, + }, + ], + }; + + const parserType = 'TypeScript'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + typeScriptTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if typeScriptTypeArguments params length is not equal to 1', () => { + const typeScriptTypeArguments = { + type: 'TSTypeParameterInstantiation', + params: [], + }; + + const parserType = 'TypeScript'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + typeScriptTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if typeScriptTypeArguments params type is not TSTypeReference', () => { + const typeScriptTypeArguments = { + type: 'TSTypeParameterInstantiation', + params: [ + { + type: '', + typeName: { + name: 'Spec', + }, + }, + ], + }; + + const parserType = 'TypeScript'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + typeScriptTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('throw error if typeScriptTypeArguments params typeName name is not Spec', () => { + const typeScriptTypeArguments = { + type: 'TSTypeParameterInstantiation', + params: [ + { + type: 'TSTypeReference', + typeName: { + name: '', + }, + }, + ], + }; + + const parserType = 'TypeScript'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + typeScriptTypeArguments, + methodName, + moduleName, + parserType, + ); + }).toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); + + it('do not throw error if typeScriptTypeArguments are correct', () => { + const typeScriptTypeArguments = { + type: 'TSTypeParameterInstantiation', + params: [ + { + type: 'TSTypeReference', + typeName: { + name: 'Spec', + }, + }, + ], + }; + + const parserType = 'TypeScript'; + + expect(() => { + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + typeScriptTypeArguments, + methodName, + moduleName, + parserType, + ); + }).not.toThrow(IncorrectModuleRegistryCallTypeParameterParserError); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 78bfeea43e2..d19d50681eb 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -17,6 +17,7 @@ const { MoreThanOneModuleRegistryCallsParserError, UnusedModuleInterfaceParserError, IncorrectModuleRegistryCallArityParserError, + IncorrectModuleRegistryCallTypeParameterParserError, } = require('./errors.js'); function throwIfModuleInterfaceNotFound( @@ -83,9 +84,48 @@ function throwIfWrongNumberOfCallExpressionArgs( } } +function throwIfIncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName: string, + typeArguments: $FlowFixMe, + methodName: string, + moduleName: string, + language: ParserType, +) { + function throwError() { + throw new IncorrectModuleRegistryCallTypeParameterParserError( + nativeModuleName, + typeArguments, + methodName, + moduleName, + language, + ); + } + + if (language === 'Flow') { + if ( + typeArguments.type !== 'TypeParameterInstantiation' || + typeArguments.params.length !== 1 || + typeArguments.params[0].type !== 'GenericTypeAnnotation' || + typeArguments.params[0].id.name !== 'Spec' + ) { + throwError(); + } + } else if (language === 'TypeScript') { + if ( + typeArguments.type !== 'TSTypeParameterInstantiation' || + typeArguments.params.length !== 1 || + typeArguments.params[0].type !== 'TSTypeReference' || + typeArguments.params[0].typeName.name !== 'Spec' + ) { + throwError(); + } + } +} + module.exports = { throwIfModuleInterfaceNotFound, throwIfMoreThanOneModuleRegistryCalls, throwIfUnusedModuleInterfaceParserError, throwIfWrongNumberOfCallExpressionArgs, + throwIfIncorrectModuleRegistryCallTypeParameterParserError, }; 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 7a8975fcef9..773c49d62ce 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -64,7 +64,6 @@ const { UnsupportedObjectPropertyTypeAnnotationParserError, UnsupportedObjectPropertyValueTypeAnnotationParserError, UntypedModuleRegistryCallParserError, - IncorrectModuleRegistryCallTypeParameterParserError, IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); @@ -72,6 +71,7 @@ const { throwIfModuleInterfaceNotFound, throwIfUnusedModuleInterfaceParserError, throwIfWrongNumberOfCallExpressionArgs, + throwIfIncorrectModuleRegistryCallTypeParameterParserError, } = require('../../error-utils'); const language = 'Flow'; @@ -667,20 +667,13 @@ function buildModuleSchema( ); } - if ( - typeArguments.type !== 'TypeParameterInstantiation' || - typeArguments.params.length !== 1 || - typeArguments.params[0].type !== 'GenericTypeAnnotation' || - typeArguments.params[0].id.name !== 'Spec' - ) { - throw new IncorrectModuleRegistryCallTypeParameterParserError( - hasteModuleName, - typeArguments, - methodName, - $moduleName, - language, - ); - } + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + hasteModuleName, + typeArguments, + methodName, + $moduleName, + language, + ); return $moduleName; }); 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 9ceedec590c..c75013154e8 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -64,7 +64,6 @@ const { UnsupportedObjectPropertyTypeAnnotationParserError, UnsupportedObjectPropertyValueTypeAnnotationParserError, UntypedModuleRegistryCallParserError, - IncorrectModuleRegistryCallTypeParameterParserError, IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); @@ -72,6 +71,7 @@ const { throwIfUnusedModuleInterfaceParserError, throwIfModuleInterfaceNotFound, throwIfWrongNumberOfCallExpressionArgs, + throwIfIncorrectModuleRegistryCallTypeParameterParserError, } = require('../../error-utils'); const language = 'TypeScript'; @@ -701,20 +701,13 @@ function buildModuleSchema( ); } - if ( - typeParameters.type !== 'TSTypeParameterInstantiation' || - typeParameters.params.length !== 1 || - typeParameters.params[0].type !== 'TSTypeReference' || - typeParameters.params[0].typeName.name !== 'Spec' - ) { - throw new IncorrectModuleRegistryCallTypeParameterParserError( - hasteModuleName, - typeParameters, - methodName, - $moduleName, - language, - ); - } + throwIfIncorrectModuleRegistryCallTypeParameterParserError( + hasteModuleName, + typeParameters, + methodName, + $moduleName, + language, + ); return $moduleName; });