Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen 129] add emitObjectProp in parser primitives #37904

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};