Skip to content

Commit b3219fe

Browse files
youeddfacebook-github-bot
authored andcommitted
Extract contents of the case 'VoidTypeAnnotation' into a single emitVoid function (#34932)
Summary: Part of #34872 This PR: - extracts the content of the case 'VoidTypeAnnotation' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L375-L377), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/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: #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
1 parent a885b1f commit b3219fe

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-primitives-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
emitObject,
2020
emitPromise,
2121
emitRootTag,
22+
emitVoid,
2223
typeAliasResolution,
2324
} = require('../parsers-primitives.js');
2425

@@ -152,6 +153,32 @@ describe('emitDouble', () => {
152153
});
153154
});
154155

156+
describe('emitVoid', () => {
157+
describe('when nullable is true', () => {
158+
it('returns nullable type annotation', () => {
159+
const result = emitVoid(true);
160+
const expected = {
161+
type: 'NullableTypeAnnotation',
162+
typeAnnotation: {
163+
type: 'VoidTypeAnnotation',
164+
},
165+
};
166+
167+
expect(result).toEqual(expected);
168+
});
169+
});
170+
describe('when nullable is false', () => {
171+
it('returns non nullable type annotation', () => {
172+
const result = emitVoid(false);
173+
const expected = {
174+
type: 'VoidTypeAnnotation',
175+
};
176+
177+
expect(result).toEqual(expected);
178+
});
179+
});
180+
});
181+
155182
describe('typeAliasResolution', () => {
156183
const objectTypeAnnotation = {
157184
type: 'ObjectTypeAnnotation',

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
emitObject,
4646
emitPromise,
4747
emitRootTag,
48+
emitVoid,
4849
typeAliasResolution,
4950
} = require('../../parsers-primitives');
5051
const {
@@ -345,9 +346,7 @@ function translateTypeAnnotation(
345346
return emitNumber(nullable);
346347
}
347348
case 'VoidTypeAnnotation': {
348-
return wrapNullable(nullable, {
349-
type: 'VoidTypeAnnotation',
350-
});
349+
return emitVoid(nullable);
351350
}
352351
case 'StringTypeAnnotation': {
353352
return wrapNullable(nullable, {

packages/react-native-codegen/src/parsers/parsers-primitives.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
ReservedTypeAnnotation,
2424
ObjectTypeAnnotation,
2525
NativeModulePromiseTypeAnnotation,
26+
VoidTypeAnnotation,
2627
} from '../CodegenSchema';
2728
import type {ParserType} from './errors';
2829
import type {TypeAliasResolutionStatus} from './utils';
@@ -65,6 +66,12 @@ function emitDouble(nullable: boolean): Nullable<DoubleTypeAnnotation> {
6566
});
6667
}
6768

69+
function emitVoid(nullable: boolean): Nullable<VoidTypeAnnotation> {
70+
return wrapNullable(nullable, {
71+
type: 'VoidTypeAnnotation',
72+
});
73+
}
74+
6875
function typeAliasResolution(
6976
typeAliasResolutionStatus: TypeAliasResolutionStatus,
7077
objectTypeAnnotation: ObjectTypeAnnotation<
@@ -152,5 +159,6 @@ module.exports = {
152159
emitObject,
153160
emitPromise,
154161
emitRootTag,
162+
emitVoid,
155163
typeAliasResolution,
156164
};

packages/react-native-codegen/src/parsers/typescript/modules/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
emitObject,
4646
emitPromise,
4747
emitRootTag,
48+
emitVoid,
4849
typeAliasResolution,
4950
} = require('../../parsers-primitives');
5051
const {
@@ -380,9 +381,7 @@ function translateTypeAnnotation(
380381
return emitNumber(nullable);
381382
}
382383
case 'TSVoidKeyword': {
383-
return wrapNullable(nullable, {
384-
type: 'VoidTypeAnnotation',
385-
});
384+
return emitVoid(nullable);
386385
}
387386
case 'TSStringKeyword': {
388387
return wrapNullable(nullable, {

0 commit comments

Comments
 (0)