Extract the content of the case 'Int32' (Flow, TypeScript) into a single emitInt32 function in the parsers-primitives.js file. (#34906)

Summary:
Part of https://github.com/facebook/react-native/issues/34872

Refactors emitInt32 in codegen parser module into a common parsers-primitive.js

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Extract the content of the case 'Int32' (Flow, TypeScript) into a single emitInt32 function

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

Test Plan:
Ran `yarn jest react-native-codegen`

<img width="690" alt="Screen Shot 2022-10-09 at 15 45 21" src="https://user-images.githubusercontent.com/6936373/194742142-801a89c8-f803-4e8d-9b05-cf6bda97ea19.png">

Reviewed By: cipolleschi

Differential Revision: D40212544

Pulled By: cipolleschi

fbshipit-source-id: 845e3758ab654edb972a8d8dc3cfa73464e2d49d
This commit is contained in:
Jesse Katsumata 2022-10-09 13:34:38 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 7a2e3468e9
Коммит db8c11d392
4 изменённых файлов: 43 добавлений и 10 удалений

Просмотреть файл

@ -11,7 +11,7 @@
'use-strict';
const {emitBoolean} = require('../parsers-primitives.js');
const {emitBoolean, emitInt32} = require('../parsers-primitives.js');
describe('emitBoolean', () => {
describe('when nullable is true', () => {
@ -38,3 +38,29 @@ describe('emitBoolean', () => {
});
});
});
describe('emitInt32', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitInt32(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'Int32TypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitInt32(false);
const expected = {
type: 'Int32TypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});

Просмотреть файл

@ -33,7 +33,7 @@ const {
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {emitBoolean} = require('../../parsers-primitives');
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedFlowGenericParserError,
MisnamedModuleFlowInterfaceParserError,
@ -195,9 +195,7 @@ function translateTypeAnnotation(
});
}
case 'Int32': {
return wrapNullable(nullable, {
type: 'Int32TypeAnnotation',
});
return emitInt32(nullable);
}
case 'Double': {
return wrapNullable(nullable, {

Просмотреть файл

@ -10,7 +10,11 @@
'use strict';
import type {BooleanTypeAnnotation, Nullable} from '../CodegenSchema';
import type {
BooleanTypeAnnotation,
Int32TypeAnnotation,
Nullable,
} from '../CodegenSchema';
const {wrapNullable} = require('./parsers-commons');
@ -20,6 +24,13 @@ function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
});
}
function emitInt32(nullable: boolean): Nullable<Int32TypeAnnotation> {
return wrapNullable(nullable, {
type: 'Int32TypeAnnotation',
});
}
module.exports = {
emitBoolean,
emitInt32,
};

Просмотреть файл

@ -33,7 +33,7 @@ const {
isModuleRegistryCall,
} = require('../utils.js');
const {unwrapNullable, wrapNullable} = require('../../parsers-commons');
const {emitBoolean} = require('../../parsers-primitives');
const {emitBoolean, emitInt32} = require('../../parsers-primitives');
const {
IncorrectlyParameterizedTypeScriptGenericParserError,
MisnamedModuleTypeScriptInterfaceParserError,
@ -228,9 +228,7 @@ function translateTypeAnnotation(
});
}
case 'Int32': {
return wrapNullable(nullable, {
type: 'Int32TypeAnnotation',
});
return emitInt32(nullable);
}
case 'Double': {
return wrapNullable(nullable, {