Skip to content

Commit

Permalink
Chore/extract codegen case object to parser primitives (#34926)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Marcoo09 authored and facebook-github-bot committed Oct 11, 2022
1 parent b33961d commit fd4451e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
Expand Up @@ -16,9 +16,10 @@ const {
emitDouble,
emitNumber,
emitInt32,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../parsers-primitives.js');

describe('emitBoolean', () => {
Expand Down Expand Up @@ -315,3 +316,29 @@ describe('emitPromise', () => {
});
});
});

describe('emitObject', () => {
describe('when nullable is true', () => {
it('returns nullable type annotation', () => {
const result = emitObject(true);
const expected = {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'GenericObjectTypeAnnotation',
},
};

expect(result).toEqual(expected);
});
});
describe('when nullable is false', () => {
it('returns non nullable type annotation', () => {
const result = emitObject(false);
const expected = {
type: 'GenericObjectTypeAnnotation',
};

expect(result).toEqual(expected);
});
});
});
Expand Up @@ -42,9 +42,10 @@ const {
emitDouble,
emitNumber,
emitInt32,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
Expand Down Expand Up @@ -218,9 +219,7 @@ function translateTypeAnnotation(
}
case 'UnsafeObject':
case 'Object': {
return wrapNullable(nullable, {
type: 'GenericObjectTypeAnnotation',
});
return emitObject(nullable);
}
default: {
const maybeEumDeclaration = types[typeAnnotation.id.name];
Expand Down
12 changes: 11 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-primitives.js
Expand Up @@ -19,6 +19,7 @@ import type {
BooleanTypeAnnotation,
DoubleTypeAnnotation,
Int32TypeAnnotation,
NativeModuleGenericObjectTypeAnnotation,
ReservedTypeAnnotation,
ObjectTypeAnnotation,
NativeModulePromiseTypeAnnotation,
Expand Down Expand Up @@ -135,12 +136,21 @@ function emitPromise(
});
}
function emitObject(
nullable: boolean,
): Nullable<NativeModuleGenericObjectTypeAnnotation> {
return wrapNullable(nullable, {
type: 'GenericObjectTypeAnnotation',
});
}

module.exports = {
emitBoolean,
emitDouble,
emitInt32,
emitNumber,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
};
Expand Up @@ -42,9 +42,10 @@ const {
emitDouble,
emitNumber,
emitInt32,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
Expand Down Expand Up @@ -251,9 +252,7 @@ function translateTypeAnnotation(
}
case 'UnsafeObject':
case 'Object': {
return wrapNullable(nullable, {
type: 'GenericObjectTypeAnnotation',
});
return emitObject(nullable);
}
default: {
const maybeEumDeclaration = types[typeAnnotation.typeName.name];
Expand Down

0 comments on commit fd4451e

Please sign in to comment.