Skip to content

Commit db8c11d

Browse files
Naturalclarfacebook-github-bot
authored andcommitted
Extract the content of the case 'Int32' (Flow, TypeScript) into a single emitInt32 function in the parsers-primitives.js file. (#34906)
Summary: Part of #34872 Refactors emitInt32 in codegen parser module into a common parsers-primitive.js ## 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 the content of the case 'Int32' (Flow, TypeScript) into a single emitInt32 function Pull Request resolved: #34906 Test Plan: Ran `yarn jest react-native-codegen` <img width="690" alt="Screen Shot 2022-10-09 at 15 45 21" src="https://user-images.githubusercontent.com/6936373/194742142-801a89c8-f803-4e8d-9b05-cf6bda97ea19.png"> Reviewed By: cipolleschi Differential Revision: D40212544 Pulled By: cipolleschi fbshipit-source-id: 845e3758ab654edb972a8d8dc3cfa73464e2d49d
1 parent 7a2e346 commit db8c11d

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
'use-strict';
1313

14-
const {emitBoolean} = require('../parsers-primitives.js');
14+
const {emitBoolean, emitInt32} = require('../parsers-primitives.js');
1515

1616
describe('emitBoolean', () => {
1717
describe('when nullable is true', () => {
@@ -38,3 +38,29 @@ describe('emitBoolean', () => {
3838
});
3939
});
4040
});
41+
42+
describe('emitInt32', () => {
43+
describe('when nullable is true', () => {
44+
it('returns nullable type annotation', () => {
45+
const result = emitInt32(true);
46+
const expected = {
47+
type: 'NullableTypeAnnotation',
48+
typeAnnotation: {
49+
type: 'Int32TypeAnnotation',
50+
},
51+
};
52+
53+
expect(result).toEqual(expected);
54+
});
55+
});
56+
describe('when nullable is false', () => {
57+
it('returns non nullable type annotation', () => {
58+
const result = emitInt32(false);
59+
const expected = {
60+
type: 'Int32TypeAnnotation',
61+
};
62+
63+
expect(result).toEqual(expected);
64+
});
65+
});
66+
});

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const {
3333
isModuleRegistryCall,
3434
} = require('../utils.js');
3535
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
36-
const {emitBoolean} = require('../../parsers-primitives');
36+
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
3737
const {
3838
IncorrectlyParameterizedFlowGenericParserError,
3939
MisnamedModuleFlowInterfaceParserError,
@@ -195,9 +195,7 @@ function translateTypeAnnotation(
195195
});
196196
}
197197
case 'Int32': {
198-
return wrapNullable(nullable, {
199-
type: 'Int32TypeAnnotation',
200-
});
198+
return emitInt32(nullable);
201199
}
202200
case 'Double': {
203201
return wrapNullable(nullable, {

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
'use strict';
1212

13-
import type {BooleanTypeAnnotation, Nullable} from '../CodegenSchema';
13+
import type {
14+
BooleanTypeAnnotation,
15+
Int32TypeAnnotation,
16+
Nullable,
17+
} from '../CodegenSchema';
1418

1519
const {wrapNullable} = require('./parsers-commons');
1620

@@ -20,6 +24,13 @@ function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
2024
});
2125
}
2226

27+
function emitInt32(nullable: boolean): Nullable<Int32TypeAnnotation> {
28+
return wrapNullable(nullable, {
29+
type: 'Int32TypeAnnotation',
30+
});
31+
}
32+
2333
module.exports = {
2434
emitBoolean,
35+
emitInt32,
2536
};

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const {
3333
isModuleRegistryCall,
3434
} = require('../utils.js');
3535
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
36-
const {emitBoolean} = require('../../parsers-primitives');
36+
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
3737
const {
3838
IncorrectlyParameterizedTypeScriptGenericParserError,
3939
MisnamedModuleTypeScriptInterfaceParserError,
@@ -228,9 +228,7 @@ function translateTypeAnnotation(
228228
});
229229
}
230230
case 'Int32': {
231-
return wrapNullable(nullable, {
232-
type: 'Int32TypeAnnotation',
233-
});
231+
return emitInt32(nullable);
234232
}
235233
case 'Double': {
236234
return wrapNullable(nullable, {

0 commit comments

Comments
 (0)