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 b87855eb5f3d..4b7317dac29b 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 169a3aeb9f1a..dd75b0a6064d 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 { emitPromise, emitRootTag, emitVoid, + emitString, emitStringish, typeAliasResolution, } = require('../../parsers-primitives'); @@ -347,9 +348,7 @@ function translateTypeAnnotation( return emitVoid(nullable); } case 'StringTypeAnnotation': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitString(nullable); } case 'FunctionTypeAnnotation': { return wrapNullable( diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index cdf0f1a10aba..090acff80623 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -79,6 +79,12 @@ function emitStringish(nullable: boolean): Nullable { }); } +function emitString(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'StringTypeAnnotation', + }); +} + function typeAliasResolution( typeAliasResolutionStatus: TypeAliasResolutionStatus, objectTypeAnnotation: ObjectTypeAnnotation< @@ -167,6 +173,7 @@ module.exports = { emitPromise, emitRootTag, 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 522eb134b361..2905b6f4364d 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 { emitPromise, emitRootTag, emitVoid, + emitString, emitStringish, typeAliasResolution, } = require('../../parsers-primitives'); @@ -382,9 +383,7 @@ function translateTypeAnnotation( return emitVoid(nullable); } case 'TSStringKeyword': { - return wrapNullable(nullable, { - type: 'StringTypeAnnotation', - }); + return emitString(nullable); } case 'TSFunctionType': { return wrapNullable(