Skip to content

Commit ccd191d

Browse files
kyawthura-ggfacebook-github-bot
authored andcommitted
- Extract the content into handleEventHandler (#38805)
Summary: Part of #34872 > Extract the content of the if branches that handle the EventHandlers ([Flow](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/flow/components/events.js#L131-L151), [TypeScript](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/typescript/components/events.js#L150-L171)) into a handleEventHandler function in parsers-commons.js. This will take a name, a typeAnnotation, a parser and a findEventArgumentsAndType function as parameters. Use the switch based approach from TypeScript. ## Changelog: [Internal][Changed]: Extract the content into handleEventHandler Pull Request resolved: #38805 Test Plan: `yarn test react-native-codegen` Reviewed By: rshest Differential Revision: D48100350 Pulled By: cipolleschi fbshipit-source-id: 5de6deacd50e87ea0ec96147fff7c14ba55e5368
1 parent f9a63ec commit ccd191d

File tree

3 files changed

+58
-43
lines changed

3 files changed

+58
-43
lines changed

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
EventTypeAnnotation,
1717
} from '../../../CodegenSchema.js';
1818
import type {Parser} from '../../parser';
19+
import type {EventArgumentReturnType} from '../../parsers-commons';
1920

2021
const {
2122
throwIfEventHasNoName,
@@ -25,6 +26,7 @@ const {
2526
const {
2627
getEventArgument,
2728
buildPropertiesForEvent,
29+
handleEventHandler,
2830
} = require('../../parsers-commons');
2931
const {
3032
emitBoolProp,
@@ -178,11 +180,7 @@ function findEventArgumentsAndType(
178180
types: TypeMap,
179181
bubblingType: void | 'direct' | 'bubble',
180182
paperName: ?$FlowFixMe,
181-
): {
182-
argumentProps: $FlowFixMe,
183-
bubblingType: ?('direct' | 'bubble'),
184-
paperTopLevelNameDeprecated: ?$FlowFixMe,
185-
} {
183+
): EventArgumentReturnType {
186184
throwIfEventHasNoName(typeAnnotation, parser);
187185
const name = parser.getTypeAnnotationName(typeAnnotation);
188186
if (name === '$ReadOnly') {
@@ -192,25 +190,12 @@ function findEventArgumentsAndType(
192190
bubblingType,
193191
};
194192
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
195-
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
196-
const paperTopLevelNameDeprecated =
197-
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
198-
if (
199-
typeAnnotation.typeParameters.params[0].type ===
200-
parser.nullLiteralTypeAnnotation
201-
) {
202-
return {
203-
argumentProps: [],
204-
bubblingType: eventType,
205-
paperTopLevelNameDeprecated,
206-
};
207-
}
208-
return findEventArgumentsAndType(
193+
return handleEventHandler(
194+
name,
195+
typeAnnotation,
209196
parser,
210-
typeAnnotation.typeParameters.params[0],
211197
types,
212-
eventType,
213-
paperTopLevelNameDeprecated,
198+
findEventArgumentsAndType,
214199
);
215200
} else if (types[name]) {
216201
return findEventArgumentsAndType(

packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ type ExtendedPropResult = {
8787
knownTypeName: 'ReactNativeCoreViewProps',
8888
} | null;
8989

90+
export type EventArgumentReturnType = {
91+
argumentProps: ?$ReadOnlyArray<$FlowFixMe>,
92+
paperTopLevelNameDeprecated: ?$FlowFixMe,
93+
bubblingType: ?'direct' | 'bubble',
94+
};
95+
9096
function wrapModuleSchema(
9197
nativeModuleSchema: NativeModuleSchema,
9298
hasteModuleName: string,
@@ -1083,6 +1089,42 @@ function verifyPropNotAlreadyDefined(
10831089
}
10841090
}
10851091

1092+
function handleEventHandler(
1093+
name: 'BubblingEventHandler' | 'DirectEventHandler',
1094+
typeAnnotation: $FlowFixMe,
1095+
parser: Parser,
1096+
types: TypeDeclarationMap,
1097+
findEventArgumentsAndType: (
1098+
parser: Parser,
1099+
typeAnnotation: $FlowFixMe,
1100+
types: TypeDeclarationMap,
1101+
bubblingType: void | 'direct' | 'bubble',
1102+
paperName: ?$FlowFixMe,
1103+
) => EventArgumentReturnType,
1104+
): EventArgumentReturnType {
1105+
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
1106+
const paperTopLevelNameDeprecated =
1107+
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
1108+
1109+
switch (typeAnnotation.typeParameters.params[0].type) {
1110+
case parser.nullLiteralTypeAnnotation:
1111+
case parser.undefinedLiteralTypeAnnotation:
1112+
return {
1113+
argumentProps: [],
1114+
bubblingType: eventType,
1115+
paperTopLevelNameDeprecated,
1116+
};
1117+
default:
1118+
return findEventArgumentsAndType(
1119+
parser,
1120+
typeAnnotation.typeParameters.params[0],
1121+
types,
1122+
eventType,
1123+
paperTopLevelNameDeprecated,
1124+
);
1125+
}
1126+
}
1127+
10861128
module.exports = {
10871129
wrapModuleSchema,
10881130
unwrapNullable,
@@ -1111,4 +1153,5 @@ module.exports = {
11111153
getTypeResolutionStatus,
11121154
buildPropertiesForEvent,
11131155
verifyPropNotAlreadyDefined,
1156+
handleEventHandler,
11141157
};

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
const {
2828
getEventArgument,
2929
buildPropertiesForEvent,
30+
handleEventHandler,
3031
} = require('../../parsers-commons');
3132
const {
3233
emitBoolProp,
@@ -204,27 +205,13 @@ function findEventArgumentsAndType(
204205
paperName,
205206
);
206207
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
207-
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
208-
const paperTopLevelNameDeprecated =
209-
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
210-
211-
switch (typeAnnotation.typeParameters.params[0].type) {
212-
case parser.nullLiteralTypeAnnotation:
213-
case parser.undefinedLiteralTypeAnnotation:
214-
return {
215-
argumentProps: [],
216-
bubblingType: eventType,
217-
paperTopLevelNameDeprecated,
218-
};
219-
default:
220-
return findEventArgumentsAndType(
221-
parser,
222-
typeAnnotation.typeParameters.params[0],
223-
types,
224-
eventType,
225-
paperTopLevelNameDeprecated,
226-
);
227-
}
208+
return handleEventHandler(
209+
name,
210+
typeAnnotation,
211+
parser,
212+
types,
213+
findEventArgumentsAndType,
214+
);
228215
} else if (types[name]) {
229216
let elementType = types[name];
230217
if (elementType.type === 'TSTypeAliasDeclaration') {

0 commit comments

Comments
 (0)