Skip to content

Commit

Permalink
move getEventArgument function into parsers-commons.js (#37133)
Browse files Browse the repository at this point in the history
Summary:
[Codegen 116] This PR attempts to extract the logic of `getEventArgument` function from the following locations :
- `parsers/flow/components/events.js`
- `parsers/typescript/components/events.js`
since they are the same and move the function to `parsers/parsers-commons.js` as requested on #34872

## Changelog:

[Internal] [Changed] - Move `getEventArgument` to parser-commons and update usages.

Pull Request resolved: #37133

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

Reviewed By: christophpurrer

Differential Revision: D45569128

Pulled By: cipolleschi

fbshipit-source-id: 63a7619e5b4fca0157c62a359ac51831f4f15945
  • Loading branch information
siddarthkay authored and facebook-github-bot committed May 4, 2023
1 parent 74987b6 commit d46f92c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');

function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand Down Expand Up @@ -257,15 +258,6 @@ function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
return getPropertyType(name, optional, typeAnnotation);
}

/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
function getEventArgument(argumentProps, name: $FlowFixMe) {
return {
type: 'ObjectTypeAnnotation',
properties: argumentProps.map(buildPropertiesForEvent),
};
}

function buildEventSchema(
types: TypeMap,
property: EventTypeAST,
Expand Down Expand Up @@ -307,7 +299,7 @@ function buildEventSchema(
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(argumentProps, name),
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
},
};
}
Expand All @@ -324,7 +316,7 @@ function buildEventSchema(
bubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(argumentProps, name),
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
},
};
}
Expand Down
17 changes: 17 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import type {
SchemaType,
NativeModuleEnumMap,
OptionsShape,
EventTypeAnnotation,
ObjectTypeAnnotation,
} from '../CodegenSchema.js';

import type {Parser} from './parser';
Expand Down Expand Up @@ -852,6 +854,20 @@ function extendsForProp(
}
}

/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
function getEventArgument(
argumentProps: PropAST,
buildPropertiesForEvent: (
property: PropAST,
) => NamedShape<EventTypeAnnotation>,
): ObjectTypeAnnotation<EventTypeAnnotation> {
return {
type: 'ObjectTypeAnnotation',
properties: argumentProps.map(buildPropertiesForEvent),
};
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -872,4 +888,5 @@ module.exports = {
getOptions,
getCommandTypeNameAndOptionsExpression,
extendsForProp,
getEventArgument,
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const {
throwIfEventHasNoName,
throwIfBubblingTypeIsNull,
} = require('../../error-utils');

const {getEventArgument} = require('../../parsers-commons');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
Expand Down Expand Up @@ -270,15 +270,6 @@ function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
return getPropertyType(name, optional, typeAnnotation);
}

/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
function getEventArgument(argumentProps, name: $FlowFixMe) {
return {
type: 'ObjectTypeAnnotation',
properties: argumentProps.map(buildPropertiesForEvent),
};
}

// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
type EventTypeAST = Object;

Expand Down Expand Up @@ -312,7 +303,7 @@ function buildEventSchema(
paperTopLevelNameDeprecated,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(argumentProps, name),
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
},
};
}
Expand All @@ -323,7 +314,7 @@ function buildEventSchema(
bubblingType,
typeAnnotation: {
type: 'EventTypeAnnotation',
argument: getEventArgument(argumentProps, name),
argument: getEventArgument(argumentProps, buildPropertiesForEvent),
},
};
}
Expand Down

0 comments on commit d46f92c

Please sign in to comment.