diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js index ab7f65379cd..a09730d1ce3 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js @@ -27,6 +27,7 @@ const { emitFloatProp, emitNumber, emitInt32, + emitInt32Prop, emitGenericObject, emitObject, emitPromise, @@ -104,6 +105,38 @@ describe('emitInt32', () => { }); }); +describe('emitInt32Prop', () => { + describe('when optional is true', () => { + it('returns optional Int32TypeAnnotation', () => { + const result = emitInt32Prop('myProp', true); + const expected = { + name: 'myProp', + optional: true, + typeAnnotation: { + type: 'Int32TypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + + describe('when nullable is false', () => { + it('returns required Int32TypeAnnotation', () => { + const result = emitInt32Prop('myProp', false); + const expected = { + name: 'myProp', + optional: false, + typeAnnotation: { + type: 'Int32TypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); +}); + describe('emitNumber', () => { describe('when nullable is true', () => { it('returns nullable type annotation', () => { diff --git a/packages/react-native-codegen/src/parsers/flow/components/events.js b/packages/react-native-codegen/src/parsers/flow/components/events.js index 35b44e494db..bab6bb593b9 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -28,6 +28,7 @@ const { emitDoubleProp, emitFloatProp, emitStringProp, + emitInt32Prop, } = require('../../parsers-primitives'); function getPropertyType( @@ -46,13 +47,7 @@ function getPropertyType( case 'StringTypeAnnotation': return emitStringProp(name, optional); case 'Int32': - return { - name, - optional, - typeAnnotation: { - type: 'Int32TypeAnnotation', - }, - }; + return emitInt32Prop(name, optional); case 'Double': return emitDoubleProp(name, optional); case 'Float': diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index f14740cce87..35835ae6705 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -76,6 +76,19 @@ function emitInt32(nullable: boolean): Nullable { }); } +function emitInt32Prop( + name: string, + optional: boolean, +): NamedShape { + return { + name, + optional, + typeAnnotation: { + type: 'Int32TypeAnnotation', + }, + }; +} + function emitNumber( nullable: boolean, ): Nullable { @@ -641,6 +654,7 @@ module.exports = { emitFloatProp, emitFunction, emitInt32, + emitInt32Prop, emitNumber, emitGenericObject, emitDictionary, diff --git a/packages/react-native-codegen/src/parsers/typescript/components/events.js b/packages/react-native-codegen/src/parsers/typescript/components/events.js index fa373703339..1c13bceb261 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -30,6 +30,7 @@ const { emitDoubleProp, emitFloatProp, emitStringProp, + emitInt32Prop, } = require('../../parsers-primitives'); function getPropertyType( /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's @@ -55,13 +56,7 @@ function getPropertyType( case 'TSStringKeyword': return emitStringProp(name, optional); case 'Int32': - return { - name, - optional, - typeAnnotation: { - type: 'Int32TypeAnnotation', - }, - }; + return emitInt32Prop(name, optional); case 'Double': return emitDoubleProp(name, optional); case 'Float':