From 5ff8895c2705b38d8ff0c0804055fc80a20ff282 Mon Sep 17 00:00:00 2001 From: gabrieldonadel Date: Mon, 27 Feb 2023 03:24:55 -0800 Subject: [PATCH] chore: Create Codegen throwIfPartialWithMoreParameter function (#36300) Summary: This PR creates a Codegen `throwIfPartialWithMoreParameter` function and extracts the error-emitting code from [Flow](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/flow/modules/index.js#L165-L169) and [Typescript](https://github.com/facebook/react-native/blob/main/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L246-L250) index files as requested on https://github.com/facebook/react-native/issues/34872. This also adds unit tests to the new function. ## Changelog [Internal] [Changed] - Create codegen `throwIfPartialWithMoreParameter` function and extract the `error-emitting` code from Flow and Typescript index files. Pull Request resolved: https://github.com/facebook/react-native/pull/36300 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/221445922-b2c21ad2-012e-4266-9456-6c781b0de7f0.png) Reviewed By: cipolleschi Differential Revision: D43616254 Pulled By: rshest fbshipit-source-id: 4742aec56598be6bac895809b96d0879c37fcfe1 --- .../src/parsers/__tests__/error-utils-test.js | 24 +++++++++++++++++++ .../src/parsers/error-utils.js | 7 ++++++ .../src/parsers/flow/modules/index.js | 7 ++---- .../src/parsers/typescript/modules/index.js | 7 ++---- 4 files changed, 35 insertions(+), 10 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 48ceb29419..3de22f258b 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 @@ -26,6 +26,7 @@ const { throwIfUnsupportedFunctionParamTypeAnnotationParserError, throwIfArrayElementTypeAnnotationIsUnsupported, throwIfPartialNotAnnotatingTypeParameter, + throwIfPartialWithMoreParameter, } = require('../error-utils'); const { UnsupportedModulePropertyParserError, @@ -796,3 +797,26 @@ describe('throwIfPartialNotAnnotatingTypeParameter', () => { }).not.toThrowError(); }); }); + +describe('throwIfPartialWithMoreParameter', () => { + it('throw error if Partial does not have exactly one parameter', () => { + const typeAnnotation = { + type: '', + typeParameters: {params: [{}, {}]}, + }; + expect(() => { + throwIfPartialWithMoreParameter(typeAnnotation); + }).toThrowError('Partials only support annotating exactly one parameter.'); + }); + + it('does not throw error if Partial has exactly one parameter', () => { + const typeAnnotation = { + type: '', + typeParameters: {params: [{}]}, + }; + + expect(() => { + throwIfPartialWithMoreParameter(typeAnnotation); + }).not.toThrowError(); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index 5bddcf4d3f..4ee09248b2 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -296,6 +296,12 @@ function throwIfPartialNotAnnotatingTypeParameter( } } +function throwIfPartialWithMoreParameter(typeAnnotation: $FlowFixMe) { + if (typeAnnotation.typeParameters.params.length !== 1) { + throw new Error('Partials only support annotating exactly one parameter.'); + } +} + module.exports = { throwIfModuleInterfaceIsMisnamed, throwIfUnsupportedFunctionReturnTypeAnnotationParserError, @@ -312,4 +318,5 @@ module.exports = { throwIfArrayElementTypeAnnotationIsUnsupported, throwIfIncorrectModuleRegistryCallArgument, throwIfPartialNotAnnotatingTypeParameter, + throwIfPartialWithMoreParameter, }; 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 03dff7ef80..29c825ee63 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -70,6 +70,7 @@ const { throwIfUntypedModule, throwIfMoreThanOneModuleInterfaceParserError, throwIfPartialNotAnnotatingTypeParameter, + throwIfPartialWithMoreParameter, } = require('../../error-utils'); const language = 'Flow'; @@ -163,11 +164,7 @@ function translateTypeAnnotation( return emitGenericObject(nullable); } case '$Partial': { - if (typeAnnotation.typeParameters.params.length !== 1) { - throw new Error( - 'Partials only support annotating exactly one parameter.', - ); - } + throwIfPartialWithMoreParameter(typeAnnotation); const annotatedElement = parser.extractAnnotatedElement( typeAnnotation, 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 ab661e0ef1..5a511b51a3 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -76,6 +76,7 @@ const { throwIfIncorrectModuleRegistryCallTypeParameterParserError, throwIfIncorrectModuleRegistryCallArgument, throwIfPartialNotAnnotatingTypeParameter, + throwIfPartialWithMoreParameter, } = require('../../error-utils'); const language = 'TypeScript'; @@ -244,11 +245,7 @@ function translateTypeAnnotation( return emitGenericObject(nullable); } case 'Partial': { - if (typeAnnotation.typeParameters.params.length !== 1) { - throw new Error( - 'Partials only support annotating exactly one parameter.', - ); - } + throwIfPartialWithMoreParameter(typeAnnotation); const annotatedElement = parser.extractAnnotatedElement( typeAnnotation,