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 2d3cec0343bc87..03bc6ab6dc3583 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 @@ -15,7 +15,8 @@ const { emitBoolean, emitNumber, emitInt32, -} = require('../parsers-primitives.js'); + emitRootTag, +} = require('../parsers-primitives'); describe('emitBoolean', () => { describe('when nullable is true', () => { @@ -94,3 +95,29 @@ describe('emitNumber', () => { }); }); }); + +describe('emitRootTag', () => { + const reservedTypeAnnotation = { + type: 'ReservedTypeAnnotation', + name: 'RootTag', + }; + + describe('when nullable is true', () => { + it('returns nullable type annotation', () => { + const result = emitRootTag(true); + + expect(result).toEqual({ + type: 'NullableTypeAnnotation', + typeAnnotation: reservedTypeAnnotation, + }); + }); + }); + + describe('when nullable is false', () => { + it('returns non nullable type annotation', () => { + const result = emitRootTag(false); + + expect(result).toEqual(reservedTypeAnnotation); + }); + }); +}); 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 b9a7e99f196c43..e53276b723e470 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/index.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/index.js @@ -37,6 +37,7 @@ const { emitBoolean, emitNumber, emitInt32, + emitRootTag, } = require('../../parsers-primitives'); const { IncorrectlyParameterizedGenericParserError, @@ -87,10 +88,7 @@ function translateTypeAnnotation( case 'GenericTypeAnnotation': { switch (typeAnnotation.id.name) { case 'RootTag': { - return wrapNullable(nullable, { - type: 'ReservedTypeAnnotation', - name: 'RootTag', - }); + return emitRootTag(nullable); } case 'Promise': { assertGenericTypeAnnotationHasExactlyOneTypeParameter( diff --git a/packages/react-native-codegen/src/parsers/parsers-primitives.js b/packages/react-native-codegen/src/parsers/parsers-primitives.js index 96c727b80c1243..55c3e0de8d4e6c 100644 --- a/packages/react-native-codegen/src/parsers/parsers-primitives.js +++ b/packages/react-native-codegen/src/parsers/parsers-primitives.js @@ -15,6 +15,7 @@ import type { Int32TypeAnnotation, NativeModuleNumberTypeAnnotation, Nullable, + ReservedTypeAnnotation, } from '../CodegenSchema'; const {wrapNullable} = require('./parsers-commons'); @@ -39,8 +40,16 @@ function emitNumber( }); } +function emitRootTag(nullable: boolean): Nullable { + return wrapNullable(nullable, { + type: 'ReservedTypeAnnotation', + name: 'RootTag', + }); +} + module.exports = { emitBoolean, emitInt32, emitNumber, + emitRootTag, }; 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 ec8ac8d9d5460c..97ea838a47472f 100644 --- a/packages/react-native-codegen/src/parsers/typescript/modules/index.js +++ b/packages/react-native-codegen/src/parsers/typescript/modules/index.js @@ -37,6 +37,7 @@ const { emitBoolean, emitNumber, emitInt32, + emitRootTag, } = require('../../parsers-primitives'); const { IncorrectlyParameterizedGenericParserError, @@ -199,10 +200,7 @@ function translateTypeAnnotation( case 'TSTypeReference': { switch (typeAnnotation.typeName.name) { case 'RootTag': { - return wrapNullable(nullable, { - type: 'ReservedTypeAnnotation', - name: 'RootTag', - }); + return emitRootTag(nullable); } case 'Promise': { assertGenericTypeAnnotationHasExactlyOneTypeParameter(