Extract contents of the case 'VoidTypeAnnotation' into a single emitVoid function (#34932)

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

This PR:
- extracts the content of the case 'VoidTypeAnnotation' ([Flow](b444f0e44e/packages/react-native-codegen/src/parsers/flow/modules/index.js (L375-L377)), [TypeScript](00b795642a/packages/react-native-codegen/src/parsers/typescript/modules/index.js (L410-L412))) into a single emitVoid function in the parsers-primitives.js file. Use the new function in the parsers.
- unit tests emitVoid function

## Changelog

[Internal] [Changed] - Extract contents of the case 'VoidTypeAnnotation' into a single emitVoid function

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

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

<img width="957" alt="image" src="https://user-images.githubusercontent.com/19575877/194931977-d5cfc5f5-c9db-498d-9e5c-ae40a38d3623.png">

Reviewed By: cipolleschi

Differential Revision: D40239730

Pulled By: rshest

fbshipit-source-id: 16a9555223cacbb3b9916fd469bd63f83db33f18
This commit is contained in:
youedd 2022-10-12 03:06:04 -07:00 коммит произвёл Facebook GitHub Bot
Родитель a885b1fcfd
Коммит b3219fe345
4 изменённых файлов: 39 добавлений и 6 удалений

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

@ -19,6 +19,7 @@ const {
emitObject,
emitPromise,
emitRootTag,
emitVoid,
typeAliasResolution,
} = require('../parsers-primitives.js');
@ -152,6 +153,32 @@ describe('emitDouble', () => {
});
});
describe('emitVoid', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitVoid(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'VoidTypeAnnotation',
},
};
expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitVoid(false);
const expected = {
type: 'VoidTypeAnnotation',
};
expect(result).toEqual(expected);
});
});
});
describe('typeAliasResolution', () => {
const objectTypeAnnotation = {
type: 'ObjectTypeAnnotation',

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

@ -45,6 +45,7 @@ const {
emitObject,
emitPromise,
emitRootTag,
emitVoid,
typeAliasResolution,
} = require('../../parsers-primitives');
const {
@ -345,9 +346,7 @@ function translateTypeAnnotation(
return emitNumber(nullable);
}
case 'VoidTypeAnnotation': {
return wrapNullable(nullable, {
type: 'VoidTypeAnnotation',
});
return emitVoid(nullable);
}
case 'StringTypeAnnotation': {
return wrapNullable(nullable, {

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

@ -23,6 +23,7 @@ import type {
ReservedTypeAnnotation,
ObjectTypeAnnotation,
NativeModulePromiseTypeAnnotation,
VoidTypeAnnotation,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {TypeAliasResolutionStatus} from './utils';
@ -65,6 +66,12 @@ function emitDouble(nullable: boolean): Nullable<DoubleTypeAnnotation> {
});
}
function emitVoid(nullable: boolean): Nullable<VoidTypeAnnotation> {
return wrapNullable(nullable, {
type: 'VoidTypeAnnotation',
});
}
function typeAliasResolution(
typeAliasResolutionStatus: TypeAliasResolutionStatus,
objectTypeAnnotation: ObjectTypeAnnotation<
@ -152,5 +159,6 @@ module.exports = {
emitObject,
emitPromise,
emitRootTag,
emitVoid,
typeAliasResolution,
};

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

@ -45,6 +45,7 @@ const {
emitObject,
emitPromise,
emitRootTag,
emitVoid,
typeAliasResolution,
} = require('../../parsers-primitives');
const {
@ -380,9 +381,7 @@ function translateTypeAnnotation(
return emitNumber(nullable);
}
case 'TSVoidKeyword': {
return wrapNullable(nullable, {
type: 'VoidTypeAnnotation',
});
return emitVoid(nullable);
}
case 'TSStringKeyword': {
return wrapNullable(nullable, {