Skip to content

Commit 8494707

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
Codegen 118: add throwIfBubblingTypeisNull in error-utils and use it in events (#37191)
Summary: part of codegen Issue #34872 > Extract the code that throws if argumentProps are null in a throwIfBubblingTypeisNull 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#L244-L246) 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#L232-L233) files ## 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]: Add throwIfBubblingTypeisNull in error-utils and use it in events Pull Request resolved: #37191 Test Plan: `yarn jest react-native-codegen` Reviewed By: cipolleschi Differential Revision: D45478073 Pulled By: dmytrorykun fbshipit-source-id: f70ce3f32d93170cafcea9c8b8c2ba06e760fa37
1 parent 042179a commit 8494707

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const {
3434
throwIfPartialWithMoreParameter,
3535
throwIfMoreThanOneCodegenNativecommands,
3636
throwIfEventHasNoName,
37+
throwIfBubblingTypeIsNull,
3738
} = require('../error-utils');
3839
const {
3940
UnsupportedModulePropertyParserError,
@@ -950,3 +951,25 @@ describe('throwIfEventHasNoName', () => {
950951
}).not.toThrow();
951952
});
952953
});
954+
955+
describe('throwIfBubblingTypeIsNull', () => {
956+
it('throw an error if unable to determine event bubbling type', () => {
957+
const bubblingType = null;
958+
const eventName = 'Event';
959+
960+
expect(() => {
961+
throwIfBubblingTypeIsNull(bubblingType, eventName);
962+
}).toThrowError(
963+
`Unable to determine event bubbling type for "${eventName}"`,
964+
);
965+
});
966+
967+
it('does not throw an error if able to determine event bubbling type', () => {
968+
const bubblingType = 'direct';
969+
const eventName = 'Event';
970+
971+
expect(() => {
972+
throwIfBubblingTypeIsNull(bubblingType, eventName);
973+
}).not.toThrow();
974+
});
975+
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,17 @@ function throwIfEventHasNoName(typeAnnotation: $FlowFixMe, parser: Parser) {
319319
}
320320
}
321321

322+
function throwIfBubblingTypeIsNull(
323+
bubblingType: ?('direct' | 'bubble'),
324+
eventName: string,
325+
) {
326+
if (!bubblingType) {
327+
throw new Error(
328+
`Unable to determine event bubbling type for "${eventName}"`,
329+
);
330+
}
331+
}
332+
322333
module.exports = {
323334
throwIfModuleInterfaceIsMisnamed,
324335
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
@@ -340,4 +351,5 @@ module.exports = {
340351
throwIfConfigNotfound,
341352
throwIfMoreThanOneConfig,
342353
throwIfEventHasNoName,
354+
throwIfBubblingTypeIsNull,
343355
};

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import type {
1616
EventTypeAnnotation,
1717
} from '../../../CodegenSchema.js';
1818
import type {Parser} from '../../parser';
19-
const {throwIfEventHasNoName} = require('../../error-utils');
19+
const {
20+
throwIfEventHasNoName,
21+
throwIfBubblingTypeIsNull,
22+
} = require('../../error-utils');
2023

2124
function getPropertyType(
2225
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -245,9 +248,7 @@ function buildEventSchema(
245248
throw new Error(`Unable to determine event arguments for "${name}"`);
246249
}
247250

248-
if (bubblingType === null) {
249-
throw new Error(`Unable to determine event arguments for "${name}"`);
250-
}
251+
throwIfBubblingTypeIsNull(bubblingType, name);
251252
}
252253

253254
// $FlowFixMe[unclear-type] there's no flowtype for ASTs

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import type {TypeDeclarationMap} from '../../utils';
1919
import type {Parser} from '../../parser';
2020
const {flattenProperties} = require('./componentsUtils');
2121
const {parseTopLevelType} = require('../parseTopLevelType');
22-
const {throwIfEventHasNoName} = require('../../error-utils');
22+
const {
23+
throwIfEventHasNoName,
24+
throwIfBubblingTypeIsNull,
25+
} = require('../../error-utils');
2326

2427
function getPropertyType(
2528
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -219,7 +222,7 @@ function buildEventSchema(
219222
types: TypeDeclarationMap,
220223
property: EventTypeAST,
221224
parser: Parser,
222-
): EventTypeShape {
225+
): ?EventTypeShape {
223226
// unpack WithDefault, (T) or T|U
224227
const topLevelType = parseTopLevelType(
225228
property.typeAnnotation.typeAnnotation,
@@ -235,7 +238,7 @@ function buildEventSchema(
235238
if (!argumentProps) {
236239
throw new Error(`Unable to determine event arguments for "${name}"`);
237240
} else if (!bubblingType) {
238-
throw new Error(`Unable to determine event bubbling type for "${name}"`);
241+
throwIfBubblingTypeIsNull(bubblingType, name);
239242
} else {
240243
if (paperTopLevelNameDeprecated != null) {
241244
return {
@@ -267,9 +270,9 @@ function getEvents(
267270
types: TypeDeclarationMap,
268271
parser: Parser,
269272
): $ReadOnlyArray<EventTypeShape> {
270-
return eventTypeAST.map(property =>
271-
buildEventSchema(types, property, parser),
272-
);
273+
return eventTypeAST
274+
.map(property => buildEventSchema(types, property, parser))
275+
.filter(Boolean);
273276
}
274277

275278
module.exports = {

0 commit comments

Comments
 (0)