Skip to content

Commit e09d585

Browse files
siddarthkayfacebook-github-bot
authored andcommitted
Add getResolvedTypeAnnotation to Parsers (#37373)
Summary: [Codegen 104] This PR introduces `getResolvedTypeAnnotation` to the Parser class and implements this function in Typescript and Flow Parsers. We also get rid of usages `resolveTypeAnnotation` from : - `packages/react-native-codegen/src/parsers/typescript/utils.js` - `packages/react-native-codegen/src/parsers/flow/utils.js` and replace it with `parsers.getResolvedTypeAnnotation` as requested on #34872 bypass-github-export-checks ## Changelog: [Internal] [Changed] - Add `getResolvedTypeAnnotation ` to Parsers and update usages. Pull Request resolved: #37373 Test Plan: Run yarn jest react-native-codegen and ensure CI is green Reviewed By: dmytrorykun Differential Revision: D45865651 Pulled By: cipolleschi fbshipit-source-id: fdce6e5059306ebe67121aa1b212e67de864bf84
1 parent 6d6b1fd commit e09d585

File tree

11 files changed

+260
-202
lines changed

11 files changed

+260
-202
lines changed

packages/eslint-plugin-specs/react-native-modules.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function requireModuleParser() {
6969
buildModuleSchema: RNParserCommons.buildModuleSchema,
7070
createParserErrorCapturer: RNParserUtils.createParserErrorCapturer,
7171
parser: new RNFlowParser.FlowParser(),
72-
resolveTypeAnnotation: RNFlowParserUtils.resolveTypeAnnotation,
7372
translateTypeAnnotation: RNModuleParser.flowTranslateTypeAnnotation,
7473
};
7574
}
@@ -149,7 +148,6 @@ function rule(context) {
149148
buildModuleSchema,
150149
createParserErrorCapturer,
151150
parser,
152-
resolveTypeAnnotation,
153151
translateTypeAnnotation,
154152
} = requireModuleParser();
155153

@@ -164,7 +162,6 @@ function rule(context) {
164162
ast,
165163
tryParse,
166164
parser,
167-
resolveTypeAnnotation,
168165
translateTypeAnnotation,
169166
);
170167
});

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 {GetSchemaInfoFN, GetTypeAnnotationFN, 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 {
2935
getSchemaInfo,
@@ -40,7 +46,6 @@ const {Visitor} = require('../parsers-primitives');
4046
const {buildComponentSchema} = require('./components');
4147
const {wrapComponentSchema} = require('../schema.js');
4248
const {buildModuleSchema} = require('../parsers-commons.js');
43-
const {resolveTypeAnnotation} = require('./utils');
4449

4550
const fs = require('fs');
4651

@@ -111,7 +116,6 @@ class FlowParser implements Parser {
111116
buildModuleSchema,
112117
Visitor,
113118
this,
114-
resolveTypeAnnotation,
115119
flowTranslateTypeAnnotation,
116120
);
117121
}
@@ -356,6 +360,75 @@ class FlowParser implements Parser {
356360
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
357361
return getTypeAnnotation;
358362
}
363+
364+
getResolvedTypeAnnotation(
365+
typeAnnotation: $FlowFixMe,
366+
types: TypeDeclarationMap,
367+
): {
368+
nullable: boolean,
369+
typeAnnotation: $FlowFixMe,
370+
typeResolutionStatus: TypeResolutionStatus,
371+
} {
372+
invariant(
373+
typeAnnotation != null,
374+
'resolveTypeAnnotation(): typeAnnotation cannot be null',
375+
);
376+
377+
let node = typeAnnotation;
378+
let nullable = false;
379+
let typeResolutionStatus: TypeResolutionStatus = {
380+
successful: false,
381+
};
382+
383+
for (;;) {
384+
if (node.type === 'NullableTypeAnnotation') {
385+
nullable = true;
386+
node = node.typeAnnotation;
387+
continue;
388+
}
389+
390+
if (node.type !== 'GenericTypeAnnotation') {
391+
break;
392+
}
393+
394+
const resolvedTypeAnnotation = types[node.id.name];
395+
if (resolvedTypeAnnotation == null) {
396+
break;
397+
}
398+
399+
switch (resolvedTypeAnnotation.type) {
400+
case 'TypeAlias': {
401+
typeResolutionStatus = {
402+
successful: true,
403+
type: 'alias',
404+
name: node.id.name,
405+
};
406+
node = resolvedTypeAnnotation.right;
407+
break;
408+
}
409+
case 'EnumDeclaration': {
410+
typeResolutionStatus = {
411+
successful: true,
412+
type: 'enum',
413+
name: node.id.name,
414+
};
415+
node = resolvedTypeAnnotation.body;
416+
break;
417+
}
418+
default: {
419+
throw new TypeError(
420+
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
421+
);
422+
}
423+
}
424+
}
425+
426+
return {
427+
nullable: nullable,
428+
typeAnnotation: node,
429+
typeResolutionStatus,
430+
};
431+
}
359432
}
360433

361434
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,79 +10,7 @@
1010

1111
'use strict';
1212

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

8715
function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
8816
if (value.type === 'GenericTypeAnnotation' && types[value.id.name]) {
@@ -93,5 +21,4 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
9321

9422
module.exports = {
9523
getValueFromTypes,
96-
resolveTypeAnnotation,
9724
};

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type {
2727
TypeDeclarationMap,
2828
PropAST,
2929
ASTNode,
30+
TypeResolutionStatus,
3031
} from './utils';
3132

3233
export type GetTypeAnnotationFN = (
@@ -318,4 +319,13 @@ export interface Parser {
318319
getGetTypeAnnotationFN(): GetTypeAnnotationFN;
319320

320321
getGetSchemaInfoFN(): GetSchemaInfoFN;
322+
323+
getResolvedTypeAnnotation(
324+
typeAnnotation: $FlowFixMe,
325+
types: TypeDeclarationMap,
326+
): {
327+
nullable: boolean,
328+
typeAnnotation: $FlowFixMe,
329+
typeResolutionStatus: TypeResolutionStatus,
330+
};
321331
}

0 commit comments

Comments
 (0)