Create emitInt32Prop function (#37663)

Summary:
Create a function emitInt32Prop(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#L53-L59) and [TypeScript](https://github.com/facebook/react-native/blob/d8ced6f8953cd896471983714e722caf50783960/packages/react-native-codegen/src/parsers/typescript/components/events.js#L63-L69) into that function. Use that function in the original call site.

## Changelog:
[INTERNAL][ADDED] - emitInt32Prop in parser-primitves

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: https://github.com/facebook/react-native/pull/37663

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

Reviewed By: cipolleschi

Differential Revision: D46437495

Pulled By: rshest

fbshipit-source-id: cebe3958c2ad9c4ab9a6c087fee4317b5e57bc58
This commit is contained in:
ahmadao2214
2023-06-05 07:48:22 -07:00
committed by Facebook GitHub Bot
parent 34c19232d3
commit 52154e54a2
4 changed files with 51 additions and 14 deletions
@@ -27,6 +27,7 @@ const {
emitFloatProp,
emitNumber,
emitInt32,
emitInt32Prop,
emitGenericObject,
emitObject,
emitPromise,
@@ -104,6 +105,38 @@ describe('emitInt32', () => {
});
});
describe('emitInt32Prop', () => {
describe('when optional is true', () => {
it('returns optional Int32TypeAnnotation', () => {
const result = emitInt32Prop('myProp', true);
const expected = {
name: 'myProp',
optional: true,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns required Int32TypeAnnotation', () => {
const result = emitInt32Prop('myProp', false);
const expected = {
name: 'myProp',
optional: false,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
});
describe('emitNumber', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
@@ -28,6 +28,7 @@ const {
emitDoubleProp,
emitFloatProp,
emitStringProp,
emitInt32Prop,
} = require('../../parsers-primitives');
function getPropertyType(
@@ -46,13 +47,7 @@ function getPropertyType(
case 'StringTypeAnnotation':
return emitStringProp(name, optional);
case 'Int32':
return {
name,
optional,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};
return emitInt32Prop(name, optional);
case 'Double':
return emitDoubleProp(name, optional);
case 'Float':
@@ -76,6 +76,19 @@ function emitInt32(nullable: boolean): Nullable<Int32TypeAnnotation> {
});
}
function emitInt32Prop(
name: string,
optional: boolean,
): NamedShape<Int32TypeAnnotation> {
return {
name,
optional,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};
}
function emitNumber(
nullable: boolean,
): Nullable<NativeModuleNumberTypeAnnotation> {
@@ -641,6 +654,7 @@ module.exports = {
emitFloatProp,
emitFunction,
emitInt32,
emitInt32Prop,
emitNumber,
emitGenericObject,
emitDictionary,
@@ -30,6 +30,7 @@ const {
emitDoubleProp,
emitFloatProp,
emitStringProp,
emitInt32Prop,
} = require('../../parsers-primitives');
function getPropertyType(
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -55,13 +56,7 @@ function getPropertyType(
case 'TSStringKeyword':
return emitStringProp(name, optional);
case 'Int32':
return {
name,
optional,
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};
return emitInt32Prop(name, optional);
case 'Double':
return emitDoubleProp(name, optional);
case 'Float':