From da237df0836170c36b3b88b942c0245479d997f2 Mon Sep 17 00:00:00 2001 From: harshsiriah Date: Thu, 13 Oct 2022 11:46:32 +0530 Subject: [PATCH 1/2] Extracted UnsupportedFunctionReturnTypeAnnotationParserError to throwIfUnsupportedFunctionReturnTypeAnnotationParserError --- .../src/parsers/__tests__/error-utils-test.js | 83 +++++++++++++++++++ .../src/parsers/error-utils.js | 39 +++++++++ .../src/parsers/flow/modules/index.js | 21 +++-- .../src/parsers/typescript/modules/index.js | 21 +++-- 4 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js create mode 100644 packages/react-native-codegen/src/parsers/error-utils.js 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 new file mode 100644 index 000000000000..adaff1bdfcc4 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js @@ -0,0 +1,83 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow strict-local + * @oncall react_native + */ + +'use strict'; + +const { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, +} = require('../error-utils'); + +const { + UnsupportedFunctionReturnTypeAnnotationParserError, +} = require('../errors'); + +describe('throwIfUnsupportedFunctionReturnTypeAnnotationParserError', () => { + const flowReturnTypeAnnotation = { + returnType: '', + }, + nativeModuleName = 'moduleName', + invalidReturnType = 'FunctionTypeAnnotation', + language = 'Flow'; + + it('do not throw error if cxxOnly is true', () => { + const cxxOnly = true, + returnTypeAnnotation = { + type: 'FunctionTypeAnnotation', + }; + + expect(() => { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + flowReturnTypeAnnotation, + invalidReturnType, + language, + cxxOnly, + returnTypeAnnotation, + ); + }).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); + }); + + it('do not throw error if returnTypeAnnotation type is not FunctionTypeAnnotation', () => { + const cxxOnly = false, + returnTypeAnnotation = { + type: '', + }; + + expect(() => { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + flowReturnTypeAnnotation, + invalidReturnType, + language, + cxxOnly, + returnTypeAnnotation, + ); + }).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); + }); + + it('throw error if cxxOnly is false and returnTypeAnnotation type is FunctionTypeAnnotation', () => { + const cxxOnly = false, + returnTypeAnnotation = { + type: 'FunctionTypeAnnotation', + }; + + expect(() => { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + flowReturnTypeAnnotation, + invalidReturnType, + language, + cxxOnly, + returnTypeAnnotation, + ); + }).toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js new file mode 100644 index 000000000000..a6ef6de6ba77 --- /dev/null +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -0,0 +1,39 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * 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 + * @format + */ + +'use strict'; + +import type {ParserType} from './errors'; + +const { + UnsupportedFunctionReturnTypeAnnotationParserError, +} = require('./errors.js'); + +function throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName: string, + flowReturnTypeAnnotation: $FlowFixMe, + invalidReturnType: string, + language: ParserType, + cxxOnly: boolean, + returnTypeAnnotation: $FlowFixMe, +) { + if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') { + throw new UnsupportedFunctionReturnTypeAnnotationParserError( + nativeModuleName, + flowReturnTypeAnnotation.returnType, + 'FunctionTypeAnnotation', + language, + ); + } +} + +module.exports = { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, +}; 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 90ab99c40252..8e65f385907b 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -58,7 +58,6 @@ const { UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, UnsupportedFunctionParamTypeAnnotationParserError, - UnsupportedFunctionReturnTypeAnnotationParserError, UnsupportedEnumDeclarationParserError, UnsupportedUnionTypeAnnotationParserError, UnsupportedModulePropertyParserError, @@ -72,6 +71,10 @@ const { IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); +const { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, +} = require('../../error-utils'); + const language = 'Flow'; function nullGuard(fn: () => T): ?T { @@ -492,14 +495,14 @@ function translateFunctionTypeAnnotation( ), ); - if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') { - throw new UnsupportedFunctionReturnTypeAnnotationParserError( - hasteModuleName, - flowFunctionTypeAnnotation.returnType, - 'FunctionTypeAnnotation', - language, - ); - } + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + hasteModuleName, + flowFunctionTypeAnnotation, + 'FunctionTypeAnnotation', + language, + cxxOnly, + returnTypeAnnotation, + ); return { type: 'FunctionTypeAnnotation', 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 8b4da89cf5a6..9bdf6145d4a4 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -58,7 +58,6 @@ const { UnsupportedGenericParserError, UnsupportedTypeAnnotationParserError, UnsupportedFunctionParamTypeAnnotationParserError, - UnsupportedFunctionReturnTypeAnnotationParserError, UnsupportedEnumDeclarationParserError, UnsupportedUnionTypeAnnotationParserError, UnsupportedModulePropertyParserError, @@ -72,6 +71,10 @@ const { IncorrectModuleRegistryCallArgumentTypeParserError, } = require('../../errors.js'); +const { + throwIfUnsupportedFunctionReturnTypeAnnotationParserError, +} = require('../../error-utils'); + const language = 'TypeScript'; function nullGuard(fn: () => T): ?T { @@ -527,14 +530,14 @@ function translateFunctionTypeAnnotation( ), ); - if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') { - throw new UnsupportedFunctionReturnTypeAnnotationParserError( - hasteModuleName, - typescriptFunctionTypeAnnotation.returnType, - 'FunctionTypeAnnotation', - language, - ); - } + throwIfUnsupportedFunctionReturnTypeAnnotationParserError( + hasteModuleName, + typescriptFunctionTypeAnnotation, + 'FunctionTypeAnnotation', + language, + cxxOnly, + returnTypeAnnotation, + ); return { type: 'FunctionTypeAnnotation', From fa44df13239eb3b58e9f66d5822a0e2986381bef Mon Sep 17 00:00:00 2001 From: harshsiriah Date: Mon, 17 Oct 2022 17:10:13 +0530 Subject: [PATCH 2/2] PR Review changes: Renamed flowReturnTypeAnnotation to returnTypeAnnotation --- .../src/parsers/__tests__/error-utils-test.js | 28 ++++++++----------- .../src/parsers/error-utils.js | 8 +++--- .../src/parsers/flow/modules/index.js | 2 +- .../src/parsers/typescript/modules/index.js | 2 +- 4 files changed, 17 insertions(+), 23 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 2499b1a50cf5..8af2ce43ea74 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 @@ -153,7 +153,7 @@ describe('throwErrorIfWrongNumberOfCallExpressionArgs', () => { }); describe('throwIfUnsupportedFunctionReturnTypeAnnotationParserError', () => { - const flowReturnTypeAnnotation = { + const returnTypeAnnotation = { returnType: '', }, nativeModuleName = 'moduleName', @@ -162,54 +162,48 @@ describe('throwIfUnsupportedFunctionReturnTypeAnnotationParserError', () => { it('do not throw error if cxxOnly is true', () => { const cxxOnly = true, - returnTypeAnnotation = { - type: 'FunctionTypeAnnotation', - }; + returnType = 'FunctionTypeAnnotation'; expect(() => { throwIfUnsupportedFunctionReturnTypeAnnotationParserError( nativeModuleName, - flowReturnTypeAnnotation, + returnTypeAnnotation, invalidReturnType, language, cxxOnly, - returnTypeAnnotation, + returnType, ); }).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); }); it('do not throw error if returnTypeAnnotation type is not FunctionTypeAnnotation', () => { const cxxOnly = false, - returnTypeAnnotation = { - type: '', - }; + returnType = ''; expect(() => { throwIfUnsupportedFunctionReturnTypeAnnotationParserError( nativeModuleName, - flowReturnTypeAnnotation, + returnTypeAnnotation, invalidReturnType, language, cxxOnly, - returnTypeAnnotation, + returnType, ); }).not.toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); }); it('throw error if cxxOnly is false and returnTypeAnnotation type is FunctionTypeAnnotation', () => { const cxxOnly = false, - returnTypeAnnotation = { - type: 'FunctionTypeAnnotation', - }; + returnType = 'FunctionTypeAnnotation'; expect(() => { throwIfUnsupportedFunctionReturnTypeAnnotationParserError( nativeModuleName, - flowReturnTypeAnnotation, + returnTypeAnnotation, invalidReturnType, language, cxxOnly, - returnTypeAnnotation, + returnType, ); }).toThrow(UnsupportedFunctionReturnTypeAnnotationParserError); }); @@ -509,7 +503,7 @@ describe('throwIfUntypedModule', () => { }); }); -describe('throwIfMoreThanOneModuleRegistryCalls', () => { +describe('throwIfModuleTypeIsUnsupported', () => { const {throwIfModuleTypeIsUnsupported} = require('../error-utils.js'); const {UnsupportedModulePropertyParserError} = require('../errors.js'); const hasteModuleName = 'moduleName'; diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index ab292614821d..290cbf0c6b82 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -127,16 +127,16 @@ function throwIfIncorrectModuleRegistryCallTypeParameterParserError( function throwIfUnsupportedFunctionReturnTypeAnnotationParserError( nativeModuleName: string, - flowReturnTypeAnnotation: $FlowFixMe, + returnTypeAnnotation: $FlowFixMe, invalidReturnType: string, language: ParserType, cxxOnly: boolean, - returnTypeAnnotation: $FlowFixMe, + returnType: string, ) { - if (!cxxOnly && returnTypeAnnotation.type === 'FunctionTypeAnnotation') { + if (!cxxOnly && returnType === 'FunctionTypeAnnotation') { throw new UnsupportedFunctionReturnTypeAnnotationParserError( nativeModuleName, - flowReturnTypeAnnotation.returnType, + returnTypeAnnotation.returnType, 'FunctionTypeAnnotation', language, ); 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 87c210b9c838..eb72b92132f2 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -516,7 +516,7 @@ function translateFunctionTypeAnnotation( 'FunctionTypeAnnotation', language, cxxOnly, - returnTypeAnnotation, + returnTypeAnnotation.type, ); return { 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 fbe019996a15..39ff2a303db9 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -532,7 +532,7 @@ function translateFunctionTypeAnnotation( 'FunctionTypeAnnotation', language, cxxOnly, - returnTypeAnnotation, + returnTypeAnnotation.type, ); return {