Skip to content

Commit 5f05b07

Browse files
MaeIgfacebook-github-bot
authored andcommitted
Extract RootTag case of translateTypeAnnotation from the flow and typescript 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
1 parent 7b345bc commit 5f05b07

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const {
1515
emitBoolean,
1616
emitNumber,
1717
emitInt32,
18-
} = require('../parsers-primitives.js');
18+
emitRootTag,
19+
} = require('../parsers-primitives');
1920

2021
describe('emitBoolean', () => {
2122
describe('when nullable is true', () => {
@@ -94,3 +95,29 @@ describe('emitNumber', () => {
9495
});
9596
});
9697
});
98+
99+
describe('emitRootTag', () => {
100+
const reservedTypeAnnotation = {
101+
type: 'ReservedTypeAnnotation',
102+
name: 'RootTag',
103+
};
104+
105+
describe('when nullable is true', () => {
106+
it('returns nullable type annotation', () => {
107+
const result = emitRootTag(true);
108+
109+
expect(result).toEqual({
110+
type: 'NullableTypeAnnotation',
111+
typeAnnotation: reservedTypeAnnotation,
112+
});
113+
});
114+
});
115+
116+
describe('when nullable is false', () => {
117+
it('returns non nullable type annotation', () => {
118+
const result = emitRootTag(false);
119+
120+
expect(result).toEqual(reservedTypeAnnotation);
121+
});
122+
});
123+
});

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
emitBoolean,
3838
emitNumber,
3939
emitInt32,
40+
emitRootTag,
4041
} = require('../../parsers-primitives');
4142
const {
4243
IncorrectlyParameterizedGenericParserError,
@@ -87,10 +88,7 @@ function translateTypeAnnotation(
8788
case 'GenericTypeAnnotation': {
8889
switch (typeAnnotation.id.name) {
8990
case 'RootTag': {
90-
return wrapNullable(nullable, {
91-
type: 'ReservedTypeAnnotation',
92-
name: 'RootTag',
93-
});
91+
return emitRootTag(nullable);
9492
}
9593
case 'Promise': {
9694
assertGenericTypeAnnotationHasExactlyOneTypeParameter(

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
Int32TypeAnnotation,
1616
NativeModuleNumberTypeAnnotation,
1717
Nullable,
18+
ReservedTypeAnnotation,
1819
} from '../CodegenSchema';
1920

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

43+
function emitRootTag(nullable: boolean): Nullable<ReservedTypeAnnotation> {
44+
return wrapNullable(nullable, {
45+
type: 'ReservedTypeAnnotation',
46+
name: 'RootTag',
47+
});
48+
}
49+
4250
module.exports = {
4351
emitBoolean,
4452
emitInt32,
4553
emitNumber,
54+
emitRootTag,
4655
};

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
emitBoolean,
3838
emitNumber,
3939
emitInt32,
40+
emitRootTag,
4041
} = require('../../parsers-primitives');
4142
const {
4243
IncorrectlyParameterizedGenericParserError,
@@ -199,10 +200,7 @@ function translateTypeAnnotation(
199200
case 'TSTypeReference': {
200201
switch (typeAnnotation.typeName.name) {
201202
case 'RootTag': {
202-
return wrapNullable(nullable, {
203-
type: 'ReservedTypeAnnotation',
204-
name: 'RootTag',
205-
});
203+
return emitRootTag(nullable);
206204
}
207205
case 'Promise': {
208206
assertGenericTypeAnnotationHasExactlyOneTypeParameter(

0 commit comments

Comments
 (0)