Skip to content

Commit 66f4a91

Browse files
frankcalisefacebook-github-bot
authored andcommitted
Codegen 123: add parser-primitive function for emitBoolProp (#37488)
Summary: Part of #34872, Improving Codegen Reduces code duplication by creating a helper function `emitBoolProp(name: string, optional: boolean)` in `parser-primitives.js`. Refactors the code from [Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/events.js#L37-L43) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L47-L53) to utilize the new function. bypass-github-export-checks ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [INTERNAL] [CHANGED] - Extract contents of the case `BooleanTypeAnnotation` and `TSBooleanKeyword` into a single `emitBoolProp` function For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: #37488 Test Plan: Run `yarn jest react-native-codegen` <img width="392" alt="image" src="https://github.com/facebook/react-native/assets/374022/79dae56d-e12d-4be2-9426-50a72e893dfe"> Reviewed By: dmytrorykun Differential Revision: D46073996 Pulled By: cipolleschi fbshipit-source-id: 4af67e7e9c3ee1712159c7a647790cb431cfb17a
1 parent af8b03d commit 66f4a91

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
1616
const {
1717
emitArrayType,
1818
emitBoolean,
19+
emitBoolProp,
1920
emitDouble,
2021
emitFloat,
2122
emitNumber,
@@ -829,7 +830,7 @@ describe('emitUnion', () => {
829830
'ObjectTypeAnnotation',
830831
];
831832
describe('when nullable is true', () => {
832-
it('throws an excpetion', () => {
833+
it('throws an exception', () => {
833834
const expected = new UnsupportedUnionTypeAnnotationParserError(
834835
hasteModuleName,
835836
typeAnnotation,
@@ -843,7 +844,7 @@ describe('emitUnion', () => {
843844
});
844845

845846
describe('when nullable is false', () => {
846-
it('throws an excpetion', () => {
847+
it('throws an exception', () => {
847848
const expected = new UnsupportedUnionTypeAnnotationParserError(
848849
hasteModuleName,
849850
typeAnnotation,
@@ -1042,7 +1043,7 @@ describe('emitUnion', () => {
10421043
'ObjectTypeAnnotation',
10431044
];
10441045
describe('when nullable is true', () => {
1045-
it('throws an excpetion', () => {
1046+
it('throws an exception', () => {
10461047
const expected = new UnsupportedUnionTypeAnnotationParserError(
10471048
hasteModuleName,
10481049
typeAnnotation,
@@ -1056,7 +1057,7 @@ describe('emitUnion', () => {
10561057
});
10571058

10581059
describe('when nullable is false', () => {
1059-
it('throws an excpetion', () => {
1060+
it('throws an exception', () => {
10601061
const expected = new UnsupportedUnionTypeAnnotationParserError(
10611062
hasteModuleName,
10621063
typeAnnotation,
@@ -1506,3 +1507,34 @@ describe('emitCommonTypes', () => {
15061507
});
15071508
});
15081509
});
1510+
1511+
describe('emitBoolProp', () => {
1512+
describe('when optional is true', () => {
1513+
it('returns optional type annotation', () => {
1514+
const result = emitBoolProp('someProp', true);
1515+
const expected = {
1516+
name: 'someProp',
1517+
optional: true,
1518+
typeAnnotation: {
1519+
type: 'BooleanTypeAnnotation',
1520+
},
1521+
};
1522+
1523+
expect(result).toEqual(expected);
1524+
});
1525+
});
1526+
describe('when optional is false', () => {
1527+
it('returns required type annotation', () => {
1528+
const result = emitBoolProp('someProp', false);
1529+
const expected = {
1530+
name: 'someProp',
1531+
optional: false,
1532+
typeAnnotation: {
1533+
type: 'BooleanTypeAnnotation',
1534+
},
1535+
};
1536+
1537+
expect(result).toEqual(expected);
1538+
});
1539+
});
1540+
});

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import type {
1616
EventTypeAnnotation,
1717
} from '../../../CodegenSchema.js';
1818
import type {Parser} from '../../parser';
19+
1920
const {
2021
throwIfEventHasNoName,
2122
throwIfBubblingTypeIsNull,
2223
throwIfArgumentPropsAreNull,
2324
} = require('../../error-utils');
2425
const {getEventArgument} = require('../../parsers-commons');
26+
const {emitBoolProp} = require('../../parsers-primitives');
2527

2628
function getPropertyType(
2729
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -35,13 +37,7 @@ function getPropertyType(
3537

3638
switch (type) {
3739
case 'BooleanTypeAnnotation':
38-
return {
39-
name,
40-
optional,
41-
typeAnnotation: {
42-
type: 'BooleanTypeAnnotation',
43-
},
44-
};
40+
return emitBoolProp(name, optional);
4541
case 'StringTypeAnnotation':
4642
return {
4743
name,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import type {
3333
VoidTypeAnnotation,
3434
NativeModuleObjectTypeAnnotation,
3535
NativeModuleEnumDeclaration,
36+
NamedShape,
37+
EventTypeAnnotation,
3638
} from '../CodegenSchema';
3739
import type {Parser} from './parser';
3840
import type {
@@ -577,9 +579,23 @@ function emitCommonTypes(
577579
);
578580
}
579581

582+
function emitBoolProp(
583+
name: string,
584+
optional: boolean,
585+
): NamedShape<EventTypeAnnotation> {
586+
return {
587+
name,
588+
optional,
589+
typeAnnotation: {
590+
type: 'BooleanTypeAnnotation',
591+
},
592+
};
593+
}
594+
580595
module.exports = {
581596
emitArrayType,
582597
emitBoolean,
598+
emitBoolProp,
583599
emitDouble,
584600
emitFloat,
585601
emitFunction,

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const {
2525
throwIfArgumentPropsAreNull,
2626
} = require('../../error-utils');
2727
const {getEventArgument} = require('../../parsers-commons');
28+
const {emitBoolProp} = require('../../parsers-primitives');
29+
2830
function getPropertyType(
2931
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
3032
* LTI update could not be added via codemod */
@@ -45,13 +47,7 @@ function getPropertyType(
4547

4648
switch (type) {
4749
case 'TSBooleanKeyword':
48-
return {
49-
name,
50-
optional,
51-
typeAnnotation: {
52-
type: 'BooleanTypeAnnotation',
53-
},
54-
};
50+
return emitBoolProp(name, optional);
5551
case 'TSStringKeyword':
5652
return {
5753
name,

0 commit comments

Comments
 (0)