Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one (#36549)

Summary:
Task from https://github.com/facebook/react-native/issues/34872
> [Codegen 80] Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one

## Changelog:

[Internal] [Changed] - Convert the emitCommonTypes implementation from a switch based implementation to a dictionary based one

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

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

Reviewed By: NickGerleman

Differential Revision: D44244901

Pulled By: rshest

fbshipit-source-id: 50712724c72aad3bd1dae3e7c381242c4913a236
This commit is contained in:
Kyaw Thura
2023-03-22 13:51:39 -07:00
committed by Facebook GitHub Bot
parent cf43f9c24b
commit 4a15f90208
2 changed files with 39 additions and 36 deletions
@@ -1263,6 +1263,7 @@ describe('emitPartial', () => {
nullable: boolean,
): $FlowFixMe {
return emitPartial(
nullable,
hasteModuleName,
typeAnnotation,
/* types: TypeDeclarationMap */
@@ -1278,7 +1279,6 @@ describe('emitPartial', () => {
},
/* cxxOnly: boolean */
false,
nullable,
parser,
);
}
@@ -1494,4 +1494,15 @@ describe('emitCommonTypes', () => {
expect(result).toEqual(expected);
});
});
describe('when typeAnnotation is invalid', () => {
const typeAnnotation = {
id: {
name: 'InvalidName',
},
};
it('returns null', () => {
expect(emitCommonTypesForUnitTest(typeAnnotation, false)).toBeNull();
});
});
});
@@ -476,6 +476,7 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): {
}
function emitPartial(
nullable: boolean,
hasteModuleName: string,
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
@@ -483,7 +484,6 @@ function emitPartial(
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
nullable: boolean,
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
throwIfPartialWithMoreParameter(typeAnnotation);
@@ -524,41 +524,33 @@ function emitCommonTypes(
const genericTypeAnnotationName =
parser.nameForGenericTypeAnnotation(typeAnnotation);
switch (genericTypeAnnotationName) {
case 'Stringish': {
return emitStringish(nullable);
}
case 'Int32': {
return emitInt32(nullable);
}
case 'Double': {
return emitDouble(nullable);
}
case 'Float': {
return emitFloat(nullable);
}
case 'UnsafeObject':
case 'Object': {
return emitGenericObject(nullable);
}
case '$Partial':
case 'Partial': {
return emitPartial(
hasteModuleName,
typeAnnotation,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
nullable,
parser,
);
}
default: {
return null;
}
const typeMap = {
Stringish: emitStringish,
Int32: emitInt32,
Double: emitDouble,
Float: emitFloat,
UnsafeObject: emitGenericObject,
Object: emitGenericObject,
$Partial: emitPartial,
Partial: emitPartial,
};
const emitter = typeMap[genericTypeAnnotationName];
if (!emitter) {
return null;
}
return emitter(
nullable,
hasteModuleName,
typeAnnotation,
types,
aliasMap,
enumMap,
tryParse,
cxxOnly,
parser,
);
}
module.exports = {