-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathStructCollector.js
More file actions
256 lines (236 loc) · 6.99 KB
/
StructCollector.js
File metadata and controls
256 lines (236 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {
BigIntTypeAnnotation,
BooleanLiteralTypeAnnotation,
BooleanTypeAnnotation,
DoubleTypeAnnotation,
FloatTypeAnnotation,
Int32TypeAnnotation,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleEnumDeclaration,
NativeModuleGenericObjectTypeAnnotation,
NativeModuleNumberTypeAnnotation,
NativeModuleObjectTypeAnnotation,
NativeModuleTypeAliasTypeAnnotation,
NativeModuleUnionTypeAnnotation,
Nullable,
NumberLiteralTypeAnnotation,
ReservedTypeAnnotation,
StringLiteralTypeAnnotation,
StringTypeAnnotation,
} from '../../../CodegenSchema';
import type {AliasResolver} from '../Utils';
const {
unwrapNullable,
wrapNullable,
} = require('../../../parsers/parsers-commons');
const {
HeterogeneousUnionError,
capitalize,
parseValidUnionType,
} = require('../../Utils');
export type StructContext = 'CONSTANTS' | 'REGULAR';
export type RegularStruct = Readonly<{
context: 'REGULAR',
name: string,
properties: ReadonlyArray<StructProperty>,
}>;
export type ConstantsStruct = Readonly<{
context: 'CONSTANTS',
name: string,
properties: ReadonlyArray<StructProperty>,
}>;
export type Struct = RegularStruct | ConstantsStruct;
export type StructProperty = Readonly<{
name: string,
optional: boolean,
typeAnnotation: Nullable<StructTypeAnnotation>,
}>;
export type StructTypeAnnotation =
| StringTypeAnnotation
| StringLiteralTypeAnnotation
| NativeModuleUnionTypeAnnotation
| NativeModuleNumberTypeAnnotation
| NumberLiteralTypeAnnotation
| BooleanLiteralTypeAnnotation
| Int32TypeAnnotation
| BigIntTypeAnnotation
| DoubleTypeAnnotation
| FloatTypeAnnotation
| BooleanTypeAnnotation
| NativeModuleEnumDeclaration
| NativeModuleGenericObjectTypeAnnotation
| ReservedTypeAnnotation
| NativeModuleTypeAliasTypeAnnotation
| NativeModuleArrayTypeAnnotation<Nullable<StructTypeAnnotation>>;
class StructCollector {
_structs: Map<string, Struct> = new Map();
process(
structName: string,
structContext: StructContext,
resolveAlias: AliasResolver,
nullableTypeAnnotation: Nullable<NativeModuleBaseTypeAnnotation>,
): Nullable<StructTypeAnnotation> {
const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
switch (typeAnnotation.type) {
case 'ObjectTypeAnnotation': {
this._insertStruct(
structName,
structContext,
resolveAlias,
typeAnnotation,
);
return wrapNullable(nullable, {
type: 'TypeAliasTypeAnnotation',
name: structName,
});
}
case 'ArrayTypeAnnotation': {
if (typeAnnotation.elementType.type === 'AnyTypeAnnotation') {
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
elementType: {
type: 'AnyTypeAnnotation',
},
});
}
return wrapNullable(nullable, {
type: 'ArrayTypeAnnotation',
elementType: this.process(
structName + 'Element',
structContext,
resolveAlias,
typeAnnotation.elementType,
),
});
}
case 'TypeAliasTypeAnnotation': {
this._insertAlias(typeAnnotation.name, structContext, resolveAlias);
return wrapNullable(nullable, typeAnnotation);
}
case 'EnumDeclaration':
return wrapNullable(nullable, typeAnnotation);
case 'MixedTypeAnnotation':
throw new Error('Mixed types are unsupported in structs');
case 'UnionTypeAnnotation':
try {
const validUnionType = parseValidUnionType(typeAnnotation);
switch (validUnionType) {
case 'boolean':
return wrapNullable(nullable, {
type: 'BooleanTypeAnnotation',
});
case 'number':
return wrapNullable(nullable, {
type: 'NumberTypeAnnotation',
});
case 'object':
// This isn't smart enough to actually know how to generate the
// options on the native side. So we just treat it as an unknown object type
return wrapNullable(nullable, {
type: 'GenericObjectTypeAnnotation',
});
case 'string':
return wrapNullable(nullable, {
type: 'StringTypeAnnotation',
});
default:
(validUnionType: empty);
throw new Error(`Unsupported union member types`);
}
} catch (ex) {
// TODO(T247151345): Implement proper heterogeneous union support.
if (ex instanceof HeterogeneousUnionError) {
return wrapNullable(nullable, typeAnnotation);
}
throw ex;
}
default: {
return wrapNullable(nullable, typeAnnotation);
}
}
}
_insertAlias(
aliasName: string,
structContext: StructContext,
resolveAlias: AliasResolver,
): void {
const usedStruct = this._structs.get(aliasName);
if (usedStruct == null) {
this._insertStruct(
aliasName,
structContext,
resolveAlias,
resolveAlias(aliasName),
);
} else if (usedStruct.context !== structContext) {
throw new Error(
`Tried to use alias '${aliasName}' in a getConstants() return type and inside a regular struct.`,
);
}
}
_insertStruct(
structName: string,
structContext: StructContext,
resolveAlias: AliasResolver,
objectTypeAnnotation: NativeModuleObjectTypeAnnotation,
): void {
// $FlowFixMe[missing-type-arg]
const properties = objectTypeAnnotation.properties.map<
Readonly<{
name: string,
optional: boolean,
typeAnnotation: Nullable<StructTypeAnnotation>,
}>,
>(property => {
const propertyStructName = structName + capitalize(property.name);
return {
...property,
typeAnnotation: this.process(
propertyStructName,
structContext,
resolveAlias,
property.typeAnnotation,
),
};
});
switch (structContext) {
case 'REGULAR':
this._structs.set(structName, {
name: structName,
context: 'REGULAR',
properties: properties,
});
break;
case 'CONSTANTS':
this._structs.set(structName, {
name: structName,
context: 'CONSTANTS',
properties: properties,
});
break;
default:
(structContext: empty);
throw new Error(`Detected an invalid struct context: ${structContext}`);
}
}
getAllStructs(): ReadonlyArray<Struct> {
return [...this._structs.values()];
}
getStruct(name: string): ?Struct {
return this._structs.get(name);
}
}
module.exports = {
StructCollector,
};