From 966f3cdea369b05adcea00a2d28110ff08e289f0 Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Tue, 11 Oct 2022 05:22:59 -0700 Subject: [PATCH] extract emitPromise from parsers modules to shared parsers-primitives (#34935) Summary: This PR is a task from https://github.com/facebook/react-native/issues/34872: > Extract the content of the case 'Promise' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L90-L97), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L197-L205)) into a single emitPromise function in the parsers-primitives.js file. Use the new function in the parsers. Note that this PR should be merged after https://github.com/facebook/react-native/pull/34933 ## Changelog [Internal] [Changed] - Extract contents of the case 'Promise' into a single emitPromise function inside parsers-primitives Pull Request resolved: https://github.com/facebook/react-native/pull/34935 Test Plan: I tested using jest and flow commands. Reviewed By: cipolleschi Differential Revision: D40257033 Pulled By: cipolleschi fbshipit-source-id: 0246f43c6b688629e2de1259e7f535c2cf6dd0a4 --- .../__tests__/parsers-primitives-test.js | 71 +++++++++++++++++++ .../src/parsers/flow/modules/index.js | 8 +-- .../src/parsers/parsers-primitives.js | 25 ++++++- .../src/parsers/typescript/modules/index.js | 8 +-- 4 files changed, 101 insertions(+), 11 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 40ec6106add..8d22808443c 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 @@ -18,6 +18,7 @@ const { emitInt32, emitRootTag, typeAliasResolution, + emitPromise, } = require('../parsers-primitives.js'); describe('emitBoolean', () => { @@ -244,3 +245,73 @@ describe('typeAliasResolution', () => { }); }); }); + +describe('emitPromise', () => { + const moduleName = 'testModuleName'; + const language = 'Flow'; + describe("when typeAnnotation doesn't have exactly one typeParameter", () => { + const typeAnnotation = { + typeParameters: { + params: [1, 2], + type: 'TypeParameterInstantiation', + }, + id: { + name: 'typeAnnotationName', + }, + }; + it('throws an IncorrectlyParameterizedGenericParserError error', () => { + const nullable = false; + expect(() => + emitPromise(moduleName, typeAnnotation, language, nullable), + ).toThrow(); + }); + }); + + describe("when typeAnnotation doesn't has exactly one typeParameter", () => { + const typeAnnotation = { + typeParameters: { + params: [1], + type: 'TypeParameterInstantiation', + }, + id: { + name: 'typeAnnotationName', + }, + }; + + describe('when nullable is true', () => { + const nullable = true; + it('returns nullable type annotation', () => { + const result = emitPromise( + moduleName, + typeAnnotation, + language, + nullable, + ); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'PromiseTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + const nullable = false; + it('returns non nullable type annotation', () => { + const result = emitPromise( + moduleName, + typeAnnotation, + language, + nullable, + ); + const expected = { + type: 'PromiseTypeAnnotation', + }; + + 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 a0735661111..8ffd9db1623 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -44,6 +44,7 @@ const { emitInt32, emitRootTag, typeAliasResolution, + emitPromise, } = require('../../parsers-primitives'); const { MisnamedModuleInterfaceParserError, @@ -95,15 +96,12 @@ function translateTypeAnnotation( return emitRootTag(nullable); } case 'Promise': { - assertGenericTypeAnnotationHasExactlyOneTypeParameter( + return emitPromise( hasteModuleName, typeAnnotation, language, + nullable, ); - - return wrapNullable(nullable, { - type: 'PromiseTypeAnnotation', - }); } case 'Array': case '$ReadOnlyArray': { diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index b949ed535ad..b497d139857 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -21,10 +21,15 @@ import type { Int32TypeAnnotation, ReservedTypeAnnotation, ObjectTypeAnnotation, + NativeModulePromiseTypeAnnotation, } from '../CodegenSchema'; +import type {ParserType} from './errors'; import type {TypeAliasResolutionStatus} from './utils'; -const {wrapNullable} = require('./parsers-commons'); +const { + wrapNullable, + assertGenericTypeAnnotationHasExactlyOneTypeParameter, +} = require('./parsers-commons'); function emitBoolean(nullable: boolean): Nullable { return wrapNullable(nullable, { @@ -113,6 +118,23 @@ function typeAliasResolution( }); } +function emitPromise( + hasteModuleName: string, + typeAnnotation: $FlowFixMe, + language: ParserType, + nullable: boolean, +): Nullable { + assertGenericTypeAnnotationHasExactlyOneTypeParameter( + hasteModuleName, + typeAnnotation, + language, + ); + + return wrapNullable(nullable, { + type: 'PromiseTypeAnnotation', + }); +} + module.exports = { emitBoolean, emitDouble, @@ -120,4 +142,5 @@ module.exports = { emitNumber, 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 5104b117691..c6dbbb9ef7f 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -44,6 +44,7 @@ const { emitInt32, emitRootTag, typeAliasResolution, + emitPromise, } = require('../../parsers-primitives'); const { MisnamedModuleInterfaceParserError, @@ -207,15 +208,12 @@ function translateTypeAnnotation( return emitRootTag(nullable); } case 'Promise': { - assertGenericTypeAnnotationHasExactlyOneTypeParameter( + return emitPromise( hasteModuleName, typeAnnotation, language, + nullable, ); - - return wrapNullable(nullable, { - type: 'PromiseTypeAnnotation', - }); } case 'Array': case 'ReadonlyArray': {