Skip to content

Commit f05252a

Browse files
Antoine Doubovetzkyfacebook-github-bot
authored andcommitted
refactor(codegen): extract throwIfArgumentPropsAreNull function in error-utils (#37252)
Summary: This PR contains task 117 from #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: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Extract throwIfArgumentPropsAreNull function in error-utils Pull Request resolved: #37252 Test Plan: I tested using Jest and Flow commands. Reviewed By: dmytrorykun Differential Revision: D45865671 Pulled By: cipolleschi fbshipit-source-id: 6711dbed0a5ccd56075e0d13ffa13b222979b8c7
1 parent b5c01ee commit f05252a

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

packages/react-native-codegen/src/parsers/__tests__/error-utils-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
throwIfMoreThanOneCodegenNativecommands,
3636
throwIfEventHasNoName,
3737
throwIfBubblingTypeIsNull,
38+
throwIfArgumentPropsAreNull,
3839
} = require('../error-utils');
3940
const {
4041
UnsupportedModulePropertyParserError,
@@ -973,3 +974,23 @@ describe('throwIfBubblingTypeIsNull', () => {
973974
}).not.toThrow();
974975
});
975976
});
977+
978+
describe('throwIfArgumentPropsAreNull', () => {
979+
it('throws an error if unable to determine event arguments', () => {
980+
const argumentProps = null;
981+
const eventName = 'Event';
982+
983+
expect(() => {
984+
throwIfArgumentPropsAreNull(argumentProps, eventName);
985+
}).toThrowError(`Unable to determine event arguments for "${eventName}"`);
986+
});
987+
988+
it('does not throw an error if able to determine event arguments', () => {
989+
const argumentProps = [{}];
990+
const eventName = 'Event';
991+
992+
expect(() => {
993+
throwIfArgumentPropsAreNull(argumentProps, eventName);
994+
}).not.toThrow();
995+
});
996+
});

packages/react-native-codegen/src/parsers/error-utils.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,25 @@ function throwIfEventHasNoName(typeAnnotation: $FlowFixMe, parser: Parser) {
322322
function throwIfBubblingTypeIsNull(
323323
bubblingType: ?('direct' | 'bubble'),
324324
eventName: string,
325-
) {
325+
): 'direct' | 'bubble' {
326326
if (!bubblingType) {
327327
throw new Error(
328328
`Unable to determine event bubbling type for "${eventName}"`,
329329
);
330330
}
331+
332+
return bubblingType;
333+
}
334+
335+
function throwIfArgumentPropsAreNull(
336+
argumentProps: ?$ReadOnlyArray<$FlowFixMe>,
337+
eventName: string,
338+
): $ReadOnlyArray<$FlowFixMe> {
339+
if (!argumentProps) {
340+
throw new Error(`Unable to determine event arguments for "${eventName}"`);
341+
}
342+
343+
return argumentProps;
331344
}
332345

333346
module.exports = {
@@ -352,4 +365,5 @@ module.exports = {
352365
throwIfMoreThanOneConfig,
353366
throwIfEventHasNoName,
354367
throwIfBubblingTypeIsNull,
368+
throwIfArgumentPropsAreNull,
355369
};

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {Parser} from '../../parser';
1919
const {
2020
throwIfEventHasNoName,
2121
throwIfBubblingTypeIsNull,
22+
throwIfArgumentPropsAreNull,
2223
} = require('../../error-utils');
2324
const {getEventArgument} = require('../../parsers-commons');
2425

@@ -296,45 +297,37 @@ function buildEventSchema(
296297
const {argumentProps, bubblingType, paperTopLevelNameDeprecated} =
297298
findEventArgumentsAndType(parser, typeAnnotation, types);
298299

299-
if (!argumentProps) {
300-
throw new Error(`Unable to determine event arguments for "${name}"`);
301-
}
302-
303-
if (!bubblingType) {
304-
throw new Error(`Unable to determine event arguments for "${name}"`);
305-
}
300+
const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
301+
argumentProps,
302+
name,
303+
);
304+
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
306305

307306
if (paperTopLevelNameDeprecated != null) {
308307
return {
309308
name,
310309
optional,
311-
bubblingType,
310+
bubblingType: nonNullableBubblingType,
312311
paperTopLevelNameDeprecated,
313312
typeAnnotation: {
314313
type: 'EventTypeAnnotation',
315314
argument: getEventArgument(
316-
argumentProps,
315+
nonNullableArgumentProps,
317316
buildPropertiesForEvent,
318317
parser,
319318
),
320319
},
321320
};
322321
}
323322

324-
if (argumentProps === null) {
325-
throw new Error(`Unable to determine event arguments for "${name}"`);
326-
}
327-
328-
throwIfBubblingTypeIsNull(bubblingType, name);
329-
330323
return {
331324
name,
332325
optional,
333-
bubblingType,
326+
bubblingType: nonNullableBubblingType,
334327
typeAnnotation: {
335328
type: 'EventTypeAnnotation',
336329
argument: getEventArgument(
337-
argumentProps,
330+
nonNullableArgumentProps,
338331
buildPropertiesForEvent,
339332
parser,
340333
),

packages/react-native-codegen/src/parsers/typescript/components/events.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {parseTopLevelType} = require('../parseTopLevelType');
2222
const {
2323
throwIfEventHasNoName,
2424
throwIfBubblingTypeIsNull,
25+
throwIfArgumentPropsAreNull,
2526
} = require('../../error-utils');
2627
const {getEventArgument} = require('../../parsers-commons');
2728
function getPropertyType(
@@ -307,42 +308,42 @@ function buildEventSchema(
307308
const {argumentProps, bubblingType, paperTopLevelNameDeprecated} =
308309
findEventArgumentsAndType(parser, typeAnnotation, types);
309310

310-
if (!argumentProps) {
311-
throw new Error(`Unable to determine event arguments for "${name}"`);
312-
} else if (!bubblingType) {
313-
throwIfBubblingTypeIsNull(bubblingType, name);
314-
} else {
315-
if (paperTopLevelNameDeprecated != null) {
316-
return {
317-
name,
318-
optional,
319-
bubblingType,
320-
paperTopLevelNameDeprecated,
321-
typeAnnotation: {
322-
type: 'EventTypeAnnotation',
323-
argument: getEventArgument(
324-
argumentProps,
325-
buildPropertiesForEvent,
326-
parser,
327-
),
328-
},
329-
};
330-
}
311+
const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
312+
argumentProps,
313+
name,
314+
);
315+
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
331316

317+
if (paperTopLevelNameDeprecated != null) {
332318
return {
333319
name,
334320
optional,
335-
bubblingType,
321+
bubblingType: nonNullableBubblingType,
322+
paperTopLevelNameDeprecated,
336323
typeAnnotation: {
337324
type: 'EventTypeAnnotation',
338325
argument: getEventArgument(
339-
argumentProps,
326+
nonNullableArgumentProps,
340327
buildPropertiesForEvent,
341328
parser,
342329
),
343330
},
344331
};
345332
}
333+
334+
return {
335+
name,
336+
optional,
337+
bubblingType: nonNullableBubblingType,
338+
typeAnnotation: {
339+
type: 'EventTypeAnnotation',
340+
argument: getEventArgument(
341+
nonNullableArgumentProps,
342+
buildPropertiesForEvent,
343+
parser,
344+
),
345+
},
346+
};
346347
}
347348

348349
function getEvents(

0 commit comments

Comments
 (0)