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);