From 1a1e3994666e1f8b2abf2c5040ac3a44149ddca0 Mon Sep 17 00:00:00 2001 From: foestauf <46908845+foestauf@users.noreply.github.com> Date: Thu, 25 May 2023 02:59:23 -0700 Subject: [PATCH] Create a function emitFloatProp (#37500) Summary: part of https://github.com/facebook/react-native/issues/34872 > Create a function emitFloatProp(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#L69-L75) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L79-L85) into that function. Use that function in the original call site. bypass-github-export-checks ## Changelog: [INTERNAL] [ADDED] - emitFloatProp in parser primitves Pull Request resolved: https://github.com/facebook/react-native/pull/37500 Test Plan: `yarn jest packages/react-native-codegen` Reviewed By: dmytrorykun Differential Revision: D46073527 Pulled By: cipolleschi fbshipit-source-id: 556b3aa0e0d330929e006758ec88d0a6889f87bc --- .../__tests__/parsers-primitives-test.js | 33 +++++++++++++++++++ .../src/parsers/flow/components/events.js | 9 ++--- .../src/parsers/parsers-primitives.js | 18 ++++++++-- .../parsers/typescript/components/events.js | 9 ++--- 4 files changed, 53 insertions(+), 16 deletions(-) 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 cb3572230878a4..ab7f65379cdd83 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 @@ -24,6 +24,7 @@ const { emitDouble, emitDoubleProp, emitFloat, + emitFloatProp, emitNumber, emitInt32, emitGenericObject, @@ -711,6 +712,38 @@ describe('emitFloat', () => { }); }); +describe('emitFloatProp', () => { + describe('when optional is true', () => { + it('returns optional FloatTypeAnnotation', () => { + const result = emitFloatProp('myProp', true); + const expected = { + name: 'myProp', + optional: true, + typeAnnotation: { + type: 'FloatTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + + describe('when optional is false', () => { + it('returns required FloatTypeAnnotation', () => { + const result = emitFloatProp('myProp', false); + const expected = { + name: 'myProp', + optional: false, + typeAnnotation: { + type: 'FloatTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); +}); + describe('emitMixed', () => { 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 bb17cb938600a6..35b44e494db295 100644 --- a/packages/react-native-codegen/src/parsers/flow/components/events.js +++ b/packages/react-native-codegen/src/parsers/flow/components/events.js @@ -26,6 +26,7 @@ const {getEventArgument} = require('../../parsers-commons'); const { emitBoolProp, emitDoubleProp, + emitFloatProp, emitStringProp, } = require('../../parsers-primitives'); @@ -55,13 +56,7 @@ function getPropertyType( case 'Double': return emitDoubleProp(name, optional); case 'Float': - return { - name, - optional, - typeAnnotation: { - type: 'FloatTypeAnnotation', - }, - }; + return emitFloatProp(name, optional); case '$ReadOnly': return getPropertyType( name, diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index a80caaf9c5b465..f14740cce87ffa 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -14,7 +14,9 @@ import type { Nullable, BooleanTypeAnnotation, DoubleTypeAnnotation, + EventTypeAnnotation, Int32TypeAnnotation, + NamedShape, NativeModuleAliasMap, NativeModuleEnumMap, NativeModuleBaseTypeAnnotation, @@ -33,8 +35,6 @@ import type { VoidTypeAnnotation, NativeModuleObjectTypeAnnotation, NativeModuleEnumDeclaration, - NamedShape, - EventTypeAnnotation, } from '../CodegenSchema'; import type {Parser} from './parser'; import type { @@ -365,6 +365,19 @@ function emitFloat( }); } +function emitFloatProp( + name: string, + optional: boolean, +): NamedShape { + return { + name, + optional, + typeAnnotation: { + type: 'FloatTypeAnnotation', + }, + }; +} + function emitUnion( nullable: boolean, hasteModuleName: string, @@ -625,6 +638,7 @@ module.exports = { emitDouble, emitDoubleProp, emitFloat, + emitFloatProp, emitFunction, emitInt32, emitNumber, 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 38a55299b7d88d..fa373703339d58 100644 --- a/packages/react-native-codegen/src/parsers/typescript/components/events.js +++ b/packages/react-native-codegen/src/parsers/typescript/components/events.js @@ -28,6 +28,7 @@ const {getEventArgument} = require('../../parsers-commons'); const { emitBoolProp, emitDoubleProp, + emitFloatProp, emitStringProp, } = require('../../parsers-primitives'); function getPropertyType( @@ -64,13 +65,7 @@ function getPropertyType( case 'Double': return emitDoubleProp(name, optional); case 'Float': - return { - name, - optional, - typeAnnotation: { - type: 'FloatTypeAnnotation', - }, - }; + return emitFloatProp(name, optional); case 'TSTypeLiteral': return { name,