Skip to content

Commit

Permalink
- Extract the content into handleEventHandler (#38805)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kyawthura-gg authored and facebook-github-bot committed Aug 7, 2023
1 parent f9a63ec commit ccd191d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
EventTypeAnnotation,
} from '../../../CodegenSchema.js';
import type {Parser} from '../../parser';
import type {EventArgumentReturnType} from '../../parsers-commons';

const {
throwIfEventHasNoName,
Expand All @@ -25,6 +26,7 @@ const {
const {
getEventArgument,
buildPropertiesForEvent,
handleEventHandler,
} = require('../../parsers-commons');
const {
emitBoolProp,
Expand Down Expand Up @@ -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') {
Expand All @@ -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(
Expand Down
43 changes: 43 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1111,4 +1153,5 @@ module.exports = {
getTypeResolutionStatus,
buildPropertiesForEvent,
verifyPropNotAlreadyDefined,
handleEventHandler,
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
const {
getEventArgument,
buildPropertiesForEvent,
handleEventHandler,
} = require('../../parsers-commons');
const {
emitBoolProp,
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit ccd191d

Please sign in to comment.