Skip to content

Commit 3cdaf7f

Browse files
committed
Add getResolvedTypeAnnotation to Parsers
1 parent cceef57 commit 3cdaf7f

File tree

10 files changed

+264
-201
lines changed

10 files changed

+264
-201
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const flowParser = new FlowParser();
5858

5959
const {flowTranslateTypeAnnotation} = require('../flow/modules/index');
6060
const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index');
61-
const {resolveTypeAnnotation} = require('../flow/utils');
6261

6362
beforeEach(() => {
6463
jest.clearAllMocks();
@@ -409,7 +408,7 @@ describe('buildSchemaFromConfigType', () => {
409408
(_ast, _parser) => componentSchemaMock,
410409
);
411410
const buildModuleSchemaMock = jest.fn(
412-
(_0, _1, _2, _3, _4, _5) => moduleSchemaMock,
411+
(_0, _1, _2, _3, _4) => moduleSchemaMock,
413412
);
414413

415414
const buildSchemaFromConfigTypeHelper = (
@@ -424,7 +423,6 @@ describe('buildSchemaFromConfigType', () => {
424423
buildComponentSchemaMock,
425424
buildModuleSchemaMock,
426425
parser,
427-
resolveTypeAnnotation,
428426
flowTranslateTypeAnnotation,
429427
);
430428

@@ -507,7 +505,6 @@ describe('buildSchemaFromConfigType', () => {
507505
astMock,
508506
expect.any(Function),
509507
parser,
510-
resolveTypeAnnotation,
511508
flowTranslateTypeAnnotation,
512509
);
513510

@@ -679,7 +676,6 @@ describe('buildSchema', () => {
679676
buildModuleSchema,
680677
Visitor,
681678
parser,
682-
resolveTypeAnnotation,
683679
flowTranslateTypeAnnotation,
684680
);
685681

@@ -713,7 +709,6 @@ describe('buildSchema', () => {
713709
buildModuleSchema,
714710
Visitor,
715711
flowParser,
716-
resolveTypeAnnotation,
717712
flowTranslateTypeAnnotation,
718713
);
719714

@@ -768,7 +763,6 @@ describe('buildSchema', () => {
768763
buildModuleSchema,
769764
Visitor,
770765
flowParser,
771-
resolveTypeAnnotation,
772766
flowTranslateTypeAnnotation,
773767
);
774768

@@ -1064,7 +1058,6 @@ describe('buildModuleSchema', () => {
10641058
ast,
10651059
tryParse,
10661060
flowParser,
1067-
resolveTypeAnnotation,
10681061
flowTranslateTypeAnnotation,
10691062
),
10701063
).toThrow(expected);
@@ -1079,7 +1072,6 @@ describe('buildModuleSchema', () => {
10791072
ast,
10801073
tryParse,
10811074
flowParser,
1082-
resolveTypeAnnotation,
10831075
flowTranslateTypeAnnotation,
10841076
),
10851077
).not.toThrow();
@@ -1116,7 +1108,6 @@ describe('buildModuleSchema', () => {
11161108
ast,
11171109
tryParse,
11181110
flowParser,
1119-
resolveTypeAnnotation,
11201111
flowTranslateTypeAnnotation,
11211112
),
11221113
).toThrow(expected);
@@ -1131,7 +1122,6 @@ describe('buildModuleSchema', () => {
11311122
ast,
11321123
tryParse,
11331124
flowParser,
1134-
resolveTypeAnnotation,
11351125
flowTranslateTypeAnnotation,
11361126
),
11371127
).not.toThrow();
@@ -1171,7 +1161,6 @@ describe('buildModuleSchema', () => {
11711161
ast,
11721162
tryParse,
11731163
flowParser,
1174-
resolveTypeAnnotation,
11751164
flowTranslateTypeAnnotation,
11761165
),
11771166
).toThrow(expected);
@@ -1186,7 +1175,6 @@ describe('buildModuleSchema', () => {
11861175
ast,
11871176
tryParse,
11881177
flowParser,
1189-
resolveTypeAnnotation,
11901178
flowTranslateTypeAnnotation,
11911179
),
11921180
).not.toThrow();
@@ -1229,7 +1217,6 @@ describe('buildModuleSchema', () => {
12291217
ast,
12301218
tryParse,
12311219
flowParser,
1232-
resolveTypeAnnotation,
12331220
flowTranslateTypeAnnotation,
12341221
);
12351222

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import type {
2222
import type {Parser} from '../../parser';
2323
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2424

25-
const {resolveTypeAnnotation} = require('../utils');
2625
const {
2726
unwrapNullable,
2827
wrapNullable,
@@ -60,7 +59,7 @@ function translateTypeAnnotation(
6059
parser: Parser,
6160
): Nullable<NativeModuleTypeAnnotation> {
6261
const {nullable, typeAnnotation, typeResolutionStatus} =
63-
resolveTypeAnnotation(flowTypeAnnotation, types);
62+
parser.getResolvedTypeAnnotation(flowTypeAnnotation, types);
6463

6564
switch (typeAnnotation.type) {
6665
case 'GenericTypeAnnotation': {

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

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ import type {
2323
} from '../../CodegenSchema';
2424
import type {ParserType} from '../errors';
2525
import type {Parser} from '../parser';
26-
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from '../utils';
26+
import type {
27+
ParserErrorCapturer,
28+
TypeDeclarationMap,
29+
PropAST,
30+
TypeResolutionStatus,
31+
} from '../utils';
32+
const invariant = require('invariant');
2733

2834
const {flowTranslateTypeAnnotation} = require('./modules');
2935

@@ -35,7 +41,6 @@ const {Visitor} = require('../parsers-primitives');
3541
const {buildComponentSchema} = require('./components');
3642
const {wrapComponentSchema} = require('../schema.js');
3743
const {buildModuleSchema} = require('../parsers-commons.js');
38-
const {resolveTypeAnnotation} = require('./utils');
3944

4045
const fs = require('fs');
4146

@@ -106,7 +111,6 @@ class FlowParser implements Parser {
106111
buildModuleSchema,
107112
Visitor,
108113
this,
109-
resolveTypeAnnotation,
110114
flowTranslateTypeAnnotation,
111115
);
112116
}
@@ -343,6 +347,75 @@ class FlowParser implements Parser {
343347
property.value.type === 'NullableTypeAnnotation' || property.optional
344348
);
345349
}
350+
351+
getResolvedTypeAnnotation(
352+
typeAnnotation: $FlowFixMe,
353+
types: TypeDeclarationMap,
354+
): {
355+
nullable: boolean,
356+
typeAnnotation: $FlowFixMe,
357+
typeResolutionStatus: TypeResolutionStatus,
358+
} {
359+
invariant(
360+
typeAnnotation != null,
361+
'resolveTypeAnnotation(): typeAnnotation cannot be null',
362+
);
363+
364+
let node = typeAnnotation;
365+
let nullable = false;
366+
let typeResolutionStatus: TypeResolutionStatus = {
367+
successful: false,
368+
};
369+
370+
for (;;) {
371+
if (node.type === 'NullableTypeAnnotation') {
372+
nullable = true;
373+
node = node.typeAnnotation;
374+
continue;
375+
}
376+
377+
if (node.type !== 'GenericTypeAnnotation') {
378+
break;
379+
}
380+
381+
const resolvedTypeAnnotation = types[node.id.name];
382+
if (resolvedTypeAnnotation == null) {
383+
break;
384+
}
385+
386+
switch (resolvedTypeAnnotation.type) {
387+
case 'TypeAlias': {
388+
typeResolutionStatus = {
389+
successful: true,
390+
type: 'alias',
391+
name: node.id.name,
392+
};
393+
node = resolvedTypeAnnotation.right;
394+
break;
395+
}
396+
case 'EnumDeclaration': {
397+
typeResolutionStatus = {
398+
successful: true,
399+
type: 'enum',
400+
name: node.id.name,
401+
};
402+
node = resolvedTypeAnnotation.body;
403+
break;
404+
}
405+
default: {
406+
throw new TypeError(
407+
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
408+
);
409+
}
410+
}
411+
}
412+
413+
return {
414+
nullable: nullable,
415+
typeAnnotation: node,
416+
typeResolutionStatus,
417+
};
418+
}
346419
}
347420

348421
module.exports = {

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

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,11 @@
1010

1111
'use strict';
1212

13-
import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
13+
import type {TypeDeclarationMap} from '../utils';
1414

1515
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
1616
export type ASTNode = Object;
1717

18-
const invariant = require('invariant');
19-
20-
function resolveTypeAnnotation(
21-
// TODO(T71778680): This is an Flow TypeAnnotation. Flow-type this
22-
typeAnnotation: $FlowFixMe,
23-
types: TypeDeclarationMap,
24-
): {
25-
nullable: boolean,
26-
typeAnnotation: $FlowFixMe,
27-
typeResolutionStatus: TypeResolutionStatus,
28-
} {
29-
invariant(
30-
typeAnnotation != null,
31-
'resolveTypeAnnotation(): typeAnnotation cannot be null',
32-
);
33-
34-
let node = typeAnnotation;
35-
let nullable = false;
36-
let typeResolutionStatus: TypeResolutionStatus = {
37-
successful: false,
38-
};
39-
40-
for (;;) {
41-
if (node.type === 'NullableTypeAnnotation') {
42-
nullable = true;
43-
node = node.typeAnnotation;
44-
continue;
45-
}
46-
47-
if (node.type !== 'GenericTypeAnnotation') {
48-
break;
49-
}
50-
51-
const resolvedTypeAnnotation = types[node.id.name];
52-
if (resolvedTypeAnnotation == null) {
53-
break;
54-
}
55-
56-
switch (resolvedTypeAnnotation.type) {
57-
case 'TypeAlias': {
58-
typeResolutionStatus = {
59-
successful: true,
60-
type: 'alias',
61-
name: node.id.name,
62-
};
63-
node = resolvedTypeAnnotation.right;
64-
break;
65-
}
66-
case 'EnumDeclaration': {
67-
typeResolutionStatus = {
68-
successful: true,
69-
type: 'enum',
70-
name: node.id.name,
71-
};
72-
node = resolvedTypeAnnotation.body;
73-
break;
74-
}
75-
default: {
76-
throw new TypeError(
77-
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
78-
);
79-
}
80-
}
81-
}
82-
83-
return {
84-
nullable: nullable,
85-
typeAnnotation: node,
86-
typeResolutionStatus,
87-
};
88-
}
89-
9018
function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
9119
if (value.type === 'GenericTypeAnnotation' && types[value.id.name]) {
9220
return getValueFromTypes(types[value.id.name].right, types);
@@ -96,5 +24,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
9624

9725
module.exports = {
9826
getValueFromTypes,
99-
resolveTypeAnnotation,
10027
};

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import type {
2222
NativeModuleEnumMap,
2323
} from '../CodegenSchema';
2424
import type {ParserType} from './errors';
25-
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
25+
import type {
26+
ParserErrorCapturer,
27+
TypeDeclarationMap,
28+
PropAST,
29+
TypeResolutionStatus,
30+
} from './utils';
2631

2732
/**
2833
* This is the main interface for Parsers of various languages.
@@ -276,4 +281,13 @@ export interface Parser {
276281
* @returns: a boolean specifying if the Property is optional
277282
*/
278283
isOptionalProperty(property: $FlowFixMe): boolean;
284+
285+
getResolvedTypeAnnotation(
286+
typeAnnotation: $FlowFixMe,
287+
types: TypeDeclarationMap,
288+
): {
289+
nullable: boolean,
290+
typeAnnotation: $FlowFixMe,
291+
typeResolutionStatus: TypeResolutionStatus,
292+
};
279293
}

0 commit comments

Comments
 (0)