From 305f7c3352142d07ed824fe4e640fa999dada04d Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Wed, 12 Oct 2022 03:06:04 -0700 Subject: [PATCH] chore: Extract codegen case 'Stringish' into a single emitStringish function (#34936) Summary: This PR extracts the content of the codegen case `'Stringish'` into a single `emitStringish` function inside the `parsers-primitives.js` file and uses it in both Flow and TypeScript parsers as requested on https://github.com/facebook/react-native/issues/34872. This also adds unit tests to the new `emitStringish` function. ## Changelog [Internal] [Changed] - Extract the content of the case 'Stringish' into a single emitStringish function Pull Request resolved: https://github.com/facebook/react-native/pull/34936 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/194987664-b588b82b-a9e0-49a9-a3cc-a03cb0a230e6.png) Reviewed By: cipolleschi Differential Revision: D40255921 Pulled By: rshest fbshipit-source-id: 9c08f81f12c93995bb6ba032fabcd6451b8dc7c1 --- .../__tests__/parsers-primitives-test.js | 28 +++++++++++++++++++ .../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(+), 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 cbd850c9f4..b87855eb5f 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 @@ -20,6 +20,7 @@ const { emitPromise, emitRootTag, emitVoid, + emitStringish, typeAliasResolution, } = require('../parsers-primitives.js'); @@ -127,6 +128,33 @@ describe('emitRootTag', () => { }); }); +describe('emitStringish', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitStringish(true); + const expected = { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'StringTypeAnnotation', + }, + }; + + expect(result).toEqual(expected); + }); + }); + + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitStringish(false); + const expected = { + type: 'StringTypeAnnotation', + }; + + expect(result).toEqual(expected); + }); + }); +}); + describe('emitDouble', () => { describe('when nullable is true', () => { it('returns nullable type annotation', () => { 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 bacdc9fc2f..90ab99c402 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -46,6 +46,7 @@ const { emitPromise, emitRootTag, emitVoid, + emitStringish, typeAliasResolution, } = require('../../parsers-primitives'); const { @@ -203,9 +204,7 @@ function translateTypeAnnotation( return wrapNullable(nullable || isParamNullable, paramType); } case 'Stringish': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitStringish(nullable); } case 'Int32': { return emitInt32(nullable); diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 52b47a9a7d..cdf0f1a10a 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -24,6 +24,7 @@ import type { ObjectTypeAnnotation, NativeModulePromiseTypeAnnotation, VoidTypeAnnotation, + StringTypeAnnotation, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type {TypeAliasResolutionStatus} from './utils'; @@ -72,6 +73,12 @@ function emitVoid(nullable: boolean): Nullable { }); } +function emitStringish(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'StringTypeAnnotation', + }); +} + function typeAliasResolution( typeAliasResolutionStatus: TypeAliasResolutionStatus, objectTypeAnnotation: ObjectTypeAnnotation< @@ -160,5 +167,6 @@ module.exports = { emitPromise, emitRootTag, emitVoid, + emitStringish, 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 480065d690..8b4da89cf5 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -46,6 +46,7 @@ const { emitPromise, emitRootTag, emitVoid, + emitStringish, typeAliasResolution, } = require('../../parsers-primitives'); const { @@ -236,9 +237,7 @@ function translateTypeAnnotation( ); } case 'Stringish': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitStringish(nullable); } case 'Int32': { return emitInt32(nullable);