From eda90e5181fc1042cb76023997d2f83e39bb72f2 Mon Sep 17 00:00:00 2001 From: Ken Tominaga Date: Wed, 19 Oct 2022 01:38:28 -0700 Subject: [PATCH] =?UTF-8?q?Extract=20the=20content=20of=20the=20case=20'St?= =?UTF-8?q?ringTypeAnnotation'=20into=20a=20single=20=E2=80=A6=20(#34981)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: This PR extracts the content of the codegen case 'String' into a single `emitString` 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 `emitString` function. ref: https://github.com/facebook/react-native/pull/34936 ## Changelog [Internal] [Changed] - Extract the content of the case 'StringTypeAnnotation' into a single emitString function Pull Request resolved: https://github.com/facebook/react-native/pull/34981 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green Reviewed By: cortinico Differential Revision: D40376836 Pulled By: cipolleschi fbshipit-source-id: feb1b07ec7fc2c333f5054f8cd8d18457d985257 --- .../__tests__/parsers-primitives-test.js | 28 +++++++++++++++++++ .../src/parsers/flow/modules/index.js | 5 ++-- .../src/parsers/parsers-primitives.js | 9 +++++- .../src/parsers/typescript/modules/index.js | 5 ++-- 4 files changed, 40 insertions(+), 7 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 b87855eb5f3..4b7317dac29 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, + emitString, emitStringish, typeAliasResolution, } = require('../parsers-primitives.js'); @@ -155,6 +156,33 @@ describe('emitStringish', () => { }); }); +describe('emitString', () => { + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitString(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 = emitString(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 d69629734aa..c9649217047 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -49,6 +49,7 @@ const { emitPromise, emitRootTag, emitVoid, + emitString, emitStringish, typeAliasResolution, } = require('../../parsers-primitives'); @@ -354,9 +355,7 @@ function translateTypeAnnotation( return emitVoid(nullable); } case 'StringTypeAnnotation': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitString(nullable); } case 'FunctionTypeAnnotation': { const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation = diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index c64f4049bdd..6af9551e24a 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -86,6 +86,12 @@ function emitFunction( return wrapNullable(nullable, translateFunctionTypeAnnotationValue); } +function emitString(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'StringTypeAnnotation', + }); +} + function typeAliasResolution( typeAliasResolutionStatus: TypeAliasResolutionStatus, objectTypeAnnotation: ObjectTypeAnnotation< @@ -174,7 +180,8 @@ module.exports = { emitObject, emitPromise, emitRootTag, - emitStringish, emitVoid, + emitString, + 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 a084949635f..d61b2a69e71 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -49,6 +49,7 @@ const { emitPromise, emitRootTag, emitVoid, + emitString, emitStringish, typeAliasResolution, } = require('../../parsers-primitives'); @@ -369,9 +370,7 @@ function translateTypeAnnotation( return emitVoid(nullable); } case 'TSStringKeyword': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitString(nullable); } case 'TSFunctionType': { const translateFunctionTypeAnnotationValue: NativeModuleFunctionTypeAnnotation =