From b3219fe34558af823b446338a77369c9ff8883a7 Mon Sep 17 00:00:00 2001 From: youedd Date: Wed, 12 Oct 2022 03:06:04 -0700 Subject: [PATCH] Extract contents of the case 'VoidTypeAnnotation' into a single emitVoid function (#34932) Summary: Part of https://github.com/facebook/react-native/issues/34872 This PR: - extracts the content of the case 'VoidTypeAnnotation' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L375-L377), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L410-L412)) into a single emitVoid function in the parsers-primitives.js file. Use the new function in the parsers. - unit tests emitVoid function ## Changelog [Internal] [Changed] - Extract contents of the case 'VoidTypeAnnotation' into a single emitVoid function Pull Request resolved: https://github.com/facebook/react-native/pull/34932 Test Plan: ` yarn jest react-native-codegen` image Reviewed By: cipolleschi Differential Revision: D40239730 Pulled By: rshest fbshipit-source-id: 16a9555223cacbb3b9916fd469bd63f83db33f18 --- .../__tests__/parsers-primitives-test.js | 27 +++++++++++++++++++ .../src/parsers/flow/modules/index.js | 5 ++-- .../src/parsers/parsers-primitives.js | 8 ++++++ .../src/parsers/typescript/modules/index.js | 5 ++-- 4 files changed, 39 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 c3f73de65d..cbd850c9f4 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 @@ -19,6 +19,7 @@ const { emitObject, emitPromise, emitRootTag, + emitVoid, typeAliasResolution, } = require('../parsers-primitives.js'); @@ -152,6 +153,32 @@ describe('emitDouble', () => { }); }); +describe('emitVoid', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitVoid(true); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'VoidTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitVoid(false); + const expected = { + type: 'VoidTypeAnnotation', + }; + + expect(result).toEqual(expected); + }); + }); +}); + describe('typeAliasResolution', () => { const objectTypeAnnotation = { type: 'ObjectTypeAnnotation', 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 ad9de4acf4..bacdc9fc2f 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -45,6 +45,7 @@ const { emitObject, emitPromise, emitRootTag, + emitVoid, typeAliasResolution, } = require('../../parsers-primitives'); const { @@ -345,9 +346,7 @@ function translateTypeAnnotation( return emitNumber(nullable); } case 'VoidTypeAnnotation': { - return wrapNullable(nullable, { - type: 'VoidTypeAnnotation', - }); + return emitVoid(nullable); } case 'StringTypeAnnotation': { 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 8a0b4fc341..52b47a9a7d 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -23,6 +23,7 @@ import type { ReservedTypeAnnotation, ObjectTypeAnnotation, NativeModulePromiseTypeAnnotation, + VoidTypeAnnotation, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type {TypeAliasResolutionStatus} from './utils'; @@ -65,6 +66,12 @@ function emitDouble(nullable: boolean): Nullable { }); } +function emitVoid(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'VoidTypeAnnotation', + }); +} + function typeAliasResolution( typeAliasResolutionStatus: TypeAliasResolutionStatus, objectTypeAnnotation: ObjectTypeAnnotation< @@ -152,5 +159,6 @@ module.exports = { emitObject, emitPromise, emitRootTag, + emitVoid, typeAliasResolution, }; 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 37dc652ffa..480065d690 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -45,6 +45,7 @@ const { emitObject, emitPromise, emitRootTag, + emitVoid, typeAliasResolution, } = require('../../parsers-primitives'); const { @@ -380,9 +381,7 @@ function translateTypeAnnotation( return emitNumber(nullable); } case 'TSVoidKeyword': { - return wrapNullable(nullable, { - type: 'VoidTypeAnnotation', - }); + return emitVoid(nullable); } case 'TSStringKeyword': { return wrapNullable(nullable, {