From 87d65803abb80bbebe2cdce433dd12af75c5195d Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Sat, 29 Oct 2022 06:40:54 -0700 Subject: [PATCH] chore: Extract codegen case 'Float' into a single emitFloat function (#35124) Summary: ## Summary This PR extracts the content of the codegen case `'Float'` into a single `emitFloat` function inside the `parsers-primitives.js` file and uses it in both Flow and TypeScript parsers as requested on https://github.com/facebook/react-native/issues/34872. This also adds unit tests to the new `emitFloat` function. ## Changelog [Internal] [Changed] - Extract the content of the case 'Float' into a single emitFloat function Pull Request resolved: https://github.com/facebook/react-native/pull/35124 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/198704932-202e2cd7-5b04-4009-b47e-b4999fee6c98.png) Reviewed By: rshest Differential Revision: D40828746 Pulled By: cipolleschi fbshipit-source-id: 9c7cecf7268f16aaef29065c1983ad9a4dd18dbe --- .../__tests__/parsers-primitives-test.js | 27 +++++++++++++++++++ .../src/parsers/flow/modules/index.js | 5 ++-- .../src/parsers/parsers-primitives.js | 10 +++++++ .../src/parsers/typescript/modules/index.js | 5 ++-- 4 files changed, 41 insertions(+), 6 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 4b7317dac29..b64c48f776a 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 @@ -14,6 +14,7 @@ const { emitBoolean, emitDouble, + emitFloat, emitNumber, emitInt32, emitObject, @@ -424,4 +425,30 @@ describe('emitObject', () => { expect(result).toEqual(expected); }); }); + + describe('emitFloat', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitFloat(true); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'FloatTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitFloat(false); + const expected = { + type: 'FloatTypeAnnotation', + }; + + expect(result).toEqual(expected); + }); + }); + }); }); diff --git a/packages/react-native-codegen/src/parsers/flow/modules/index.js b/packages/react-native-codegen/src/parsers/flow/modules/index.js index 041257f1aac..90bbfeda7e3 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -43,6 +43,7 @@ const { const { emitBoolean, emitDouble, + emitFloat, emitFunction, emitNumber, emitInt32, @@ -242,9 +243,7 @@ function translateTypeAnnotation( return emitDouble(nullable); } case 'Float': { - return wrapNullable(nullable, { - type: 'FloatTypeAnnotation', - }); + return emitFloat(nullable); } case 'UnsafeObject': case 'Object': { diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 6af9551e24a..1b450d044ee 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -26,6 +26,7 @@ import type { NativeModulePromiseTypeAnnotation, StringTypeAnnotation, VoidTypeAnnotation, + NativeModuleFloatTypeAnnotation, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type {TypeAliasResolutionStatus} from './utils'; @@ -171,9 +172,18 @@ function emitObject( }); } +function emitFloat( + nullable: boolean, +): Nullable { + return wrapNullable(nullable, { + type: 'FloatTypeAnnotation', + }); +} + module.exports = { emitBoolean, emitDouble, + emitFloat, emitFunction, emitInt32, emitNumber, diff --git a/packages/react-native-codegen/src/parsers/typescript/modules/index.js b/packages/react-native-codegen/src/parsers/typescript/modules/index.js index c66a01ff469..5971cc84661 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -46,6 +46,7 @@ const { const { emitBoolean, emitDouble, + emitFloat, emitFunction, emitNumber, emitInt32, @@ -255,9 +256,7 @@ function translateTypeAnnotation( return emitDouble(nullable); } case 'Float': { - return wrapNullable(nullable, { - type: 'FloatTypeAnnotation', - }); + return emitFloat(nullable); } case 'UnsafeObject': case 'Object': {