From fd4451ecfa54450e830e5f70fb0d90547ce7a040 Mon Sep 17 00:00:00 2001 From: Marco Fiorito Date: Tue, 11 Oct 2022 15:17:13 -0700 Subject: [PATCH] Chore/extract codegen case object to parser primitives (#34926) Summary: Part of https://github.com/facebook/react-native/issues/34872 This PR extracts the content of the case 'Object' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L365-L367), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L400-L402)) into a single emitObject function in the parsers-primitives.js file. Use the new function in the parsers. ## Changelog [Internal] [Changed] - Extract contents of the case 'Object' into a single emitObject function Pull Request resolved: https://github.com/facebook/react-native/pull/34926 Test Plan: image Reviewed By: rshest Differential Revision: D40231670 Pulled By: cipolleschi fbshipit-source-id: db6a61427c8c020d48be5317b094f136842b62ca --- .../__tests__/parsers-primitives-test.js | 29 ++++++++++++++++++- .../src/parsers/flow/modules/index.js | 7 ++--- .../src/parsers/parsers-primitives.js | 12 +++++++- .../src/parsers/typescript/modules/index.js | 7 ++--- 4 files changed, 45 insertions(+), 10 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 8d22808443cdf4..c3f73de65da177 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 @@ -16,9 +16,10 @@ const { emitDouble, emitNumber, emitInt32, + emitObject, + emitPromise, emitRootTag, typeAliasResolution, - emitPromise, } = require('../parsers-primitives.js'); describe('emitBoolean', () => { @@ -315,3 +316,29 @@ describe('emitPromise', () => { }); }); }); + +describe('emitObject', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitObject(true); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'GenericObjectTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitObject(false); + const expected = { + type: 'GenericObjectTypeAnnotation', + }; + + 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 3597f261498dea..ad9de4acf4d0d8 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -42,9 +42,10 @@ const { emitDouble, emitNumber, emitInt32, + emitObject, + emitPromise, emitRootTag, typeAliasResolution, - emitPromise, } = require('../../parsers-primitives'); const { MisnamedModuleInterfaceParserError, @@ -218,9 +219,7 @@ function translateTypeAnnotation( } case 'UnsafeObject': case 'Object': { - return wrapNullable(nullable, { - type: 'GenericObjectTypeAnnotation', - }); + return emitObject(nullable); } default: { const maybeEumDeclaration = types[typeAnnotation.id.name]; diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index b497d1398572b0..8a0b4fc341d49b 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -19,6 +19,7 @@ import type { BooleanTypeAnnotation, DoubleTypeAnnotation, Int32TypeAnnotation, + NativeModuleGenericObjectTypeAnnotation, ReservedTypeAnnotation, ObjectTypeAnnotation, NativeModulePromiseTypeAnnotation, @@ -135,12 +136,21 @@ function emitPromise( }); } +function emitObject( + nullable: boolean, +): Nullable { + return wrapNullable(nullable, { + type: 'GenericObjectTypeAnnotation', + }); +} + module.exports = { emitBoolean, emitDouble, emitInt32, emitNumber, + emitObject, + emitPromise, emitRootTag, typeAliasResolution, - emitPromise, }; 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 5d73f31d5f61ec..37dc652ffad45c 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -42,9 +42,10 @@ const { emitDouble, emitNumber, emitInt32, + emitObject, + emitPromise, emitRootTag, typeAliasResolution, - emitPromise, } = require('../../parsers-primitives'); const { MisnamedModuleInterfaceParserError, @@ -251,9 +252,7 @@ function translateTypeAnnotation( } case 'UnsafeObject': case 'Object': { - return wrapNullable(nullable, { - type: 'GenericObjectTypeAnnotation', - }); + return emitObject(nullable); } default: { const maybeEumDeclaration = types[typeAnnotation.typeName.name];