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 796be641804..cb357223087 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 @@ -11,13 +11,18 @@ 'use-strict'; -import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema'; +import type { + DoubleTypeAnnotation, + NamedShape, + UnionTypeAnnotationMemberType, +} from '../../CodegenSchema'; const { emitArrayType, emitBoolean, emitBoolProp, emitDouble, + emitDoubleProp, emitFloat, emitNumber, emitInt32, @@ -262,6 +267,35 @@ describe('emitDouble', () => { }); }); +describe('emitDoubleProp', () => { + describe('when optional is true', () => { + it('returns optional type annotation', () => { + const result = emitDoubleProp('Foo', true); + const expected: NamedShape = { + name: 'Foo', + optional: true, + typeAnnotation: { + type: 'DoubleTypeAnnotation', + }, + }; + expect(result).toEqual(expected); + }); + }); + describe('when optional is false', () => { + it('returns required type annotation', () => { + const result = emitDoubleProp('Foo', false); + const expected: NamedShape = { + name: 'Foo', + optional: false, + typeAnnotation: { + type: 'DoubleTypeAnnotation', + }, + }; + expect(result).toEqual(expected); + }); + }); +}); + describe('emitVoid', () => { 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 8e876ac4109..bb17cb93860 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -23,7 +23,11 @@ const { throwIfArgumentPropsAreNull, } = require('../../error-utils'); const {getEventArgument} = require('../../parsers-commons'); -const {emitBoolProp, emitStringProp} = require('../../parsers-primitives'); +const { + emitBoolProp, + emitDoubleProp, + emitStringProp, +} = require('../../parsers-primitives'); function getPropertyType( /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's @@ -49,13 +53,7 @@ function getPropertyType( }, }; case 'Double': - return { - name, - optional, - typeAnnotation: { - type: 'DoubleTypeAnnotation', - }, - }; + return emitDoubleProp(name, optional); case 'Float': return { name, diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 48ab8f26b89..a80caaf9c5b 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -97,6 +97,19 @@ function emitDouble(nullable: boolean): Nullable { }); } +function emitDoubleProp( + name: string, + optional: boolean, +): NamedShape { + return { + name, + optional, + typeAnnotation: { + type: 'DoubleTypeAnnotation', + }, + }; +} + function emitVoid(nullable: boolean): Nullable { return wrapNullable(nullable, { type: 'VoidTypeAnnotation', @@ -610,6 +623,7 @@ module.exports = { emitBoolean, emitBoolProp, emitDouble, + emitDoubleProp, emitFloat, emitFunction, emitInt32, 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 27301297c58..38a55299b7d 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -25,8 +25,11 @@ const { throwIfArgumentPropsAreNull, } = require('../../error-utils'); const {getEventArgument} = require('../../parsers-commons'); -const {emitBoolProp, emitStringProp} = require('../../parsers-primitives'); - +const { + emitBoolProp, + emitDoubleProp, + emitStringProp, +} = require('../../parsers-primitives'); function getPropertyType( /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's * LTI update could not be added via codemod */ @@ -59,13 +62,7 @@ function getPropertyType( }, }; case 'Double': - return { - name, - optional, - typeAnnotation: { - type: 'DoubleTypeAnnotation', - }, - }; + return emitDoubleProp(name, optional); case 'Float': return { name,