- Extract the content into handleEventHandler (#38805)

Summary:
Part of https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/38805

Test Plan: `yarn test react-native-codegen`

Reviewed By: rshest

Differential Revision: D48100350

Pulled By: cipolleschi

fbshipit-source-id: 5de6deacd50e87ea0ec96147fff7c14ba55e5368
This commit is contained in:
Kyaw Thura
2023-08-07 03:41:39 -07:00
committed by Facebook GitHub Bot
parent f9a63ec005
commit ccd191d79a
3 changed files with 58 additions and 43 deletions
@@ -16,6 +16,7 @@ import type {
EventTypeAnnotation,
} from '../../../CodegenSchema.js';
import type {Parser} from '../../parser';
import type {EventArgumentReturnType} from '../../parsers-commons';
const {
throwIfEventHasNoName,
@@ -25,6 +26,7 @@ const {
const {
getEventArgument,
buildPropertiesForEvent,
handleEventHandler,
} = require('../../parsers-commons');
const {
emitBoolProp,
@@ -178,11 +180,7 @@ function findEventArgumentsAndType(
types: TypeMap,
bubblingType: void | 'direct' | 'bubble',
paperName: ?$FlowFixMe,
): {
argumentProps: $FlowFixMe,
bubblingType: ?('direct' | 'bubble'),
paperTopLevelNameDeprecated: ?$FlowFixMe,
} {
): EventArgumentReturnType {
throwIfEventHasNoName(typeAnnotation, parser);
const name = parser.getTypeAnnotationName(typeAnnotation);
if (name === '$ReadOnly') {
@@ -192,25 +190,12 @@ function findEventArgumentsAndType(
bubblingType,
};
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
const paperTopLevelNameDeprecated =
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
if (
typeAnnotation.typeParameters.params[0].type ===
parser.nullLiteralTypeAnnotation
) {
return {
argumentProps: [],
bubblingType: eventType,
paperTopLevelNameDeprecated,
};
}
return findEventArgumentsAndType(
return handleEventHandler(
name,
typeAnnotation,
parser,
typeAnnotation.typeParameters.params[0],
types,
eventType,
paperTopLevelNameDeprecated,
findEventArgumentsAndType,
);
} else if (types[name]) {
return findEventArgumentsAndType(
@@ -87,6 +87,12 @@ type ExtendedPropResult = {
knownTypeName: 'ReactNativeCoreViewProps',
} | null;
export type EventArgumentReturnType = {
argumentProps: ?$ReadOnlyArray<$FlowFixMe>,
paperTopLevelNameDeprecated: ?$FlowFixMe,
bubblingType: ?'direct' | 'bubble',
};
function wrapModuleSchema(
nativeModuleSchema: NativeModuleSchema,
hasteModuleName: string,
@@ -1083,6 +1089,42 @@ function verifyPropNotAlreadyDefined(
}
}
function handleEventHandler(
name: 'BubblingEventHandler' | 'DirectEventHandler',
typeAnnotation: $FlowFixMe,
parser: Parser,
types: TypeDeclarationMap,
findEventArgumentsAndType: (
parser: Parser,
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
bubblingType: void | 'direct' | 'bubble',
paperName: ?$FlowFixMe,
) => EventArgumentReturnType,
): EventArgumentReturnType {
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
const paperTopLevelNameDeprecated =
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
switch (typeAnnotation.typeParameters.params[0].type) {
case parser.nullLiteralTypeAnnotation:
case parser.undefinedLiteralTypeAnnotation:
return {
argumentProps: [],
bubblingType: eventType,
paperTopLevelNameDeprecated,
};
default:
return findEventArgumentsAndType(
parser,
typeAnnotation.typeParameters.params[0],
types,
eventType,
paperTopLevelNameDeprecated,
);
}
}
module.exports = {
wrapModuleSchema,
unwrapNullable,
@@ -1111,4 +1153,5 @@ module.exports = {
getTypeResolutionStatus,
buildPropertiesForEvent,
verifyPropNotAlreadyDefined,
handleEventHandler,
};
@@ -27,6 +27,7 @@ const {
const {
getEventArgument,
buildPropertiesForEvent,
handleEventHandler,
} = require('../../parsers-commons');
const {
emitBoolProp,
@@ -204,27 +205,13 @@ function findEventArgumentsAndType(
paperName,
);
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
const eventType = name === 'BubblingEventHandler' ? 'bubble' : 'direct';
const paperTopLevelNameDeprecated =
parser.getPaperTopLevelNameDeprecated(typeAnnotation);
switch (typeAnnotation.typeParameters.params[0].type) {
case parser.nullLiteralTypeAnnotation:
case parser.undefinedLiteralTypeAnnotation:
return {
argumentProps: [],
bubblingType: eventType,
paperTopLevelNameDeprecated,
};
default:
return findEventArgumentsAndType(
parser,
typeAnnotation.typeParameters.params[0],
types,
eventType,
paperTopLevelNameDeprecated,
);
}
return handleEventHandler(
name,
typeAnnotation,
parser,
types,
findEventArgumentsAndType,
);
} else if (types[name]) {
let elementType = types[name];
if (elementType.type === 'TSTypeAliasDeclaration') {