Skip to content

Commit

Permalink
Create a function emitFloatProp (facebook#37500)
Browse files Browse the repository at this point in the history
Summary:
part of facebook#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: facebook#37500

Test Plan: `yarn jest packages/react-native-codegen`

Reviewed By: dmytrorykun

Differential Revision: D46073527

Pulled By: cipolleschi

fbshipit-source-id: 556b3aa0e0d330929e006758ec88d0a6889f87bc
  • Loading branch information
foestauf authored and facebook-github-bot committed May 25, 2023
1 parent 8ca085c commit 1a1e399
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
emitDouble,
emitDoubleProp,
emitFloat,
emitFloatProp,
emitNumber,
emitInt32,
emitGenericObject,
Expand Down Expand Up @@ -711,6 +712,38 @@ describe('emitFloat', () => {
});
});

describe('emitFloatProp', () => {
describe('when optional is true', () => {
it('returns optional FloatTypeAnnotation', () => {
const result = emitFloatProp('myProp', true);
const expected = {
name: 'myProp',
optional: true,
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
};

expect(result).toEqual(expected);
});
});

describe('when optional is false', () => {
it('returns required FloatTypeAnnotation', () => {
const result = emitFloatProp('myProp', false);
const expected = {
name: 'myProp',
optional: false,
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
};

expect(result).toEqual(expected);
});
});
});

describe('emitMixed', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const {getEventArgument} = require('../../parsers-commons');
const {
emitBoolProp,
emitDoubleProp,
emitFloatProp,
emitStringProp,
} = require('../../parsers-primitives');

Expand Down Expand Up @@ -55,13 +56,7 @@ function getPropertyType(
case 'Double':
return emitDoubleProp(name, optional);
case 'Float':
return {
name,
optional,
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
};
return emitFloatProp(name, optional);
case '$ReadOnly':
return getPropertyType(
name,
Expand Down
18 changes: 16 additions & 2 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import type {
Nullable,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
EventTypeAnnotation,
Int32TypeAnnotation,
NamedShape,
NativeModuleAliasMap,
NativeModuleEnumMap,
NativeModuleBaseTypeAnnotation,
Expand All @@ -33,8 +35,6 @@ import type {
VoidTypeAnnotation,
NativeModuleObjectTypeAnnotation,
NativeModuleEnumDeclaration,
NamedShape,
EventTypeAnnotation,
} from '../CodegenSchema';
import type {Parser} from './parser';
import type {
Expand Down Expand Up @@ -365,6 +365,19 @@ function emitFloat(
});
}

function emitFloatProp(
name: string,
optional: boolean,
): NamedShape<EventTypeAnnotation> {
return {
name,
optional,
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
};
}

function emitUnion(
nullable: boolean,
hasteModuleName: string,
Expand Down Expand Up @@ -625,6 +638,7 @@ module.exports = {
emitDouble,
emitDoubleProp,
emitFloat,
emitFloatProp,
emitFunction,
emitInt32,
emitNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {getEventArgument} = require('../../parsers-commons');
const {
emitBoolProp,
emitDoubleProp,
emitFloatProp,
emitStringProp,
} = require('../../parsers-primitives');
function getPropertyType(
Expand Down Expand Up @@ -64,13 +65,7 @@ function getPropertyType(
case 'Double':
return emitDoubleProp(name, optional);
case 'Float':
return {
name,
optional,
typeAnnotation: {
type: 'FloatTypeAnnotation',
},
};
return emitFloatProp(name, optional);
case 'TSTypeLiteral':
return {
name,
Expand Down

0 comments on commit 1a1e399

Please sign in to comment.