Skip to content
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 numberDiff line numberDiff line change
Expand Up@@ -42,12 +42,14 @@ const {
typeEnumResolution,
Visitor,
emitStringProp,
emitObjectProp,
} = require('../parsers-primitives.js');
const {MockedParser} = require('../parserMock');
const {emitUnion} = require('../parsers-primitives');
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
const {FlowParser} = require('../flow/parser');
const {TypeScriptParser} = require('../typescript/parser');
const {extractArrayElementType} = require('../flow/components/events');

const parser = new MockedParser();
const flowParser = new FlowParser();
Expand DownExpand Up@@ -1671,3 +1673,106 @@ describe('emitBoolProp', () => {
});
});
});

describe('emitObjectProp', () => {
const name = 'someProp';
describe('when property is optional', () => {
it('returns optional Object Prop', () => {
const typeAnnotation = {
type: 'GenericTypeAnnotation',
id: {
name: 'ObjectTypeAnnotation',
},
properties: [
{
key: {
name: 'someKey',
},
optional: true,
value: {
type: 'StringTypeAnnotation',
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
},
],
};
const result = emitObjectProp(
name,
true,
flowParser,
typeAnnotation,
extractArrayElementType,
);
const expected = {
name: 'someProp',
optional: true,
typeAnnotation: {
properties: [
{
name: 'someKey',
optional: true,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
],
type: 'ObjectTypeAnnotation',
},
};

expect(result).toEqual(expected);
});
});

describe('when property is required', () => {
it('returns required Object Prop', () => {
const typeAnnotation = {
type: 'GenericTypeAnnotation',
id: {
name: 'ObjectTypeAnnotation',
},
properties: [
{
key: {
name: 'someKey',
},
optional: false,
value: {
type: 'StringTypeAnnotation',
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
},
],
};
const result = emitObjectProp(
name,
false,
flowParser,
typeAnnotation,
extractArrayElementType,
);
const expected = {
name: 'someProp',
optional: false,
typeAnnotation: {
properties: [
{
name: 'someKey',
optional: false,
typeAnnotation: {
type: 'StringTypeAnnotation',
},
},
],
type: 'ObjectTypeAnnotation',
},
};

expect(result).toEqual(expected);
});
});
});
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,6 +33,7 @@ const {
emitMixedProp,
emitStringProp,
emitInt32Prop,
emitObjectProp,
} = require('../../parsers-primitives');

function getPropertyType(
Expand DownExpand Up@@ -64,18 +65,13 @@ function getPropertyType(
parser,
);
case 'ObjectTypeAnnotation':
return {
return emitObjectProp(
name,
optional,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
};
parser,
typeAnnotation,
extractArrayElementType,
);
case 'UnionTypeAnnotation':
return {
name,
Expand DownExpand Up@@ -315,4 +311,5 @@ function getEvents(

module.exports = {
getEvents,
extractArrayElementType,
};
19 changes: 19 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -657,6 +657,24 @@ function emitMixedProp(
};
}

function emitObjectProp(
name: string,
optional: boolean,
parser: Parser,
typeAnnotation: $FlowFixMe,
extractArrayElementType: (
typeAnnotation: $FlowFixMe,
name: string,
parser: Parser,
) => EventTypeAnnotation,
): NamedShape<EventTypeAnnotation> {
return {
name,
optional,
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
};
}

module.exports = {
emitArrayType,
emitBoolean,
Expand DownExpand Up@@ -687,4 +705,5 @@ module.exports = {
typeEnumResolution,
translateArrayTypeAnnotation,
Visitor,
emitObjectProp,
};
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,7 @@ const {
emitMixedProp,
emitStringProp,
emitInt32Prop,
emitObjectProp,
} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand All@@ -49,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@@ -66,18 +64,13 @@ function getPropertyType(
case 'Float':
return emitFloatProp(name, optional);
case 'TSTypeLiteral':
return {
return emitObjectProp(
name,
optional,
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: parser
.getObjectProperties(typeAnnotation)
.map(member =>
buildPropertiesForEvent(member, parser, getPropertyType),
),
},
};
parser,
typeAnnotation,
extractArrayElementType,
);
case 'TSUnionType':
return {
name,
Expand All@@ -96,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 DownExpand Up@@ -318,4 +310,5 @@ function getEvents(

module.exports = {
getEvents,
extractArrayElementType,
};