Skip to content

Commit

Permalink
Extract RootTag case of translateTypeAnnotation from the flow and typ…
Browse files Browse the repository at this point in the history
…escript folders in parsers-primitives (#34901)

Summary:
This PR aims to reduce code duplication by extracting `emitRootTag` logic from the flow and typescript folders into a shared parsers-primitives file. It is a task of #34872:
> Extract the content of the case 'RootTag' (Flow TypeScript) into a single emitRootTag function in the parsers-primitives.js file. Use the new function in the parsers.

~~Note that #34898 should be merged first. I rebased on it's branch.~~

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[Internal] [Changed] - Extract RootTag case of translateTypeAnnotation from the flow and typescript folders in parsers-primitives

Pull Request resolved: #34901

Test Plan:
All tests are passing, with `yarn jest react-native-codegen`:
<img width="934" alt="image" src="https://user-images.githubusercontent.com/40902940/194777150-6136c1b6-11f8-4e21-829b-fda418b6925c.png">

Reviewed By: cipolleschi

Differential Revision: D40212553

Pulled By: cipolleschi

fbshipit-source-id: eadbbfb5cf6dfa6c966f4c08a5d9372a3470b621
  • Loading branch information
MaeIg authored and facebook-github-bot committed Oct 10, 2022
1 parent 7b345bc commit 5f05b07
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const {
emitBoolean,
emitNumber,
emitInt32,
} = require('../parsers-primitives.js');
emitRootTag,
} = require('../parsers-primitives');

describe('emitBoolean', () => {
describe('when nullable is true', () => {
Expand Down Expand Up @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
emitBoolean,
emitNumber,
emitInt32,
emitRootTag,
} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedGenericParserError,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
Int32TypeAnnotation,
NativeModuleNumberTypeAnnotation,
Nullable,
ReservedTypeAnnotation,
} from '../CodegenSchema';

const {wrapNullable} = require('./parsers-commons');
Expand All @@ -39,8 +40,16 @@ function emitNumber(
});
}

function emitRootTag(nullable: boolean): Nullable<ReservedTypeAnnotation> {
return wrapNullable(nullable, {
type: 'ReservedTypeAnnotation',
name: 'RootTag',
});
}

module.exports = {
emitBoolean,
emitInt32,
emitNumber,
emitRootTag,
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
emitBoolean,
emitNumber,
emitInt32,
emitRootTag,
} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedGenericParserError,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 5f05b07

Please sign in to comment.