Skip to content

Commit

Permalink
Convert the emitCommonTypes implementation from a switch based implem…
Browse files Browse the repository at this point in the history
…entation to a dictionary based one (#36549)

Summary:
Task from #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: #36549

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

Reviewed By: NickGerleman

Differential Revision: D44244901

Pulled By: rshest

fbshipit-source-id: 50712724c72aad3bd1dae3e7c381242c4913a236
  • Loading branch information
kyawthura-gg authored and facebook-github-bot committed Mar 22, 2023
1 parent cf43f9c commit 4a15f90
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ describe('emitPartial', () => {
nullable: boolean,
): $FlowFixMe {
return emitPartial(
nullable,
hasteModuleName,
typeAnnotation,
/* types: TypeDeclarationMap */
Expand All @@ -1278,7 +1279,6 @@ describe('emitPartial', () => {
},
/* cxxOnly: boolean */
false,
nullable,
parser,
);
}
Expand Down Expand Up @@ -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();
});
});
});
62 changes: 27 additions & 35 deletions packages/react-native-codegen/src/parsers/parsers-primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,14 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): {
}

function emitPartial(
nullable: boolean,
hasteModuleName: string,
typeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
enumMap: {...NativeModuleEnumMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
nullable: boolean,
parser: Parser,
): Nullable<NativeModuleTypeAnnotation> {
throwIfPartialWithMoreParameter(typeAnnotation);
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 4a15f90

Please sign in to comment.