Skip to content

Commit

Permalink
add emitBuildEventSchema to parser commons (#38810)
Browse files Browse the repository at this point in the history
Summary:
[Codegen 119] This PR introduces `emitBuildEventSchema` to parser commons and abstracts the logic out of typescript and parser events as requested on #34872

## Changelog:

[Internal] [Changed] - Add `emitBuildEventSchema` to parser commons and update usages.

Pull Request resolved: #38810

Test Plan:
Run `yarn jest react-native-codegen` locally and ensure CI is green

## Screenshot of test passing locally:

<img width="1047" alt="Screenshot 2023-08-07 at 8 03 49 AM" src="https://github.com/facebook/react-native/assets/64726664/ab87f004-d9f8-4b5f-800b-0e724430301f">

Reviewed By: rshest

Differential Revision: D48155051

Pulled By: cipolleschi

fbshipit-source-id: a3db3f4783d60681b366a871b1d2ee32eb864bc0
  • Loading branch information
siddarthkay authored and facebook-github-bot committed Aug 8, 2023
1 parent e563f0f commit 1d87279
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
38 changes: 11 additions & 27 deletions packages/react-native-codegen/src/parsers/flow/components/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
getEventArgument,
buildPropertiesForEvent,
handleEventHandler,
emitBuildEventSchema,
} = require('../../parsers-commons');
const {
emitBoolProp,
Expand Down Expand Up @@ -245,36 +246,19 @@ function buildEventSchema(
);
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);

if (paperTopLevelNameDeprecated != null) {
return {
name,
optional,
bubblingType: nonNullableBubblingType,
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
parser,
getPropertyType,
),
},
};
}
const argument = getEventArgument(
nonNullableArgumentProps,
parser,
getPropertyType,
);

return {
return emitBuildEventSchema(
paperTopLevelNameDeprecated,
name,
optional,
bubblingType: nonNullableBubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
parser,
getPropertyType,
),
},
};
nonNullableBubblingType,
argument,
);
}

// $FlowFixMe[unclear-type] there's no flowtype for ASTs
Expand Down
33 changes: 33 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
PropTypeAnnotation,
EventTypeAnnotation,
ObjectTypeAnnotation,
EventTypeShape,
} from '../CodegenSchema.js';

import type {Parser} from './parser';
Expand Down Expand Up @@ -1125,6 +1126,37 @@ function handleEventHandler(
}
}

function emitBuildEventSchema(
paperTopLevelNameDeprecated: $FlowFixMe,
name: $FlowFixMe,
optional: $FlowFixMe,
nonNullableBubblingType: 'direct' | 'bubble',
argument: ObjectTypeAnnotation<EventTypeAnnotation>,
): ?EventTypeShape {
if (paperTopLevelNameDeprecated != null) {
return {
name,
optional,
bubblingType: nonNullableBubblingType,
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: argument,
},
};
}

return {
name,
optional,
bubblingType: nonNullableBubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: argument,
},
};
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand Down Expand Up @@ -1154,4 +1186,5 @@ module.exports = {
buildPropertiesForEvent,
verifyPropNotAlreadyDefined,
handleEventHandler,
emitBuildEventSchema,
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
getEventArgument,
buildPropertiesForEvent,
handleEventHandler,
emitBuildEventSchema,
} = require('../../parsers-commons');
const {
emitBoolProp,
Expand Down Expand Up @@ -259,36 +260,19 @@ function buildEventSchema(
);
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);

if (paperTopLevelNameDeprecated != null) {
return {
name,
optional,
bubblingType: nonNullableBubblingType,
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
parser,
getPropertyType,
),
},
};
}
const argument = getEventArgument(
nonNullableArgumentProps,
parser,
getPropertyType,
);

return {
return emitBuildEventSchema(
paperTopLevelNameDeprecated,
name,
optional,
bubblingType: nonNullableBubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
parser,
getPropertyType,
),
},
};
nonNullableBubblingType,
argument,
);
}

function getEvents(
Expand Down

0 comments on commit 1d87279

Please sign in to comment.