From 54fc62cda7e37056ba62539a401926ef3b0602cf Mon Sep 17 00:00:00 2001 From: Youssef Eddibili Date: Sun, 9 Oct 2022 17:34:10 -0700 Subject: [PATCH] Extract contents of the case 'NumberTypeAnnotation' into a single emitNumber function (#34908) Summary: Part of https://github.com/facebook/react-native/issues/34872 This PR: - extracts the content of the case 'NumberTypeAnnotation' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L370-L372), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L405-L407)) into a single emitNumber function in the parsers-primitives.js file, and uses the new function in the parsers - unit tests emitNumber function ## Changelog [Internal] [Changed] - Extract contents of the case 'NumberTypeAnnotation' into a single emitNumber function Pull Request resolved: https://github.com/facebook/react-native/pull/34908 Test Plan: ` yarn jest react-native-codegen` image Reviewed By: cipolleschi Differential Revision: D40214542 Pulled By: cipolleschi fbshipit-source-id: a7746d3f1dd5f127dc520c6156383b18e00281ec --- .../__tests__/parsers-primitives-test.js | 32 ++++++++++++++++++- .../src/parsers/flow/modules/index.js | 10 +++--- .../src/parsers/parsers-primitives.js | 10 ++++++ .../src/parsers/typescript/modules/index.js | 10 +++--- 4 files changed, 53 insertions(+), 9 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 f4d1026a76caa6..2d3cec0343bc87 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,7 +11,11 @@ 'use-strict'; -const {emitBoolean, emitInt32} = require('../parsers-primitives.js'); +const { + emitBoolean, + emitNumber, + emitInt32, +} = require('../parsers-primitives.js'); describe('emitBoolean', () => { describe('when nullable is true', () => { @@ -64,3 +68,29 @@ describe('emitInt32', () => { }); }); }); + +describe('emitNumber', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitNumber(true); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'NumberTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitNumber(false); + const expected = { + type: 'NumberTypeAnnotation', + }; + + 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 f6e3f3a4f6f5c6..7864ef87dd6df8 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -33,7 +33,11 @@ const { isModuleRegistryCall, } = require('../utils.js'); const {unwrapNullable, wrapNullable} = require('../../parsers-commons'); -const {emitBoolean, emitInt32} = require('../../parsers-primitives'); +const { + emitBoolean, + emitNumber, + emitInt32, +} = require('../../parsers-primitives'); const { IncorrectlyParameterizedFlowGenericParserError, MisnamedModuleFlowInterfaceParserError, @@ -364,9 +368,7 @@ function translateTypeAnnotation( return emitBoolean(nullable); } case 'NumberTypeAnnotation': { - return wrapNullable(nullable, { - type: 'NumberTypeAnnotation', - }); + return emitNumber(nullable); } case 'VoidTypeAnnotation': { 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 046bbd1c4a932d..96c727b80c1243 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -13,6 +13,7 @@ import type { BooleanTypeAnnotation, Int32TypeAnnotation, + NativeModuleNumberTypeAnnotation, Nullable, } from '../CodegenSchema'; @@ -30,7 +31,16 @@ function emitInt32(nullable: boolean): Nullable { }); } +function emitNumber( + nullable: boolean, +): Nullable { + return wrapNullable(nullable, { + type: 'NumberTypeAnnotation', + }); +} + module.exports = { emitBoolean, 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 37558070087119..64cd84e914a809 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -33,7 +33,11 @@ const { isModuleRegistryCall, } = require('../utils.js'); const {unwrapNullable, wrapNullable} = require('../../parsers-commons'); -const {emitBoolean, emitInt32} = require('../../parsers-primitives'); +const { + emitBoolean, + emitNumber, + emitInt32, +} = require('../../parsers-primitives'); const { IncorrectlyParameterizedTypeScriptGenericParserError, MisnamedModuleTypeScriptInterfaceParserError, @@ -399,9 +403,7 @@ function translateTypeAnnotation( return emitBoolean(nullable); } case 'TSNumberKeyword': { - return wrapNullable(nullable, { - type: 'NumberTypeAnnotation', - }); + return emitNumber(nullable); } case 'TSVoidKeyword': { return wrapNullable(nullable, {