Skip to content

Commit 66ae98e

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
chore: Expand Codegen emitCommonTypes function adding the remaining basic types (#36706)
Summary: [Codegen 81] This PR expands the `emitCommonTypes` function adding support for the remaining basic types and adding a `convertKeywordToTypeannotation` function to the codegen Parser class and implementing it in the Flow and TypeScript parsers as requested on #34872 ## Changelog: [Internal] [Added] - Expand Codegen emitCommonTypes function adding the remaining basic types Pull Request resolved: #36706 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green Reviewed By: christophpurrer Differential Revision: D44504571 Pulled By: cipolleschi fbshipit-source-id: 220d9bc0cc39614002a67dafd626e5a4878a1447
1 parent a44d8a0 commit 66ae98e

File tree

7 files changed

+85
-56
lines changed

7 files changed

+85
-56
lines changed

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,10 @@ const {
3131
} = require('../../parsers-commons');
3232
const {
3333
emitArrayType,
34-
emitBoolean,
3534
emitFunction,
36-
emitNumber,
3735
emitGenericObject,
3836
emitPromise,
3937
emitRootTag,
40-
emitVoid,
41-
emitString,
42-
emitMixed,
4338
emitUnion,
4439
emitCommonTypes,
4540
typeAliasResolution,
@@ -207,18 +202,6 @@ function translateTypeAnnotation(
207202
nullable,
208203
);
209204
}
210-
case 'BooleanTypeAnnotation': {
211-
return emitBoolean(nullable);
212-
}
213-
case 'NumberTypeAnnotation': {
214-
return emitNumber(nullable);
215-
}
216-
case 'VoidTypeAnnotation': {
217-
return emitVoid(nullable);
218-
}
219-
case 'StringTypeAnnotation': {
220-
return emitString(nullable);
221-
}
222205
case 'FunctionTypeAnnotation': {
223206
return emitFunction(
224207
nullable,
@@ -243,13 +226,6 @@ function translateTypeAnnotation(
243226
memberType: 'StringTypeAnnotation',
244227
});
245228
}
246-
case 'MixedTypeAnnotation': {
247-
if (cxxOnly) {
248-
return emitMixed(nullable);
249-
} else {
250-
return emitGenericObject(nullable);
251-
}
252-
}
253229
case 'EnumStringBody':
254230
case 'EnumNumberBody': {
255231
return typeEnumResolution(
@@ -262,11 +238,26 @@ function translateTypeAnnotation(
262238
);
263239
}
264240
default: {
265-
throw new UnsupportedTypeAnnotationParserError(
241+
const commonType = emitCommonTypes(
266242
hasteModuleName,
243+
types,
267244
typeAnnotation,
268-
parser.language(),
245+
aliasMap,
246+
enumMap,
247+
tryParse,
248+
cxxOnly,
249+
nullable,
250+
parser,
269251
);
252+
253+
if (!commonType) {
254+
throw new UnsupportedTypeAnnotationParserError(
255+
hasteModuleName,
256+
typeAnnotation,
257+
parser.language(),
258+
);
259+
}
260+
return commonType;
270261
}
271262
}
272263
}

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FlowParser implements Parser {
6767
}
6868

6969
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
70-
return typeAnnotation.id.name;
70+
return typeAnnotation?.id?.name;
7171
}
7272

7373
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
@@ -325,6 +325,10 @@ class FlowParser implements Parser {
325325
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
326326
return typeAlias.body.properties;
327327
}
328+
329+
convertKeywordToTypeAnnotation(keyword: string): string {
330+
return keyword;
331+
}
328332
}
329333

