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 =