Skip to content

Commit

Permalink
add emitObjectProp in parser primitives (#37904)
Browse files Browse the repository at this point in the history
Summary:
This is a follow up PR to #37872 as it was not merged correctly.

## 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
-->

[Internal][Changed]: Add emitObjectProp in parser primitives

Pull Request resolved: #37904

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

Reviewed By: cipolleschi

Differential Revision: D46753690

Pulled By: rshest

fbshipit-source-id: a1d0a727222066f3721f62427a51ee0317e06f13
  • Loading branch information
tarunrajput authored and facebook-github-bot committed Jun 15, 2023
1 parent 1561d29 commit 24f7707
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const {emitUnion} = require('../parsers-primitives');
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
const {FlowParser} = require('../flow/parser');
const {TypeScriptParser} = require('../typescript/parser');
const {getPropertyType} = require('../flow/components/events');
const {extractArrayElementType} = require('../flow/components/events');

const parser = new MockedParser();
const flowParser = new FlowParser();
Expand Down Expand Up @@ -1679,6 +1679,10 @@ describe('emitObjectProp', () => {
describe('when property is optional', () => {
it('returns optional Object Prop', () => {
const typeAnnotation = {
type: 'GenericTypeAnnotation',
id: {
name: 'ObjectTypeAnnotation',
},
properties: [
{
key: {
Expand All @@ -1699,7 +1703,7 @@ describe('emitObjectProp', () => {
true,
flowParser,
typeAnnotation,
getPropertyType,
extractArrayElementType,
);
const expected = {
name: 'someProp',
Expand All @@ -1725,6 +1729,10 @@ describe('emitObjectProp', () => {
describe('when property is required', () => {
it('returns required Object Prop', () => {
const typeAnnotation = {
type: 'GenericTypeAnnotation',
id: {
name: 'ObjectTypeAnnotation',
},
properties: [
{
key: {
Expand All @@ -1745,7 +1753,7 @@ describe('emitObjectProp', () => {
false,
flowParser,
typeAnnotation,
getPropertyType,
extractArrayElementType,
);
const expected = {
name: 'someProp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getPropertyType(
optional,
parser,
typeAnnotation,
getPropertyType,
extractArrayElementType,
);
case 'UnionTypeAnnotation':
return {
Expand Down Expand Up @@ -311,5 +311,5 @@ function getEvents(

module.exports = {
getEvents,
getPropertyType,
extractArrayElementType,
};
17 changes: 4 additions & 13 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const {
wrapNullable,
unwrapNullable,
translateFunctionTypeAnnotation,
buildPropertiesForEvent,
} = require('./parsers-commons');

const {isModuleRegistryCall} = require('./utils');
Expand Down Expand Up @@ -663,24 +662,16 @@ function emitObjectProp(
optional: boolean,
parser: Parser,
typeAnnotation: $FlowFixMe,
getPropertyType: (
name: $FlowFixMe,
optional: boolean,
extractArrayElementType: (
typeAnnotation: $FlowFixMe,
name: string,
parser: Parser,
) => NamedShape<EventTypeAnnotation>,
) => EventTypeAnnotation,
): NamedShape<EventTypeAnnotation> {
return {
name,
optional,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ function getPropertyType(
const topLevelType = parseTopLevelType(annotation);
const typeAnnotation = topLevelType.type;
const optional = optionalProperty || topLevelType.optional;
const type =
typeAnnotation.type === 'TSTypeReference'
? typeAnnotation.typeName.name
: typeAnnotation.type;
const type = parser.extractTypeFromTypeAnnotation(typeAnnotation);

switch (type) {
case 'TSBooleanKeyword':
Expand All @@ -72,7 +69,7 @@ function getPropertyType(
optional,
parser,
typeAnnotation,
getPropertyType,
extractArrayElementType,
);
case 'TSUnionType':
return {
Expand All @@ -92,7 +89,6 @@ function getPropertyType(
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
};
default:
(type: empty);
throw new Error(`Unable to determine event type for "${name}": ${type}`);
}
}
Expand Down Expand Up @@ -314,4 +310,5 @@ function getEvents(

module.exports = {
getEvents,
extractArrayElementType,
};

0 comments on commit 24f7707

Please sign in to comment.