Skip to content

Commit 1a1e399

Browse files
foestauffacebook-github-bot
authored andcommitted
Create a function emitFloatProp (#37500)
Summary: part of #34872 > Create a function emitFloatProp(name: string, optional: boolean) in parser-primitives.js. Factor out the code from [Flow](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/flow/components/events.js#L69-L75) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L79-L85) into that function. Use that function in the original call site. bypass-github-export-checks ## Changelog: [INTERNAL] [ADDED] - emitFloatProp in parser primitves Pull Request resolved: #37500 Test Plan: `yarn jest packages/react-native-codegen` Reviewed By: dmytrorykun Differential Revision: D46073527 Pulled By: cipolleschi fbshipit-source-id: 556b3aa0e0d330929e006758ec88d0a6889f87bc
1 parent 8ca085c commit 1a1e399

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
emitDouble,
2525
emitDoubleProp,
2626
emitFloat,
27+
emitFloatProp,
2728
emitNumber,
2829
emitInt32,
2930
emitGenericObject,
@@ -711,6 +712,38 @@ describe('emitFloat', () => {
711712
});
712713
});
713714

715+
describe('emitFloatProp', () => {
716+
describe('when optional is true', () => {
717+
it('returns optional FloatTypeAnnotation', () => {
718+
const result = emitFloatProp('myProp', true);
719+
const expected = {
720+
name: 'myProp',
721+
optional: true,
722+
typeAnnotation: {
723+
type: 'FloatTypeAnnotation',
724+
},
725+
};
726+
727+
expect(result).toEqual(expected);
728+
});
729+
});
730+
731+
describe('when optional is false', () => {
732+
it('returns required FloatTypeAnnotation', () => {
733+
const result = emitFloatProp('myProp', false);
734+
const expected = {
735+
name: 'myProp',
736+
optional: false,
737+
typeAnnotation: {
738+
type: 'FloatTypeAnnotation',
739+
},
740+
};
741+
742+
expect(result).toEqual(expected);
743+
});
744+
});
745+
});
746+
714747
describe('emitMixed', () => {
715748
describe('when nullable is true', () => {
716749
it('returns nullable type annotation', () => {

packages/react-native-codegen/src/parsers/flow/components/events.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {getEventArgument} = require('../../parsers-commons');
2626
const {
2727
emitBoolProp,
2828
emitDoubleProp,
29+
emitFloatProp,
2930
emitStringProp,
3031
} = require('../../parsers-primitives');
3132

@@ -55,13 +56,7 @@ function getPropertyType(
5556
case 'Double':
5657
return emitDoubleProp(name, optional);
5758
case 'Float':
58-
return {
59-
name,
60-
optional,
61-
typeAnnotation: {
62-
type: 'FloatTypeAnnotation',
63-
},
64-
};
59+
return emitFloatProp(name, optional);
6560
case '$ReadOnly':
6661
return getPropertyType(
6762
name,

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import type {
1414
Nullable,
1515
BooleanTypeAnnotation,
1616
DoubleTypeAnnotation,
17+
EventTypeAnnotation,
1718
Int32TypeAnnotation,
19+
NamedShape,
1820
NativeModuleAliasMap,
1921
NativeModuleEnumMap,
2022
NativeModuleBaseTypeAnnotation,
@@ -33,8 +35,6 @@ import type {
3335
VoidTypeAnnotation,
3436
NativeModuleObjectTypeAnnotation,
3537
NativeModuleEnumDeclaration,
36-
NamedShape,
37-
EventTypeAnnotation,
3838
} from '../CodegenSchema';
3939
import type {Parser} from './parser';
4040
import type {
@@ -365,6 +365,19 @@ function emitFloat(
365365
});
366366
}
367367

368+
function emitFloatProp(
369+
name: string,
370+
optional: boolean,
371+
): NamedShape<EventTypeAnnotation> {
372+
return {
373+
name,
374+
optional,
375+
typeAnnotation: {
376+
type: 'FloatTypeAnnotation',
377+
},
378+
};
379+
}
380+
368381
function emitUnion(
369382
nullable: boolean,
370383
hasteModuleName: string,
@@ -625,6 +638,7 @@ module.exports = {
625638
emitDouble,
626639
emitDoubleProp,
627640
emitFloat,
641+
emitFloatProp,
628642
emitFunction,
629643
emitInt32,
630644
emitNumber,

packages/react-native-codegen/src/parsers/typescript/components/events.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {getEventArgument} = require('../../parsers-commons');
2828
const {
2929
emitBoolProp,
3030
emitDoubleProp,
31+
emitFloatProp,
3132
emitStringProp,
3233
} = require('../../parsers-primitives');
3334
function getPropertyType(
@@ -64,13 +65,7 @@ function getPropertyType(
6465
case 'Double':
6566
return emitDoubleProp(name, optional);
6667
case 'Float':
67-
return {
68-
name,
69-
optional,
70-
typeAnnotation: {
71-
type: 'FloatTypeAnnotation',
72-
},
73-
};
68+
return emitFloatProp(name, optional);
7469
case 'TSTypeLiteral':
7570
return {
7671
name,

0 commit comments

Comments
 (0)