From f05252ae4e602eebfe9ddd39f731e744360dbaaa Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Wed, 17 May 2023 03:45:34 -0700 Subject: [PATCH] refactor(codegen): extract throwIfArgumentPropsAreNull function in error-utils (#37252) Summary: This PR contains task 117 from https://github.com/facebook/react-native/issues/34872: > [Codegen 117] Extract the code that throws if argumentProps are null in a throwIfArgumentPropsAreNull function in the error-utils.js file. Use it in the [flow/components/events.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/flow/components/events.js#L240-L242) and in the [typescript/components/event.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/typescript/components/events.js#L230-L231) files bypass-github-export-checks ## Changelog: [Internal] [Changed] - Extract throwIfArgumentPropsAreNull function in error-utils Pull Request resolved: https://github.com/facebook/react-native/pull/37252 Test Plan: I tested using Jest and Flow commands. Reviewed By: dmytrorykun Differential Revision: D45865671 Pulled By: cipolleschi fbshipit-source-id: 6711dbed0a5ccd56075e0d13ffa13b222979b8c7 --- .../src/parsers/__tests__/error-utils-test.js | 21 +++++++++ .../src/parsers/error-utils.js | 16 ++++++- .../src/parsers/flow/components/events.js | 27 ++++------- .../parsers/typescript/components/events.js | 47 ++++++++++--------- 4 files changed, 70 insertions(+), 41 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 49174e96ca6..60ee91bf1a4 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 @@ -35,6 +35,7 @@ const { throwIfMoreThanOneCodegenNativecommands, throwIfEventHasNoName, throwIfBubblingTypeIsNull, + throwIfArgumentPropsAreNull, } = require('../error-utils'); const { UnsupportedModulePropertyParserError, @@ -973,3 +974,23 @@ describe('throwIfBubblingTypeIsNull', () => { }).not.toThrow(); }); }); + +describe('throwIfArgumentPropsAreNull', () => { + it('throws an error if unable to determine event arguments', () => { + const argumentProps = null; + const eventName = 'Event'; + + expect(() => { + throwIfArgumentPropsAreNull(argumentProps, eventName); + }).toThrowError(`Unable to determine event arguments for "${eventName}"`); + }); + + it('does not throw an error if able to determine event arguments', () => { + const argumentProps = [{}]; + const eventName = 'Event'; + + expect(() => { + throwIfArgumentPropsAreNull(argumentProps, eventName); + }).not.toThrow(); + }); +}); diff --git a/packages/react-native-codegen/src/parsers/error-utils.js b/packages/react-native-codegen/src/parsers/error-utils.js index df9fe1bd47d..55a46fe3726 100644 --- a/packages/react-native-codegen/src/parsers/error-utils.js +++ b/packages/react-native-codegen/src/parsers/error-utils.js @@ -322,12 +322,25 @@ function throwIfEventHasNoName(typeAnnotation: $FlowFixMe, parser: Parser) { function throwIfBubblingTypeIsNull( bubblingType: ?('direct' | 'bubble'), eventName: string, -) { +): 'direct' | 'bubble' { if (!bubblingType) { throw new Error( `Unable to determine event bubbling type for "${eventName}"`, ); } + + return bubblingType; +} + +function throwIfArgumentPropsAreNull( + argumentProps: ?$ReadOnlyArray<$FlowFixMe>, + eventName: string, +): $ReadOnlyArray<$FlowFixMe> { + if (!argumentProps) { + throw new Error(`Unable to determine event arguments for "${eventName}"`); + } + + return argumentProps; } module.exports = { @@ -352,4 +365,5 @@ module.exports = { throwIfMoreThanOneConfig, throwIfEventHasNoName, throwIfBubblingTypeIsNull, + throwIfArgumentPropsAreNull, }; diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index 667a8e6244d..f4f6d7b9ee1 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -19,6 +19,7 @@ import type {Parser} from '../../parser'; const { throwIfEventHasNoName, throwIfBubblingTypeIsNull, + throwIfArgumentPropsAreNull, } = require('../../error-utils'); const {getEventArgument} = require('../../parsers-commons'); @@ -296,24 +297,22 @@ function buildEventSchema( const {argumentProps, bubblingType, paperTopLevelNameDeprecated} = findEventArgumentsAndType(parser, typeAnnotation, types); - if (!argumentProps) { - throw new Error(`Unable to determine event arguments for "${name}"`); - } - - if (!bubblingType) { - throw new Error(`Unable to determine event arguments for "${name}"`); - } + const nonNullableArgumentProps = throwIfArgumentPropsAreNull( + argumentProps, + name, + ); + const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name); if (paperTopLevelNameDeprecated != null) { return { name, optional, - bubblingType, + bubblingType: nonNullableBubblingType, paperTopLevelNameDeprecated, typeAnnotation: { type: 'EventTypeAnnotation', argument: getEventArgument( - argumentProps, + nonNullableArgumentProps, buildPropertiesForEvent, parser, ), @@ -321,20 +320,14 @@ function buildEventSchema( }; } - if (argumentProps === null) { - throw new Error(`Unable to determine event arguments for "${name}"`); - } - - throwIfBubblingTypeIsNull(bubblingType, name); - return { name, optional, - bubblingType, + bubblingType: nonNullableBubblingType, typeAnnotation: { type: 'EventTypeAnnotation', argument: getEventArgument( - argumentProps, + nonNullableArgumentProps, buildPropertiesForEvent, parser, ), diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index 5bdeb107932..469be6b257a 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -22,6 +22,7 @@ const {parseTopLevelType} = require('../parseTopLevelType'); const { throwIfEventHasNoName, throwIfBubblingTypeIsNull, + throwIfArgumentPropsAreNull, } = require('../../error-utils'); const {getEventArgument} = require('../../parsers-commons'); function getPropertyType( @@ -307,42 +308,42 @@ function buildEventSchema( const {argumentProps, bubblingType, paperTopLevelNameDeprecated} = findEventArgumentsAndType(parser, typeAnnotation, types); - if (!argumentProps) { - throw new Error(`Unable to determine event arguments for "${name}"`); - } else if (!bubblingType) { - throwIfBubblingTypeIsNull(bubblingType, name); - } else { - if (paperTopLevelNameDeprecated != null) { - return { - name, - optional, - bubblingType, - paperTopLevelNameDeprecated, - typeAnnotation: { - type: 'EventTypeAnnotation', - argument: getEventArgument( - argumentProps, - buildPropertiesForEvent, - parser, - ), - }, - }; - } + const nonNullableArgumentProps = throwIfArgumentPropsAreNull( + argumentProps, + name, + ); + const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name); + if (paperTopLevelNameDeprecated != null) { return { name, optional, - bubblingType, + bubblingType: nonNullableBubblingType, + paperTopLevelNameDeprecated, typeAnnotation: { type: 'EventTypeAnnotation', argument: getEventArgument( - argumentProps, + nonNullableArgumentProps, buildPropertiesForEvent, parser, ), }, }; } + + return { + name, + optional, + bubblingType: nonNullableBubblingType, + typeAnnotation: { + type: 'EventTypeAnnotation', + argument: getEventArgument( + nonNullableArgumentProps, + buildPropertiesForEvent, + parser, + ), + }, + }; } function getEvents(