Skip to content

Commit 0de4768

Browse files
tarunrajputfacebook-github-bot
authored andcommitted
Codegen 105: add typeAlias prop in parsers (#37334)
Summary: Part of Codegen Issue: #34872 > Add a typeAlias: string property to the Parser class. Implement it in the Flow parser so that it returns TypeAlias and in > the TypeScriptParser so that it returns TSTypeAliasDeclaration. Replace the case in the switch in the [parsers/flow/utils.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/flow/utils.js#L57) and [parsers/typescript/utils.js](https://github.com/facebook/react-native/blob/e133100721939108b0f28dfa9f60ac627c804018/packages/react-native-codegen/src/parsers/typescript/utils.js#L60) with this prop. bypass-github-export-checks ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal][Added]: Add typeAlias property in parsers Pull Request resolved: #37334 Test Plan: ```yarn test``` Reviewed By: dmytrorykun Differential Revision: D45691915 Pulled By: cipolleschi fbshipit-source-id: efe5c87afc3193f6a35325190a67f98d64426ab3
1 parent e09d585 commit 0de4768

File tree

10 files changed

+245
-7
lines changed

10 files changed

+245
-7
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ describe('FlowParser', () => {
305305
expect(parser.isOptionalProperty(property)).toEqual(true);
306306
});
307307
});
308+
309+
describe('typeAlias', () => {
310+
it('returns typeAlias Property', () => {
311+
expect(parser.typeAlias).toEqual('TypeAlias');
312+
});
313+
});
308314
});
309315

310316
describe('TypeScriptParser', () => {
@@ -582,4 +588,10 @@ describe('TypeScriptParser', () => {
582588
expect(parser.isOptionalProperty(property)).toEqual(false);
583589
});
584590
});
591+
592+
describe('typeAlias', () => {
593+
it('returns typeAlias Property', () => {
594+
expect(parser.typeAlias).toEqual('TSTypeAliasDeclaration');
595+
});
596+
});
585597
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
} from '../../../CodegenSchema';
2121

2222
import type {Parser} from '../../parser';
23+
const {resolveTypeAnnotation} = require('../utils');
2324
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2425

