From b418b3a195f49cc19d29a5c63dfbe1aaeafd02ba Mon Sep 17 00:00:00 2001 From: Gabriel Donadel Dall'Agnol Date: Tue, 11 Oct 2022 00:02:57 -0300 Subject: [PATCH] chore: Extract codegen case 'Stringish' into a single emitStringish function --- .../__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 8d22808443cd..88b733b7e0d6 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 @@ -17,6 +17,7 @@ const { emitNumber, emitInt32, emitRootTag, + emitStringish, typeAliasResolution, emitPromise, } = require('../parsers-primitives.js'); @@ -125,6 +126,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 3597f261498d..cd0edb030c04 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -43,6 +43,7 @@ const { emitNumber, emitInt32, emitRootTag, + emitStringish, typeAliasResolution, emitPromise, } = require('../../parsers-primitives'); @@ -201,9 +202,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 b497d1398572..21b6832b1345 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -22,6 +22,7 @@ import type { ReservedTypeAnnotation, ObjectTypeAnnotation, NativeModulePromiseTypeAnnotation, + StringTypeAnnotation, } from '../CodegenSchema'; import type {ParserType} from './errors'; import type {TypeAliasResolutionStatus} from './utils'; @@ -64,6 +65,12 @@ function emitDouble(nullable: boolean): Nullable { }); } +function emitStringish(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'StringTypeAnnotation', + }); +} + function typeAliasResolution( typeAliasResolutionStatus: TypeAliasResolutionStatus, objectTypeAnnotation: ObjectTypeAnnotation< @@ -141,6 +148,7 @@ module.exports = { emitInt32, emitNumber, emitRootTag, + emitStringish, 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 5d73f31d5f61..cceaaf7767d1 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -43,6 +43,7 @@ const { emitNumber, emitInt32, emitRootTag, + emitStringish, typeAliasResolution, emitPromise, } = require('../../parsers-primitives'); @@ -234,9 +235,7 @@ function translateTypeAnnotation( ); } case 'Stringish': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitStringish(nullable); } case 'Int32': { return emitInt32(nullable);