Chore/extract codegen case object to parser primitives (#34926)

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

This PR extracts the content of the case 'Object' ([Flow](b444f0e44e/packages/react-native-codegen/src/parsers/flow/modules/index.js (L365-L367)), [TypeScript](00b795642a/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Marco Fiorito 2022-10-11 15:17:13 -07:00 коммит произвёл Facebook GitHub Bot
Родитель b33961d7a0
Коммит fd4451ecfa
4 изменённых файлов: 45 добавлений и 10 удалений

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

@ -16,9 +16,10 @@ const {
emitDouble,
emitNumber,
emitInt32,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../parsers-primitives.js');
describe('emitBoolean', () => {
@ -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);
});
});
});

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

@ -42,9 +42,10 @@ const {
emitDouble,
emitNumber,
emitInt32,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
@ -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];

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

@ -19,6 +19,7 @@ import type {
BooleanTypeAnnotation,
DoubleTypeAnnotation,
Int32TypeAnnotation,
NativeModuleGenericObjectTypeAnnotation,
ReservedTypeAnnotation,
ObjectTypeAnnotation,
NativeModulePromiseTypeAnnotation,
@ -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,
};

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

@ -42,9 +42,10 @@ const {
emitDouble,
emitNumber,
emitInt32,
emitObject,
emitPromise,
emitRootTag,
typeAliasResolution,
emitPromise,
} = require('../../parsers-primitives');
const {
MisnamedModuleInterfaceParserError,
@ -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];