Skip to content

Commit

Permalink
extract buildPropertiesForEvent to parsers-commons (#37714)
Browse files Browse the repository at this point in the history
Summary:
## 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
-->

Part of #34872

> Extract buildPropertiesForEvent into parsers-commons.js file. Use the code from either [Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/events.js#L258-L272) or [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L277-L288) which now should be equal. Delete the original ones and use the newly created method instead of those.

[Internal][Changed]: Extract buildPropertiesForEvent and update callsites

Pull Request resolved: #37714

Test Plan: `yarn test`

Reviewed By: cipolleschi

Differential Revision: D46514046

Pulled By: rshest

fbshipit-source-id: 120e9a09180735f8eeb0419b16eac566d5dcc6ba
  • Loading branch information
tarunrajput authored and facebook-github-bot committed Jun 8, 2023
1 parent 3250725 commit 942bd61
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const {
throwIfBubblingTypeIsNull,
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {
getEventArgument,
buildPropertiesForEvent,
} = require('../../parsers-commons');
const {
emitBoolProp,
emitDoubleProp,
Expand Down Expand Up @@ -67,7 +70,7 @@ function getPropertyType(
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.properties.map(member =>
buildPropertiesForEvent(member, parser),
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
};
Expand Down Expand Up @@ -126,7 +129,7 @@ function extractArrayElementType(
return {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.properties.map(member =>
buildPropertiesForEvent(member, parser),
buildPropertiesForEvent(member, parser, getPropertyType),
),
};
case 'ArrayTypeAnnotation':
Expand Down Expand Up @@ -229,19 +232,6 @@ function findEventArgumentsAndType(
}
}
function buildPropertiesForEvent(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
property,
parser: Parser,
): NamedShape<EventTypeAnnotation> {
const name = property.key.name;
const optional = parser.isOptionalProperty(property);
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);

return getPropertyType(name, optional, typeAnnotation, parser);
}

function buildEventSchema(
types: TypeMap,
property: EventTypeAST,
Expand Down Expand Up @@ -283,8 +273,8 @@ function buildEventSchema(
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
getPropertyType,
),
},
};
Expand All @@ -298,8 +288,8 @@ function buildEventSchema(
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
getPropertyType,
),
},
};
Expand Down
28 changes: 24 additions & 4 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -894,16 +894,18 @@ function buildPropSchema(
* LTI update could not be added via codemod */
function getEventArgument(
argumentProps: PropAST,
buildPropertiesForEvent: (
property: PropAST,
parser: Parser,
getPropertyType: (
name: $FlowFixMe,
optional: boolean,
typeAnnotation: $FlowFixMe,
parser: Parser,
) => NamedShape<EventTypeAnnotation>,
parser: Parser,
): ObjectTypeAnnotation<EventTypeAnnotation> {
return {
type: 'ObjectTypeAnnotation',
properties: argumentProps.map(member =>
buildPropertiesForEvent(member, parser),
buildPropertiesForEvent(member, parser, getPropertyType),
),
};
}
Expand Down Expand Up @@ -1053,6 +1055,23 @@ function handleGenericTypeAnnotation(
};
}

function buildPropertiesForEvent(
property: $FlowFixMe,
parser: Parser,
getPropertyType: (
name: $FlowFixMe,
optional: boolean,
typeAnnotation: $FlowFixMe,
parser: Parser,
) => NamedShape<EventTypeAnnotation>,
): NamedShape<EventTypeAnnotation> {
const name = property.key.name;
const optional = parser.isOptionalProperty(property);
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);

return getPropertyType(name, optional, typeAnnotation, parser);
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -1079,4 +1098,5 @@ module.exports = {
getCommandProperties,
handleGenericTypeAnnotation,
getTypeResolutionStatus,
buildPropertiesForEvent,
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const {
throwIfBubblingTypeIsNull,
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {
getEventArgument,
buildPropertiesForEvent,
} = require('../../parsers-commons');
const {
emitBoolProp,
emitDoubleProp,
Expand Down Expand Up @@ -69,7 +72,7 @@ function getPropertyType(
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.members.map(member =>
buildPropertiesForEvent(member, parser),
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
};
Expand Down Expand Up @@ -136,7 +139,7 @@ function extractArrayElementType(
return {
type: 'ObjectTypeAnnotation',
properties: typeAnnotation.members.map(member =>
buildPropertiesForEvent(member, parser),
buildPropertiesForEvent(member, parser, getPropertyType),
),
};
case 'TSArrayType':
Expand Down Expand Up @@ -247,19 +250,6 @@ function findEventArgumentsAndType(
}
}
function buildPropertiesForEvent(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
property,
parser: Parser,
): NamedShape<EventTypeAnnotation> {
const name = property.key.name;
const optional = parser.isOptionalProperty(property);
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);

return getPropertyType(name, optional, typeAnnotation, parser);
}

// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
type EventTypeAST = Object;
Expand Down Expand Up @@ -296,8 +286,8 @@ function buildEventSchema(
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
getPropertyType,
),
},
};
Expand All @@ -311,8 +301,8 @@ function buildEventSchema(
type: 'EventTypeAnnotation',
argument: getEventArgument(
nonNullableArgumentProps,
buildPropertiesForEvent,
parser,
getPropertyType,
),
},
};
Expand Down

0 comments on commit 942bd61

Please sign in to comment.