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 131]: add emitUnionProp in parser primitives #38705

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 @@ -43,6 +43,7 @@ const {
Visitor,
emitStringProp,
emitObjectProp,
emitUnionProp,
} = require('../parsers-primitives.js');
const {MockedParser} = require('../parserMock');
const {emitUnion} = require('../parsers-primitives');
Expand Down Expand Up @@ -1776,3 +1777,71 @@ describe('emitObjectProp', () => {
});
});
});

describe('emitUnionProp', () => {
describe('when property is optional', () => {
it('returns optional type annotation with Flow parser', () => {
const typeAnnotation = {
types: [
{
value: 'someValue1',
},
{
value: 'someValue2',
},
],
};
const result = emitUnionProp(
'someProp',
true,
flowParser,
typeAnnotation,
);
const expected = {
name: 'someProp',
optional: true,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: ['someValue1', 'someValue2'],
},
};

expect(result).toEqual(expected);
});
});
describe('when property is required', () => {
it('returns required type annotation with TypeScript parser', () => {
const typeAnnotation = {
types: [
{
literal: {
value: 'someValue1',
},
},
{
literal: {
value: 'someValue2',
},
},
],
};
const result = emitUnionProp(
'someProp',
false,
typeScriptParser,
typeAnnotation,
);

const expected = {
name: 'someProp',
optional: false,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: ['someValue1', 'someValue2'],
},
};

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

function getPropertyType(
Expand Down Expand Up @@ -73,16 +74,7 @@ function getPropertyType(
extractArrayElementType,
);
case 'UnionTypeAnnotation':
return {
name,
optional,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: typeAnnotation.types.map(option =>
parser.getLiteralValue(option),
),
},
};
return emitUnionProp(name, optional, parser, typeAnnotation);
case 'UnsafeMixed':
return emitMixedProp(name, optional);
case 'ArrayTypeAnnotation':
Expand Down
19 changes: 19 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,24 @@ function emitObjectProp(
};
}

function emitUnionProp(
name: string,
optional: boolean,
parser: Parser,
typeAnnotation: $FlowFixMe,
): NamedShape<EventTypeAnnotation> {
return {
name,
optional,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: typeAnnotation.types.map(option =>
parser.getLiteralValue(option),
),
},
};
}

module.exports = {
emitArrayType,
emitBoolean,
Expand Down Expand Up @@ -706,4 +724,5 @@ module.exports = {
translateArrayTypeAnnotation,
Visitor,
emitObjectProp,
emitUnionProp,
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
emitStringProp,
emitInt32Prop,
emitObjectProp,
emitUnionProp,
} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand Down Expand Up @@ -72,16 +73,7 @@ function getPropertyType(
extractArrayElementType,
);
case 'TSUnionType':
return {
name,
optional,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
options: typeAnnotation.types.map(option =>
parser.getLiteralValue(option),
),
},
};
return emitUnionProp(name, optional, parser, typeAnnotation);
case 'UnsafeMixed':
return emitMixedProp(name, optional);
case 'TSArrayType':
Expand Down