Skip to content

Commit 54fc62c

Browse files
youeddfacebook-github-bot
authored andcommitted
Extract contents of the case 'NumberTypeAnnotation' into a single emitNumber function (#34908)
Summary: Part of #34872 This PR: - extracts the content of the case 'NumberTypeAnnotation' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L370-L372), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L405-L407)) into a single emitNumber function in the parsers-primitives.js file, and uses the new function in the parsers - unit tests emitNumber function ## Changelog [Internal] [Changed] - Extract contents of the case 'NumberTypeAnnotation' into a single emitNumber function Pull Request resolved: #34908 Test Plan: ` yarn jest react-native-codegen` <img width="935" alt="image" src="https://user-images.githubusercontent.com/19575877/194764122-44975a97-3acd-4f27-babe-ddcd58c3ec61.png"> Reviewed By: cipolleschi Differential Revision: D40214542 Pulled By: cipolleschi fbshipit-source-id: a7746d3f1dd5f127dc520c6156383b18e00281ec
1 parent 74fda10 commit 54fc62c

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
'use-strict';
1313

14-
const {emitBoolean, emitInt32} = require('../parsers-primitives.js');
14+
const {
15+
emitBoolean,
16+
emitNumber,
17+
emitInt32,
18+
} = require('../parsers-primitives.js');
1519

1620
describe('emitBoolean', () => {
1721
describe('when nullable is true', () => {
@@ -64,3 +68,29 @@ describe('emitInt32', () => {
6468
});
6569
});
6670
});
71+
72+
describe('emitNumber', () => {
73+
describe('when nullable is true', () => {
74+
it('returns nullable type annotation', () => {
75+
const result = emitNumber(true);
76+
const expected = {
77+
type: 'NullableTypeAnnotation',
78+
typeAnnotation: {
79+
type: 'NumberTypeAnnotation',
80+
},
81+
};
82+
83+
expect(result).toEqual(expected);
84+
});
85+
});
86+
describe('when nullable is false', () => {
87+
it('returns non nullable type annotation', () => {
88+
const result = emitNumber(false);
89+
const expected = {
90+
type: 'NumberTypeAnnotation',
91+
};
92+
93+
expect(result).toEqual(expected);
94+
});
95+
});
96+
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ const {
3333
isModuleRegistryCall,
3434
} = require('../utils.js');
3535
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
36-
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
36+
const {
37+
emitBoolean,
38+
emitNumber,
39+
emitInt32,
40+
} = require('../../parsers-primitives');
3741
const {
3842
IncorrectlyParameterizedFlowGenericParserError,
3943
MisnamedModuleFlowInterfaceParserError,
@@ -364,9 +368,7 @@ function translateTypeAnnotation(
364368
return emitBoolean(nullable);
365369
}
366370
case 'NumberTypeAnnotation': {
367-
return wrapNullable(nullable, {
368-
type: 'NumberTypeAnnotation',
369-
});
371+
return emitNumber(nullable);
370372
}
371373
case 'VoidTypeAnnotation': {
372374
return wrapNullable(nullable, {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import type {
1414
BooleanTypeAnnotation,
1515
Int32TypeAnnotation,
16+
NativeModuleNumberTypeAnnotation,
1617
Nullable,
1718
} from '../CodegenSchema';
1819

@@ -30,7 +31,16 @@ function emitInt32(nullable: boolean): Nullable<Int32TypeAnnotation> {
3031
});
3132
}
3233

34+
function emitNumber(
35+
nullable: boolean,
36+
): Nullable<NativeModuleNumberTypeAnnotation> {
37+
return wrapNullable(nullable, {
38+
type: 'NumberTypeAnnotation',
39+
});
40+
}
41+
3342
module.exports = {
3443
emitBoolean,
3544
emitInt32,
45+
emitNumber,
3646
};

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ const {
3333
isModuleRegistryCall,
3434
} = require('../utils.js');
3535
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
36-
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
36+
const {
37+
emitBoolean,
38+
emitNumber,
39+
emitInt32,
40+
} = require('../../parsers-primitives');
3741
const {
3842
IncorrectlyParameterizedTypeScriptGenericParserError,
3943
MisnamedModuleTypeScriptInterfaceParserError,
@@ -399,9 +403,7 @@ function translateTypeAnnotation(
399403
return emitBoolean(nullable);
400404
}
401405
case 'TSNumberKeyword': {
402-
return wrapNullable(nullable, {
403-
type: 'NumberTypeAnnotation',
404-
});
406+
return emitNumber(nullable);
405407
}
406408
case 'TSVoidKeyword': {
407409
return wrapNullable(nullable, {

0 commit comments

Comments
 (0)