Skip to content

Commit fd4451e

Browse files
Marcoo09facebook-github-bot
authored andcommitted
Chore/extract codegen case object to parser primitives (#34926)
Summary: Part of #34872 This PR extracts the content of the case 'Object' ([Flow](https://github.com/facebook/react-native/blob/b444f0e44e0d8670139acea5f14c2de32c5e2ddc/packages/react-native-codegen/src/parsers/flow/modules/index.js#L365-L367), [TypeScript](https://github.com/facebook/react-native/blob/00b795642a6562fb52d6df12e367b84674994623/packages/react-native-codegen/src/parsers/typescript/modules/index.js#L400-L402)) into a single emitObject function in the parsers-primitives.js file. Use the new function in the parsers. ## Changelog [Internal] [Changed] - Extract contents of the case 'Object' into a single emitObject function Pull Request resolved: #34926 Test Plan: <img width="276" alt="image" src="https://user-images.githubusercontent.com/18408823/194892107-1da9d6e5-c659-47f9-8597-ff4a4b7710ca.png"> Reviewed By: rshest Differential Revision: D40231670 Pulled By: cipolleschi fbshipit-source-id: db6a61427c8c020d48be5317b094f136842b62ca
1 parent b33961d commit fd4451e

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const {
1616
emitDouble,
1717
emitNumber,
1818
emitInt32,
19+
emitObject,
20+
emitPromise,
1921
emitRootTag,
2022
typeAliasResolution,
21-
emitPromise,
2223
} = require('../parsers-primitives.js');
2324

2425
describe('emitBoolean', () => {
@@ -315,3 +316,29 @@ describe('emitPromise', () => {
315316
});
316317
});
317318
});
319+
320+
describe('emitObject', () => {
321+
describe('when nullable is true', () => {
322+
it('returns nullable type annotation', () => {
323+
const result = emitObject(true);
324+
const expected = {
325+
type: 'NullableTypeAnnotation',
326+
typeAnnotation: {
327+
type: 'GenericObjectTypeAnnotation',
328+
},
329+
};
330+
331+
expect(result).toEqual(expected);
332+
});
333+
});
334+
describe('when nullable is false', () => {
335+
it('returns non nullable type annotation', () => {
336+
const result = emitObject(false);
337+
const expected = {
338+
type: 'GenericObjectTypeAnnotation',
339+
};
340+
341+
expect(result).toEqual(expected);
342+
});
343+
});
344+
});

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ const {
4242
emitDouble,
4343
emitNumber,
4444
emitInt32,
45+
emitObject,
46+
emitPromise,
4547
emitRootTag,
4648
typeAliasResolution,
47-
emitPromise,
4849
} = require('../../parsers-primitives');
4950
const {
5051
MisnamedModuleInterfaceParserError,
@@ -218,9 +219,7 @@ function translateTypeAnnotation(
218219
}
219220
case 'UnsafeObject':
220221
case 'Object': {
221-
return wrapNullable(nullable, {
222-
type: 'GenericObjectTypeAnnotation',
223-
});
222+
return emitObject(nullable);
224223
}
225224
default: {
226225
const maybeEumDeclaration = types[typeAnnotation.id.name];

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
BooleanTypeAnnotation,
2020
DoubleTypeAnnotation,
2121
Int32TypeAnnotation,
22+
NativeModuleGenericObjectTypeAnnotation,
2223
ReservedTypeAnnotation,
2324
ObjectTypeAnnotation,
2425
NativeModulePromiseTypeAnnotation,
@@ -135,12 +136,21 @@ function emitPromise(
135136
});
136137
}
137138

139+
function emitObject(
140+
nullable: boolean,
141+
): Nullable<NativeModuleGenericObjectTypeAnnotation> {
142+
return wrapNullable(nullable, {
143+
type: 'GenericObjectTypeAnnotation',
144+
});
145+
}
146+
138147
module.exports = {
139148
emitBoolean,
140149
emitDouble,
141150
emitInt32,
142151
emitNumber,
152+
emitObject,
153+
emitPromise,
143154
emitRootTag,
144155
typeAliasResolution,
145-
emitPromise,
146156
};

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ const {
4242
emitDouble,
4343
emitNumber,
4444
emitInt32,
45+
emitObject,
46+
emitPromise,
4547
emitRootTag,
4648
typeAliasResolution,
47-
emitPromise,
4849
} = require('../../parsers-primitives');
4950
const {
5051
MisnamedModuleInterfaceParserError,
@@ -251,9 +252,7 @@ function translateTypeAnnotation(
251252
}
252253
case 'UnsafeObject':
253254
case 'Object': {
254-
return wrapNullable(nullable, {
255-
type: 'GenericObjectTypeAnnotation',
256-
});
255+
return emitObject(nullable);
257256
}
258257
default: {
259258
const maybeEumDeclaration = types[typeAnnotation.typeName.name];

0 commit comments

Comments
 (0)