330334
module.exports = {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,11 @@ export interface Parser {
248248
* @returns: an array of properties.
249249
*/
250250
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe>;
251+
252+
/**
253+
* Given a keyword convert it to TypeAnnotation.
254+
* @parameter keyword
255+
* @returns: converted TypeAnnotation to Keywords
256+
*/
257+
convertKeywordToTypeAnnotation(keyword: string): string;
251258
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,8 @@ export class MockedParser implements Parser {
239239
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
240240
return typeAlias.body.properties;
241241
}
242+
243+
convertKeywordToTypeAnnotation(keyword: string): string {
244+
return keyword;
245+
}
242246
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,6 @@ function emitCommonTypes(
521521
nullable: boolean,
522522
parser: Parser,
523523
): $FlowFixMe {
524-
const genericTypeAnnotationName =
525-
parser.nameForGenericTypeAnnotation(typeAnnotation);
526-
527524
const typeMap = {
528525
Stringish: emitStringish,
529526
Int32: emitInt32,
@@ -533,8 +530,25 @@ function emitCommonTypes(
533530
Object: emitGenericObject,
534531
$Partial: emitPartial,
535532
Partial: emitPartial,
533+
BooleanTypeAnnotation: emitBoolean,
534+
NumberTypeAnnotation: emitNumber,
535+
VoidTypeAnnotation: emitVoid,
536+
StringTypeAnnotation: emitString,
537+
MixedTypeAnnotation: cxxOnly ? emitMixed : emitGenericObject,
536538
};
537539

540+
const typeAnnotationName = parser.convertKeywordToTypeAnnotation(
541+
typeAnnotation.type,
542+
);
543+
544+
const simpleEmitter = typeMap[typeAnnotationName];
545+
if (simpleEmitter) {
546+
return simpleEmitter(nullable);
547+
}
548+
549+
const genericTypeAnnotationName =
550+
parser.nameForGenericTypeAnnotation(typeAnnotation);
551+
538552
const emitter = typeMap[genericTypeAnnotationName];
539553
if (!emitter) {
540554
return null;

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

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,10 @@ const {parseObjectProperty} = require('../../parsers-commons');
3434

3535
const {
3636
emitArrayType,
37-
emitBoolean,
3837
emitFunction,
39-
emitNumber,
4038
emitGenericObject,
4139
emitPromise,
4240
emitRootTag,
43-
emitVoid,
44-
emitString,
45-
emitMixed,
4641
emitUnion,
4742
emitCommonTypes,
4843
typeAliasResolution,
@@ -353,18 +348,6 @@ function translateTypeAnnotation(
353348
parser,
354349
);
355350
}
356-
case 'TSBooleanKeyword': {
357-
return emitBoolean(nullable);
358-
}
359-
case 'TSNumberKeyword': {
360-
return emitNumber(nullable);
361-
}
362-
case 'TSVoidKeyword': {
363-
return emitVoid(nullable);
364-
}
365-
case 'TSStringKeyword': {
366-
return emitString(nullable);
367-
}
368351
case 'TSFunctionType': {
369352
return emitFunction(
370353
nullable,
@@ -382,18 +365,27 @@ function translateTypeAnnotation(
382365
case 'TSUnionType': {
383366
return emitUnion(nullable, hasteModuleName, typeAnnotation, parser);
384367
}
385-
case 'TSUnknownKeyword': {
386-
if (cxxOnly) {
387-
return emitMixed(nullable);
388-
}
389-
// Fallthrough
390-
}
391368
default: {
392-
throw new UnsupportedTypeAnnotationParserError(
369+
const commonType = emitCommonTypes(
393370
hasteModuleName,
371+
types,
394372
typeAnnotation,
395-
parser.language(),
373+
aliasMap,
374+
enumMap,
375+
tryParse,
376+
cxxOnly,
377+
nullable,
378+
parser,
396379
);
380+
381+
if (!commonType) {
382+
throw new UnsupportedTypeAnnotationParserError(
383+
hasteModuleName,
384+
typeAnnotation,
385+
parser.language(),
386+
);
387+
}
388+
return commonType;
397389
}
398390
}
399391
}

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TypeScriptParser implements Parser {
6767
}
6868

6969
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
70-
return typeAnnotation.typeName.name;
70+
return typeAnnotation?.typeName?.name;
7171
}
7272

7373
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
@@ -311,6 +311,23 @@ class TypeScriptParser implements Parser {
311311
bodyProperties(typeAlias: TypeDeclarationMap): $ReadOnlyArray<$FlowFixMe> {
312312
return typeAlias.body.body;
313313
}
314+
315+
convertKeywordToTypeAnnotation(keyword: string): string {
316+
switch (keyword) {
317+
case 'TSBooleanKeyword':
318+
return 'BooleanTypeAnnotation';
319+
case 'TSNumberKeyword':
320+
return 'NumberTypeAnnotation';
321+
case 'TSVoidKeyword':
322+
return 'VoidTypeAnnotation';
323+
case 'TSStringKeyword':
324+
return 'StringTypeAnnotation';
325+
case 'TSUnknownKeyword':
326+
return 'MixedTypeAnnotation';
327+
}
328+
329+
return keyword;
330+
}
314331
}
315332

316333
module.exports = {

0 commit comments

Comments
 (0)