From 3ab7ef24a700b58ada28fb4d3e1acf6347898325 Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Mon, 10 Oct 2022 04:23:30 -0700 Subject: [PATCH] extract emitDouble from parsers modules to shared parsers-primitives (#34913) Summary: Part of https://github.com/facebook/react-native/issues/34872 This PR extracts the content of the case 'Double' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L202-L204), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L1235-L237)) into a single emitDouble function in the parsers-primitives.js file. Use the new function in the parsers. ## Changelog [Internal] [Changed] - Extract contents of the case 'Double' into a single emitDouble function inside parsers-primitives Pull Request resolved: https://github.com/facebook/react-native/pull/34913 Test Plan: I tested using jest and flow commands. Reviewed By: cipolleschi Differential Revision: D40216018 Pulled By: cipolleschi fbshipit-source-id: bc2aaa7636fbd2f6c861e4d87e394d0e4875a4a5 --- .../__tests__/parsers-primitives-test.js | 29 ++++++++++++++++++- .../src/parsers/flow/modules/index.js | 5 ++-- .../src/parsers/parsers-primitives.js | 8 +++++ .../src/parsers/typescript/modules/index.js | 5 ++-- 4 files changed, 40 insertions(+), 7 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 03bc6ab6dc3..5f3f785221b 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 @@ -13,10 +13,11 @@ const { emitBoolean, + emitDouble, emitNumber, emitInt32, emitRootTag, -} = require('../parsers-primitives'); +} = require('../parsers-primitives.js'); describe('emitBoolean', () => { describe('when nullable is true', () => { @@ -121,3 +122,29 @@ describe('emitRootTag', () => { }); }); }); + +describe('emitDouble', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitDouble(true); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'DoubleTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitDouble(false); + const expected = { + type: 'DoubleTypeAnnotation', + }; + + 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 e53276b723e..dc0c26659c9 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -35,6 +35,7 @@ const { const {unwrapNullable, wrapNullable} = require('../../parsers-commons'); const { emitBoolean, + emitDouble, emitNumber, emitInt32, emitRootTag, @@ -204,9 +205,7 @@ function translateTypeAnnotation( return emitInt32(nullable); } case 'Double': { - return wrapNullable(nullable, { - type: 'DoubleTypeAnnotation', - }); + return emitDouble(nullable); } case 'Float': { return wrapNullable(nullable, { diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 55c3e0de8d4..5c2c9c86751 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -12,6 +12,7 @@ import type { BooleanTypeAnnotation, + DoubleTypeAnnotation, Int32TypeAnnotation, NativeModuleNumberTypeAnnotation, Nullable, @@ -47,8 +48,15 @@ function emitRootTag(nullable: boolean): Nullable { }); } +function emitDouble(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'DoubleTypeAnnotation', + }); +} + module.exports = { emitBoolean, + emitDouble, emitInt32, emitNumber, emitRootTag, 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 97ea838a474..fb5bfcef9c8 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -35,6 +35,7 @@ const { const {unwrapNullable, wrapNullable} = require('../../parsers-commons'); const { emitBoolean, + emitDouble, emitNumber, emitInt32, emitRootTag, @@ -238,9 +239,7 @@ function translateTypeAnnotation( return emitInt32(nullable); } case 'Double': { - return wrapNullable(nullable, { - type: 'DoubleTypeAnnotation', - }); + return emitDouble(nullable); } case 'Float': { return wrapNullable(nullable, {