Skip to content

Commit 202b965

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
add emitObjectProp in parser primitives (#37872)
Summary: Part of #34872 > Create a function emitObjectProp(name: string, optional: boolean) in parser-primitives.js. Factor out the code from [Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/events.js#L84-L93) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L87-L96) into that function. Use that function in the original call site. ## 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: #37872 Test Plan: ```shell yarn test react-native-codegen ``` Reviewed By: cipolleschi Differential Revision: D46715634 Pulled By: rshest fbshipit-source-id: 6c58002df132bc5b05a10a19f8611819d7179cb1
1 parent 4475572 commit 202b965

File tree

4 files changed

+139
-21
lines changed

4 files changed

+139
-21
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ const {
4242
typeEnumResolution,
4343
Visitor,
4444
emitStringProp,
45+
emitObjectProp,
4546
} = require('../parsers-primitives.js');
4647
const {MockedParser} = require('../parserMock');
4748
const {emitUnion} = require('../parsers-primitives');
4849
const {UnsupportedUnionTypeAnnotationParserError} = require('../errors');
4950
const {FlowParser} = require('../flow/parser');
5051
const {TypeScriptParser} = require('../typescript/parser');
52+
const {getPropertyType} = require('../flow/components/events');
5153

5254
const parser = new MockedParser();
5355
const flowParser = new FlowParser();
@@ -1671,3 +1673,98 @@ describe('emitBoolProp', () => {
16711673
});
16721674
});
16731675
});
1676+
1677+
describe('emitObjectProp', () => {
1678+
const name = 'someProp';
1679+
describe('when property is optional', () => {
1680+
it('returns optional Object Prop', () => {
1681+
const typeAnnotation = {
1682+
properties: [
1683+
{
1684+
key: {
1685+
name: 'someKey',
1686+
},
1687+
optional: true,
1688+
value: {
1689+
type: 'StringTypeAnnotation',
1690+
typeAnnotation: {
1691+
type: 'StringTypeAnnotation',
1692+
},
1693+
},
1694+
},
1695+
],
1696+
};
1697+
const result = emitObjectProp(
1698+
name,
1699+
true,
1700+
flowParser,
1701+
typeAnnotation,
1702+
getPropertyType,
1703+
);
1704+
const expected = {
1705+
name: 'someProp',
1706+
optional: true,
1707+
typeAnnotation: {
1708+
properties: [
1709+
{
1710+
name: 'someKey',
1711+
optional: true,
1712+
typeAnnotation: {
1713+
type: 'StringTypeAnnotation',
1714+
},
1715+
},
1716+
],
1717+
type: 'ObjectTypeAnnotation',
1718+
},
1719+
};
1720+
1721+
expect(result).toEqual(expected);
1722+
});
1723+
});
1724+
1725+
describe('when property is required', () => {
1726+
it('returns required Object Prop', () => {
1727+
const typeAnnotation = {
1728+
properties: [
1729+
{
1730+
key: {
1731+
name: 'someKey',
1732+
},
1733+
optional: false,
1734+
value: {
1735+
type: 'StringTypeAnnotation',
1736+
typeAnnotation: {
1737+
type: 'StringTypeAnnotation',
1738+
},
1739+
},
1740+
},
1741+
],
1742+
};
1743+
const result = emitObjectProp(
1744+
name,
1745+
false,
1746+
flowParser,
1747+
typeAnnotation,
1748+
getPropertyType,
1749+
);
1750+
const expected = {
1751+
name: 'someProp',
1752+
optional: false,
1753+
typeAnnotation: {
1754+
properties: [
1755+
{
1756+
name: 'someKey',
1757+
optional: false,
1758+
typeAnnotation: {
1759+
type: 'StringTypeAnnotation',
1760+
},
1761+
},
1762+
],
1763+
type: 'ObjectTypeAnnotation',
1764+
},
1765+
};
1766+
1767+
expect(result).toEqual(expected);
1768+
});
1769+
});
1770+
});

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ const {
3333
emitMixedProp,
3434
emitStringProp,
3535
emitInt32Prop,
36+
emitObjectProp,
3637
} = require('../../parsers-primitives');
3738

3839
function getPropertyType(
3940
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
4041
* LTI update could not be added via codemod */
41-
name,
42+
name: string,
4243
optional: boolean,
4344
typeAnnotation: $FlowFixMe,
4445
parser: Parser,
@@ -64,18 +65,13 @@ function getPropertyType(
6465
parser,
6566
);
6667
case 'ObjectTypeAnnotation':
67-
return {
68+
return emitObjectProp(
6869
name,
6970
optional,
70-
typeAnnotation: {
71-
type: 'ObjectTypeAnnotation',
72-
properties: parser
73-
.getObjectProperties(typeAnnotation)
74-
.map(member =>
75-
buildPropertiesForEvent(member, parser, getPropertyType),
76-
),
77-
},
78-
};
71+
parser,
72+
typeAnnotation,
73+
getPropertyType,
74+
);
7975
case 'UnionTypeAnnotation':
8076
return {
8177
name,
@@ -315,4 +311,5 @@ function getEvents(
315311

316312
module.exports = {
317313
getEvents,
314+
getPropertyType,
318315
};

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const {
6060
wrapNullable,
6161
unwrapNullable,
6262
translateFunctionTypeAnnotation,
63+
buildPropertiesForEvent,
6364
} = require('./parsers-commons');
6465

6566
const {isModuleRegistryCall} = require('./utils');
@@ -657,6 +658,32 @@ function emitMixedProp(
657658
};
658659
}
659660

661+
function emitObjectProp(
662+
name: string,
663+
optional: boolean,
664+
parser: Parser,
665+
typeAnnotation: $FlowFixMe,
666+
getPropertyType: (
667+
name: $FlowFixMe,
668+
optional: boolean,
669+
typeAnnotation: $FlowFixMe,
670+
parser: Parser,
671+
) => NamedShape<EventTypeAnnotation>,
672+
): NamedShape<EventTypeAnnotation> {
673+
return {
674+
name,
675+
optional,
676+
typeAnnotation: {
677+
type: 'ObjectTypeAnnotation',
678+
properties: parser
679+
.getObjectProperties(typeAnnotation)
680+
.map(member =>
681+
buildPropertiesForEvent(member, parser, getPropertyType),
682+
),
683+
},
684+
};
685+
}
686+
660687
module.exports = {
661688
emitArrayType,
662689
emitBoolean,
@@ -687,4 +714,5 @@ module.exports = {
687714
typeEnumResolution,
688715
translateArrayTypeAnnotation,
689716
Visitor,
717+
emitObjectProp,
690718
};

packages/react-native-codegen/src/parsers/typescript/components/events.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const {
3535
emitMixedProp,
3636
emitStringProp,
3737
emitInt32Prop,
38+
emitObjectProp,
3839
} = require('../../parsers-primitives');
3940
function getPropertyType(
4041
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -66,18 +67,13 @@ function getPropertyType(
6667
case 'Float':
6768
return emitFloatProp(name, optional);
6869
case 'TSTypeLiteral':
69-
return {
70+
return emitObjectProp(
7071
name,
7172
optional,
72-
typeAnnotation: {
73-
type: 'ObjectTypeAnnotation',
74-
properties: parser
75-
.getObjectProperties(typeAnnotation)
76-
.map(member =>
77-
buildPropertiesForEvent(member, parser, getPropertyType),
78-
),
79-
},
80-
};
73+
parser,
74+
typeAnnotation,
75+
getPropertyType,
76+
);
8177
case 'TSUnionType':
8278
return {
8379
name,

0 commit comments

Comments
 (0)