Skip to content

Commit

Permalink
Create a function emitDoubleProp (facebook#37515)
Browse files Browse the repository at this point in the history
Summary:
> Create a function emitDoubleProp(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#L61-L67) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L71-L77) into that function. Use that function in the original call site.

bypass-github-export-checks

## Changelog:

[INTERNAL][ADDED] - emitDoubleProp in parser primitves

Pull Request resolved: facebook#37515

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

Reviewed By: cortinico

Differential Revision: D46149450

Pulled By: cipolleschi

fbshipit-source-id: 78381214a79c33d975dff490599d510e8001254e
  • Loading branch information
rota-rossi authored and facebook-github-bot committed May 25, 2023
1 parent 7062398 commit 8ca085c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

'use-strict';

import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
import type {
DoubleTypeAnnotation,
NamedShape,
UnionTypeAnnotationMemberType,
} from '../../CodegenSchema';

const {
emitArrayType,
emitBoolean,
emitBoolProp,
emitDouble,
emitDoubleProp,
emitFloat,
emitNumber,
emitInt32,
Expand Down Expand Up @@ -262,6 +267,35 @@ describe('emitDouble', () => {
});
});

describe('emitDoubleProp', () => {
describe('when optional is true', () => {
it('returns optional type annotation', () => {
const result = emitDoubleProp('Foo', true);
const expected: NamedShape<DoubleTypeAnnotation> = {
name: 'Foo',
optional: true,
typeAnnotation: {
type: 'DoubleTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when optional is false', () => {
it('returns required type annotation', () => {
const result = emitDoubleProp('Foo', false);
const expected: NamedShape<DoubleTypeAnnotation> = {
name: 'Foo',
optional: false,
typeAnnotation: {
type: 'DoubleTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
});

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

function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
Expand All @@ -49,13 +53,7 @@ function getPropertyType(
},
};
case 'Double':
return {
name,
optional,
typeAnnotation: {
type: 'DoubleTypeAnnotation',
},
};
return emitDoubleProp(name, optional);
case 'Float':
return {
name,
Expand Down
14 changes: 14 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ function emitDouble(nullable: boolean): Nullable<DoubleTypeAnnotation> {
});
}

function emitDoubleProp(
name: string,
optional: boolean,
): NamedShape<DoubleTypeAnnotation> {
return {
name,
optional,
typeAnnotation: {
type: 'DoubleTypeAnnotation',
},
};
}

function emitVoid(nullable: boolean): Nullable<VoidTypeAnnotation> {
return wrapNullable(nullable, {
type: 'VoidTypeAnnotation',
Expand Down Expand Up @@ -610,6 +623,7 @@ module.exports = {
emitBoolean,
emitBoolProp,
emitDouble,
emitDoubleProp,
emitFloat,
emitFunction,
emitInt32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ const {
throwIfArgumentPropsAreNull,
} = require('../../error-utils');
const {getEventArgument} = require('../../parsers-commons');
const {emitBoolProp, emitStringProp} = require('../../parsers-primitives');

const {
emitBoolProp,
emitDoubleProp,
emitStringProp,
} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
Expand Down Expand Up @@ -59,13 +62,7 @@ function getPropertyType(
},
};
case 'Double':
return {
name,
optional,
typeAnnotation: {
type: 'DoubleTypeAnnotation',
},
};
return emitDoubleProp(name, optional);
case 'Float':
return {
name,
Expand Down

0 comments on commit 8ca085c

Please sign in to comment.