Skip to content

Commit

Permalink
add emitUnionProp in parser primitives (#38705)
Browse files Browse the repository at this point in the history
Summary:
[Codegen 131] This PR add a function `emitUnionProp` to the parser-primitives, as requested on #34872

## Changelog:

[INTERNAL] [ADDED] - Add `emitUnionProp` function to parser-primitives

Pull Request resolved: #38705

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

Reviewed By: christophpurrer

Differential Revision: D47921708

Pulled By: rshest

fbshipit-source-id: c2c081c6317928e5eb8b0c1d0640c7b7f40a4b0b
  • Loading branch information
Zwem authored and facebook-github-bot committed Aug 1, 2023
1 parent 529c952 commit 5eaf28b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
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

0 comments on commit 5eaf28b

Please sign in to comment.