2526
const {
@@ -59,7 +60,7 @@ function translateTypeAnnotation(
5960
parser: Parser,
6061
): Nullable<NativeModuleTypeAnnotation> {
6162
const {nullable, typeAnnotation, typeResolutionStatus} =
62-
parser.getResolvedTypeAnnotation(flowTypeAnnotation, types);
63+
resolveTypeAnnotation(flowTypeAnnotation, types, parser);
6364

6465
switch (typeAnnotation.type) {
6566
case 'GenericTypeAnnotation': {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ import type {
2222
NativeModuleEnumMap,
2323
} from '../../CodegenSchema';
2424
import type {ParserType} from '../errors';
25-
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from '../parser';
25+
import type {
26+
GetSchemaInfoFN,
27+
GetTypeAnnotationFN,
28+
Parser,
29+
ResolveTypeAnnotationFN,
30+
} from '../parser';
2631
import type {
2732
ParserErrorCapturer,
2833
TypeDeclarationMap,
2934
PropAST,
3035
TypeResolutionStatus,
3136
} from '../utils';
37+
38+
const {resolveTypeAnnotation} = require('./utils');
3239
const invariant = require('invariant');
3340

3441
const {
@@ -55,6 +62,7 @@ const {
5562

5663
class FlowParser implements Parser {
5764
typeParameterInstantiation: string = 'TypeParameterInstantiation';
65+
typeAlias: string = 'TypeAlias';
5866

5967
isProperty(property: $FlowFixMe): boolean {
6068
return property.type === 'ObjectTypeProperty';
@@ -429,6 +437,10 @@ class FlowParser implements Parser {
429437
typeResolutionStatus,
430438
};
431439
}
440+
441+
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN {
442+
return resolveTypeAnnotation;
443+
}
432444
}
433445

434446
module.exports = {

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,81 @@
1010

1111
'use strict';
1212

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

1589
function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
1690
if (value.type === 'GenericTypeAnnotation' && types[value.id.name]) {
@@ -21,4 +95,5 @@ function getValueFromTypes(value: ASTNode, types: TypeDeclarationMap): ASTNode {
2195

2296
module.exports = {
2397
getValueFromTypes,
98+
resolveTypeAnnotation,
2499
};

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ export type BuildSchemaFN<T> = (
6363
parser: Parser,
6464
) => ?NamedShape<T>;
6565

66+
export type ResolveTypeAnnotationFN = (
67+
typeAnnotation: $FlowFixMe,
68+
types: TypeDeclarationMap,
69+
parser: Parser,
70+
) => {
71+
nullable: boolean,
72+
typeAnnotation: $FlowFixMe,
73+
typeResolutionStatus: TypeResolutionStatus,
74+
};
75+
6676
/**
6777
* This is the main interface for Parsers of various languages.
6878
* It exposes all the methods that contain language-specific logic.
@@ -73,6 +83,11 @@ export interface Parser {
7383
*/
7484
typeParameterInstantiation: string;
7585

86+
/**
87+
* TypeAlias property of the Parser
88+
*/
89+
typeAlias: string;
90+
7691
/**
7792
* Given a declaration, it returns true if it is a property
7893
*/
@@ -328,4 +343,6 @@ export interface Parser {
328343
typeAnnotation: $FlowFixMe,
329344
typeResolutionStatus: TypeResolutionStatus,
330345
};
346+
347+
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN;
331348
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
'use strict';
1212

13-
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from './parser';
13+
import type {
14+
GetSchemaInfoFN,
15+
GetTypeAnnotationFN,
16+
Parser,
17+
ResolveTypeAnnotationFN,
18+
} from './parser';
1419
import type {ParserType} from './errors';
1520
import type {
1621
UnionTypeAnnotationMemberType,
@@ -55,6 +60,7 @@ const schemaMock = {
5560

5661
export class MockedParser implements Parser {
5762
typeParameterInstantiation: string = 'TypeParameterInstantiation';
63+
typeAlias: string = 'TypeAlias';
5864

5965
isProperty(property: $FlowFixMe): boolean {
6066
return property.type === 'ObjectTypeProperty';
@@ -280,6 +286,16 @@ export class MockedParser implements Parser {
280286
};
281287
}
282288

289+
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN {
290+
return () => {
291+
return {
292+
nullable: false,
293+
typeAnnotation: null,
294+
typeResolutionStatus: {successful: false},
295+
};
296+
};
297+
}
298+
283299
getResolvedTypeAnnotation(
284300
typeAnnotation: $FlowFixMe,
285301
types: TypeDeclarationMap,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,11 @@ function buildPropertySchema(
336336
: property.typeAnnotation;
337337
}
338338

339-
({nullable, typeAnnotation: value} = parser.getResolvedTypeAnnotation(
339+
const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN();
340+
({nullable, typeAnnotation: value} = resolveTypeAnnotationFN(
340341
value,
341342
types,
343+
parser,
342344
));
343345

344346
throwIfModuleTypeIsUnsupported(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const {flattenIntersectionType} = require('../parseTopLevelType');
2929
const {flattenProperties} = require('../components/componentsUtils');
3030

3131
const {parseObjectProperty} = require('../../parsers-commons');
32+
const {resolveTypeAnnotation} = require('../utils');
3233

3334
const {
3435
emitArrayType,
@@ -189,6 +190,7 @@ function translateTypeAnnotation(
189190
): Nullable<NativeModuleTypeAnnotation> {
190191
const {nullable, typeAnnotation, typeResolutionStatus} =
191192
parser.getResolvedTypeAnnotation(typeScriptTypeAnnotation, types);
193+
resolveTypeAnnotation(typeScriptTypeAnnotation, types, parser);
192194

193195
switch (typeAnnotation.type) {
194196
case 'TSArrayType': {

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

Lines changed: 12 additions & 2 deletions
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 {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from '../parser';
25+
import type {
26+
GetSchemaInfoFN,
27+
GetTypeAnnotationFN,
28+
Parser,
29+
ResolveTypeAnnotationFN,
30+
} from '../parser';
2631
import type {
2732
ParserErrorCapturer,
2833
TypeDeclarationMap,
@@ -46,7 +51,7 @@ const {
4651
getSchemaInfo,
4752
getTypeAnnotation,
4853
} = require('./components/componentsUtils');
49-
54+
const {resolveTypeAnnotation} = require('./utils');
5055
const fs = require('fs');
5156

5257
const {
@@ -55,6 +60,7 @@ const {
5560

5661
class TypeScriptParser implements Parser {
5762
typeParameterInstantiation: string = 'TSTypeParameterInstantiation';
63+
typeAlias: string = 'TSTypeAliasDeclaration';
5864

5965
isProperty(property: $FlowFixMe): boolean {
6066
return property.type === 'TSPropertySignature';
@@ -437,6 +443,10 @@ class TypeScriptParser implements Parser {
437443
typeResolutionStatus,
438444
};
439445
}
446+
447+
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN {
448+
return resolveTypeAnnotation;
449+
}
440450
}
441451

442452
module.exports = {

0 commit comments

Comments
 (0)