@react-native/codegen 0.72.1 → 0.72.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/CodegenSchema.d.ts +348 -0
- package/lib/CodegenSchema.js.flow +31 -3
- package/lib/SchemaValidator.d.ts +11 -0
- package/lib/cli/combine/combine-js-to-schema.js +8 -8
- package/lib/cli/combine/combine-js-to-schema.js.flow +8 -7
- package/lib/cli/parser/parser.js +8 -15
- package/lib/cli/parser/parser.js.flow +8 -14
- package/lib/generators/Utils.js +16 -0
- package/lib/generators/Utils.js.flow +20 -0
- package/lib/generators/__test_fixtures__/fixtures.js +2 -1
- package/lib/generators/__test_fixtures__/fixtures.js.flow +2 -1
- package/lib/generators/components/ComponentsGeneratorUtils.js +10 -1
- package/lib/generators/components/ComponentsGeneratorUtils.js.flow +12 -2
- package/lib/generators/components/CppHelpers.js +8 -15
- package/lib/generators/components/CppHelpers.js.flow +8 -19
- package/lib/generators/components/GenerateEventEmitterCpp.js +7 -3
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +7 -3
- package/lib/generators/components/GenerateEventEmitterH.js +2 -2
- package/lib/generators/components/GenerateEventEmitterH.js.flow +1 -2
- package/lib/generators/components/GeneratePropsH.js +3 -4
- package/lib/generators/components/GeneratePropsH.js.flow +2 -4
- package/lib/generators/components/GeneratePropsJavaDelegate.js +2 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +2 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js +3 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js.flow +3 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +8 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +12 -0
- package/lib/generators/components/GenerateTests.js +3 -2
- package/lib/generators/components/GenerateTests.js.flow +3 -1
- package/lib/generators/components/GenerateViewConfigJs.js +5 -2
- package/lib/generators/components/GenerateViewConfigJs.js.flow +5 -2
- package/lib/generators/components/JavaHelpers.js +9 -0
- package/lib/generators/components/JavaHelpers.js.flow +12 -1
- package/lib/generators/components/__test_fixtures__/fixtures.js +41 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +42 -0
- package/lib/generators/modules/GenerateModuleCpp.js +9 -4
- package/lib/generators/modules/GenerateModuleCpp.js.flow +11 -3
- package/lib/generators/modules/GenerateModuleH.js +185 -33
- package/lib/generators/modules/GenerateModuleH.js.flow +203 -25
- package/lib/generators/modules/GenerateModuleJavaSpec.js +2 -2
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +5 -5
- package/lib/generators/modules/GenerateModuleJniCpp.js +2 -2
- package/lib/generators/modules/GenerateModuleJniCpp.js.flow +5 -5
- package/lib/generators/modules/GenerateModuleObjCpp/index.js +2 -2
- package/lib/generators/modules/GenerateModuleObjCpp/index.js.flow +2 -2
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +2 -2
- package/lib/generators/modules/Utils.js +4 -0
- package/lib/generators/modules/Utils.js.flow +6 -0
- package/lib/generators/modules/__test_fixtures__/fixtures.js +152 -10
- package/lib/generators/modules/__test_fixtures__/fixtures.js.flow +152 -10
- package/lib/parsers/error-utils.js +42 -19
- package/lib/parsers/error-utils.js.flow +46 -19
- package/lib/parsers/errors.d.ts +10 -0
- package/lib/parsers/errors.js +9 -29
- package/lib/parsers/errors.js.flow +2 -20
- package/lib/parsers/flow/Visitor.js +37 -0
- package/lib/parsers/flow/Visitor.js.flow +41 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +20 -2
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +20 -2
- package/lib/parsers/flow/components/commands.js +3 -0
- package/lib/parsers/flow/components/commands.js.flow +2 -1
- package/lib/parsers/flow/components/componentsUtils.js +10 -0
- package/lib/parsers/flow/components/componentsUtils.js.flow +11 -1
- package/lib/parsers/flow/components/events.js.flow +1 -1
- package/lib/parsers/flow/components/extends.js.flow +1 -1
- package/lib/parsers/flow/components/index.js +16 -20
- package/lib/parsers/flow/components/index.js.flow +8 -7
- package/lib/parsers/flow/components/options.js.flow +1 -1
- package/lib/parsers/flow/components/props.js.flow +1 -1
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js +54 -0
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js.flow +56 -0
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +64 -1
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +66 -1
- package/lib/parsers/flow/modules/index.js +112 -48
- package/lib/parsers/flow/modules/index.js.flow +115 -44
- package/lib/parsers/flow/parser.d.ts +10 -0
- package/lib/parsers/flow/parser.js +173 -32
- package/lib/parsers/flow/parser.js.flow +201 -24
- package/lib/parsers/flow/utils.js +33 -57
- package/lib/parsers/flow/utils.js.flow +37 -60
- package/lib/parsers/parser.d.ts +17 -0
- package/lib/parsers/parser.js.flow +124 -17
- package/lib/parsers/parserMock.js +101 -30
- package/lib/parsers/parserMock.js.flow +139 -24
- package/lib/parsers/parsers-commons.js +121 -119
- package/lib/parsers/parsers-commons.js.flow +136 -131
- package/lib/parsers/parsers-primitives.js +75 -12
- package/lib/parsers/parsers-primitives.js.flow +92 -13
- package/lib/parsers/schema/index.d.ts +10 -0
- package/lib/parsers/{typescript/components/schema.js.flow → schema.js.flow} +1 -1
- package/lib/parsers/typescript/Visitor.js +42 -0
- package/lib/parsers/typescript/Visitor.js.flow +47 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +22 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +22 -0
- package/lib/parsers/typescript/components/commands.js +3 -0
- package/lib/parsers/typescript/components/commands.js.flow +2 -1
- package/lib/parsers/typescript/components/componentsUtils.js +5 -0
- package/lib/parsers/typescript/components/componentsUtils.js.flow +6 -1
- package/lib/parsers/typescript/components/events.js.flow +1 -1
- package/lib/parsers/typescript/components/extends.js.flow +1 -1
- package/lib/parsers/typescript/components/index.js +15 -19
- package/lib/parsers/typescript/components/index.js.flow +8 -7
- package/lib/parsers/typescript/components/options.js.flow +1 -1
- package/lib/parsers/typescript/components/props.js.flow +1 -1
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js +52 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js.flow +54 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js +142 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js.flow +146 -0
- package/lib/parsers/typescript/modules/index.js +276 -110
- package/lib/parsers/typescript/modules/index.js.flow +237 -68
- package/lib/parsers/typescript/parser.d.ts +10 -0
- package/lib/parsers/typescript/parser.js +175 -25
- package/lib/parsers/typescript/parser.js.flow +184 -26
- package/lib/parsers/typescript/utils.js +40 -51
- package/lib/parsers/typescript/utils.js.flow +44 -55
- package/lib/parsers/utils.js +0 -125
- package/lib/parsers/utils.js.flow +3 -70
- package/package.json +4 -1
- package/lib/parsers/flow/components/schema.js +0 -106
- package/lib/parsers/flow/components/schema.js.flow +0 -62
- package/lib/parsers/flow/index.js +0 -80
- package/lib/parsers/flow/index.js.flow +0 -85
- package/lib/parsers/typescript/index.js +0 -85
- package/lib/parsers/typescript/index.js.flow +0 -94
- /package/lib/parsers/{typescript/components/schema.js → schema.js} +0 -0
|
@@ -13,22 +13,24 @@
|
|
|
13
13
|
import type {
|
|
14
14
|
NamedShape,
|
|
15
15
|
NativeModuleAliasMap,
|
|
16
|
+
NativeModuleEnumMap,
|
|
16
17
|
NativeModuleBaseTypeAnnotation,
|
|
17
18
|
NativeModuleTypeAnnotation,
|
|
18
19
|
NativeModulePropertyShape,
|
|
19
20
|
NativeModuleSchema,
|
|
20
21
|
Nullable,
|
|
21
22
|
} from '../../../CodegenSchema';
|
|
23
|
+
|
|
24
|
+
import type {Parser} from '../../parser';
|
|
22
25
|
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
|
|
23
26
|
|
|
24
27
|
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
|
|
25
|
-
const {resolveTypeAnnotation
|
|
28
|
+
const {resolveTypeAnnotation} = require('../utils');
|
|
26
29
|
const {
|
|
27
30
|
unwrapNullable,
|
|
28
31
|
wrapNullable,
|
|
29
32
|
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
|
30
33
|
parseObjectProperty,
|
|
31
|
-
translateDefault,
|
|
32
34
|
buildPropertySchema,
|
|
33
35
|
} = require('../../parsers-commons');
|
|
34
36
|
const {
|
|
@@ -39,6 +41,7 @@ const {
|
|
|
39
41
|
emitFunction,
|
|
40
42
|
emitNumber,
|
|
41
43
|
emitInt32,
|
|
44
|
+
emitGenericObject,
|
|
42
45
|
emitObject,
|
|
43
46
|
emitPromise,
|
|
44
47
|
emitRootTag,
|
|
@@ -48,12 +51,12 @@ const {
|
|
|
48
51
|
emitMixed,
|
|
49
52
|
emitUnion,
|
|
50
53
|
typeAliasResolution,
|
|
51
|
-
|
|
54
|
+
typeEnumResolution,
|
|
52
55
|
} = require('../../parsers-primitives');
|
|
53
56
|
|
|
54
57
|
const {
|
|
55
58
|
UnsupportedTypeAnnotationParserError,
|
|
56
|
-
|
|
59
|
+
UnsupportedGenericParserError,
|
|
57
60
|
} = require('../../errors');
|
|
58
61
|
|
|
59
62
|
const {
|
|
@@ -63,14 +66,14 @@ const {
|
|
|
63
66
|
throwIfWrongNumberOfCallExpressionArgs,
|
|
64
67
|
throwIfMoreThanOneModuleRegistryCalls,
|
|
65
68
|
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
|
|
69
|
+
throwIfIncorrectModuleRegistryCallArgument,
|
|
66
70
|
throwIfUntypedModule,
|
|
67
71
|
throwIfMoreThanOneModuleInterfaceParserError,
|
|
72
|
+
throwIfPartialNotAnnotatingTypeParameter,
|
|
73
|
+
throwIfPartialWithMoreParameter,
|
|
68
74
|
} = require('../../error-utils');
|
|
69
75
|
|
|
70
|
-
const {FlowParser} = require('../parser');
|
|
71
|
-
|
|
72
76
|
const language = 'Flow';
|
|
73
|
-
const parser = new FlowParser();
|
|
74
77
|
|
|
75
78
|
function translateTypeAnnotation(
|
|
76
79
|
hasteModuleName: string,
|
|
@@ -80,10 +83,12 @@ function translateTypeAnnotation(
|
|
|
80
83
|
flowTypeAnnotation: $FlowFixMe,
|
|
81
84
|
types: TypeDeclarationMap,
|
|
82
85
|
aliasMap: {...NativeModuleAliasMap},
|
|
86
|
+
enumMap: {...NativeModuleEnumMap},
|
|
83
87
|
tryParse: ParserErrorCapturer,
|
|
84
88
|
cxxOnly: boolean,
|
|
89
|
+
parser: Parser,
|
|
85
90
|
): Nullable<NativeModuleTypeAnnotation> {
|
|
86
|
-
const {nullable, typeAnnotation,
|
|
91
|
+
const {nullable, typeAnnotation, typeResolutionStatus} =
|
|
87
92
|
resolveTypeAnnotation(flowTypeAnnotation, types);
|
|
88
93
|
|
|
89
94
|
switch (typeAnnotation.type) {
|
|
@@ -100,6 +105,7 @@ function translateTypeAnnotation(
|
|
|
100
105
|
nullable,
|
|
101
106
|
types,
|
|
102
107
|
aliasMap,
|
|
108
|
+
enumMap,
|
|
103
109
|
tryParse,
|
|
104
110
|
cxxOnly,
|
|
105
111
|
translateTypeAnnotation,
|
|
@@ -113,6 +119,7 @@ function translateTypeAnnotation(
|
|
|
113
119
|
parser,
|
|
114
120
|
types,
|
|
115
121
|
aliasMap,
|
|
122
|
+
enumMap,
|
|
116
123
|
cxxOnly,
|
|
117
124
|
nullable,
|
|
118
125
|
translateTypeAnnotation,
|
|
@@ -131,8 +138,10 @@ function translateTypeAnnotation(
|
|
|
131
138
|
typeAnnotation.typeParameters.params[0],
|
|
132
139
|
types,
|
|
133
140
|
aliasMap,
|
|
141
|
+
enumMap,
|
|
134
142
|
tryParse,
|
|
135
143
|
cxxOnly,
|
|
144
|
+
parser,
|
|
136
145
|
),
|
|
137
146
|
);
|
|
138
147
|
|
|
@@ -152,20 +161,75 @@ function translateTypeAnnotation(
|
|
|
152
161
|
}
|
|
153
162
|
case 'UnsafeObject':
|
|
154
163
|
case 'Object': {
|
|
155
|
-
return
|
|
164
|
+
return emitGenericObject(nullable);
|
|
165
|
+
}
|
|
166
|
+
case '$Partial': {
|
|
167
|
+
throwIfPartialWithMoreParameter(typeAnnotation);
|
|
168
|
+
|
|
169
|
+
const annotatedElement = parser.extractAnnotatedElement(
|
|
170
|
+
typeAnnotation,
|
|
171
|
+
types,
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
throwIfPartialNotAnnotatingTypeParameter(
|
|
175
|
+
typeAnnotation,
|
|
176
|
+
types,
|
|
177
|
+
parser,
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
const properties = annotatedElement.right.properties.map(prop => {
|
|
181
|
+
return {
|
|
182
|
+
name: prop.key.name,
|
|
183
|
+
optional: true,
|
|
184
|
+
typeAnnotation: translateTypeAnnotation(
|
|
185
|
+
hasteModuleName,
|
|
186
|
+
prop.value,
|
|
187
|
+
types,
|
|
188
|
+
aliasMap,
|
|
189
|
+
enumMap,
|
|
190
|
+
tryParse,
|
|
191
|
+
cxxOnly,
|
|
192
|
+
parser,
|
|
193
|
+
),
|
|
194
|
+
};
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return emitObject(nullable, properties);
|
|
156
198
|
}
|
|
157
199
|
default: {
|
|
158
|
-
|
|
200
|
+
throw new UnsupportedGenericParserError(
|
|
159
201
|
hasteModuleName,
|
|
160
202
|
typeAnnotation,
|
|
161
|
-
types,
|
|
162
|
-
nullable,
|
|
163
203
|
parser,
|
|
164
204
|
);
|
|
165
205
|
}
|
|
166
206
|
}
|
|
167
207
|
}
|
|
168
208
|
case 'ObjectTypeAnnotation': {
|
|
209
|
+
// if there is any indexer, then it is a dictionary
|
|
210
|
+
if (typeAnnotation.indexers) {
|
|
211
|
+
const indexers = typeAnnotation.indexers.filter(
|
|
212
|
+
member => member.type === 'ObjectTypeIndexer',
|
|
213
|
+
);
|
|
214
|
+
if (indexers.length > 0) {
|
|
215
|
+
// check the property type to prevent developers from using unsupported types
|
|
216
|
+
// the return value from `translateTypeAnnotation` is unused
|
|
217
|
+
const propertyType = indexers[0].value;
|
|
218
|
+
translateTypeAnnotation(
|
|
219
|
+
hasteModuleName,
|
|
220
|
+
propertyType,
|
|
221
|
+
types,
|
|
222
|
+
aliasMap,
|
|
223
|
+
enumMap,
|
|
224
|
+
tryParse,
|
|
225
|
+
cxxOnly,
|
|
226
|
+
parser,
|
|
227
|
+
);
|
|
228
|
+
// no need to do further checking
|
|
229
|
+
return emitGenericObject(nullable);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
169
233
|
const objectTypeAnnotation = {
|
|
170
234
|
type: 'ObjectTypeAnnotation',
|
|
171
235
|
// $FlowFixMe[missing-type-arg]
|
|
@@ -181,6 +245,7 @@ function translateTypeAnnotation(
|
|
|
181
245
|
hasteModuleName,
|
|
182
246
|
types,
|
|
183
247
|
aliasMap,
|
|
248
|
+
enumMap,
|
|
184
249
|
tryParse,
|
|
185
250
|
cxxOnly,
|
|
186
251
|
nullable,
|
|
@@ -194,7 +259,7 @@ function translateTypeAnnotation(
|
|
|
194
259
|
};
|
|
195
260
|
|
|
196
261
|
return typeAliasResolution(
|
|
197
|
-
|
|
262
|
+
typeResolutionStatus,
|
|
198
263
|
objectTypeAnnotation,
|
|
199
264
|
aliasMap,
|
|
200
265
|
nullable,
|
|
@@ -219,10 +284,11 @@ function translateTypeAnnotation(
|
|
|
219
284
|
typeAnnotation,
|
|
220
285
|
types,
|
|
221
286
|
aliasMap,
|
|
287
|
+
enumMap,
|
|
222
288
|
tryParse,
|
|
223
289
|
cxxOnly,
|
|
224
290
|
translateTypeAnnotation,
|
|
225
|
-
|
|
291
|
+
parser,
|
|
226
292
|
);
|
|
227
293
|
}
|
|
228
294
|
case 'UnionTypeAnnotation': {
|
|
@@ -238,8 +304,21 @@ function translateTypeAnnotation(
|
|
|
238
304
|
case 'MixedTypeAnnotation': {
|
|
239
305
|
if (cxxOnly) {
|
|
240
306
|
return emitMixed(nullable);
|
|
307
|
+
} else {
|
|
308
|
+
return emitGenericObject(nullable);
|
|
241
309
|
}
|
|
242
|
-
|
|
310
|
+
}
|
|
311
|
+
case 'EnumStringBody':
|
|
312
|
+
case 'EnumNumberBody': {
|
|
313
|
+
return typeEnumResolution(
|
|
314
|
+
typeAnnotation,
|
|
315
|
+
typeResolutionStatus,
|
|
316
|
+
nullable,
|
|
317
|
+
hasteModuleName,
|
|
318
|
+
language,
|
|
319
|
+
enumMap,
|
|
320
|
+
parser,
|
|
321
|
+
);
|
|
243
322
|
}
|
|
244
323
|
default: {
|
|
245
324
|
throw new UnsupportedTypeAnnotationParserError(
|
|
@@ -251,15 +330,6 @@ function translateTypeAnnotation(
|
|
|
251
330
|
}
|
|
252
331
|
}
|
|
253
332
|
|
|
254
|
-
function isModuleInterface(node: $FlowFixMe) {
|
|
255
|
-
return (
|
|
256
|
-
node.type === 'InterfaceDeclaration' &&
|
|
257
|
-
node.extends.length === 1 &&
|
|
258
|
-
node.extends[0].type === 'InterfaceExtends' &&
|
|
259
|
-
node.extends[0].id.name === 'TurboModule'
|
|
260
|
-
);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
333
|
function buildModuleSchema(
|
|
264
334
|
hasteModuleName: string,
|
|
265
335
|
/**
|
|
@@ -267,10 +337,11 @@ function buildModuleSchema(
|
|
|
267
337
|
*/
|
|
268
338
|
ast: $FlowFixMe,
|
|
269
339
|
tryParse: ParserErrorCapturer,
|
|
340
|
+
parser: Parser,
|
|
270
341
|
): NativeModuleSchema {
|
|
271
|
-
const types = getTypes(ast);
|
|
342
|
+
const types = parser.getTypes(ast);
|
|
272
343
|
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
|
|
273
|
-
isModuleInterface,
|
|
344
|
+
t => parser.isModuleInterface(t),
|
|
274
345
|
);
|
|
275
346
|
|
|
276
347
|
throwIfModuleInterfaceNotFound(
|
|
@@ -305,14 +376,12 @@ function buildModuleSchema(
|
|
|
305
376
|
hasteModuleName,
|
|
306
377
|
moduleSpec,
|
|
307
378
|
callExpressions,
|
|
308
|
-
language,
|
|
309
379
|
);
|
|
310
380
|
|
|
311
381
|
throwIfMoreThanOneModuleRegistryCalls(
|
|
312
382
|
hasteModuleName,
|
|
313
383
|
callExpressions,
|
|
314
384
|
callExpressions.length,
|
|
315
|
-
language,
|
|
316
385
|
);
|
|
317
386
|
|
|
318
387
|
const [callExpression] = callExpressions;
|
|
@@ -324,19 +393,13 @@ function buildModuleSchema(
|
|
|
324
393
|
callExpression,
|
|
325
394
|
methodName,
|
|
326
395
|
callExpression.arguments.length,
|
|
327
|
-
language,
|
|
328
396
|
);
|
|
329
397
|
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
methodName,
|
|
336
|
-
type,
|
|
337
|
-
language,
|
|
338
|
-
);
|
|
339
|
-
}
|
|
398
|
+
throwIfIncorrectModuleRegistryCallArgument(
|
|
399
|
+
hasteModuleName,
|
|
400
|
+
callExpression.arguments[0],
|
|
401
|
+
methodName,
|
|
402
|
+
);
|
|
340
403
|
|
|
341
404
|
const $moduleName = callExpression.arguments[0].value;
|
|
342
405
|
|
|
@@ -346,7 +409,6 @@ function buildModuleSchema(
|
|
|
346
409
|
callExpression,
|
|
347
410
|
methodName,
|
|
348
411
|
$moduleName,
|
|
349
|
-
language,
|
|
350
412
|
);
|
|
351
413
|
|
|
352
414
|
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
|
|
@@ -374,29 +436,37 @@ function buildModuleSchema(
|
|
|
374
436
|
.filter(property => property.type === 'ObjectTypeProperty')
|
|
375
437
|
.map<?{
|
|
376
438
|
aliasMap: NativeModuleAliasMap,
|
|
439
|
+
enumMap: NativeModuleEnumMap,
|
|
377
440
|
propertyShape: NativeModulePropertyShape,
|
|
378
441
|
}>(property => {
|
|
379
442
|
const aliasMap: {...NativeModuleAliasMap} = {};
|
|
443
|
+
const enumMap: {...NativeModuleEnumMap} = {};
|
|
380
444
|
return tryParse(() => ({
|
|
381
445
|
aliasMap: aliasMap,
|
|
446
|
+
enumMap: enumMap,
|
|
382
447
|
propertyShape: buildPropertySchema(
|
|
383
448
|
hasteModuleName,
|
|
384
449
|
property,
|
|
385
450
|
types,
|
|
386
451
|
aliasMap,
|
|
452
|
+
enumMap,
|
|
387
453
|
tryParse,
|
|
388
454
|
cxxOnly,
|
|
389
|
-
language,
|
|
390
455
|
resolveTypeAnnotation,
|
|
391
456
|
translateTypeAnnotation,
|
|
457
|
+
parser,
|
|
392
458
|
),
|
|
393
459
|
}));
|
|
394
460
|
})
|
|
395
461
|
.filter(Boolean)
|
|
396
462
|
.reduce(
|
|
397
|
-
(
|
|
463
|
+
(
|
|
464
|
+
moduleSchema: NativeModuleSchema,
|
|
465
|
+
{aliasMap, enumMap, propertyShape},
|
|
466
|
+
) => ({
|
|
398
467
|
type: 'NativeModule',
|
|
399
|
-
|
|
468
|
+
aliasMap: {...moduleSchema.aliasMap, ...aliasMap},
|
|
469
|
+
enumMap: {...moduleSchema.enumMap, ...enumMap},
|
|
400
470
|
spec: {
|
|
401
471
|
properties: [...moduleSchema.spec.properties, propertyShape],
|
|
402
472
|
},
|
|
@@ -405,7 +475,8 @@ function buildModuleSchema(
|
|
|
405
475
|
}),
|
|
406
476
|
{
|
|
407
477
|
type: 'NativeModule',
|
|
408
|
-
|
|
478
|
+
aliasMap: {},
|
|
479
|
+
enumMap: {},
|
|
409
480
|
spec: {properties: []},
|
|
410
481
|
moduleName,
|
|
411
482
|
excludedPlatforms:
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {Parser} from '../parser';
|
|
9
|
+
|
|
10
|
+
export declare class FlowParser implements Parser{}
|
|
@@ -38,9 +38,22 @@ function _toPrimitive(input, hint) {
|
|
|
38
38
|
}
|
|
39
39
|
return (hint === 'string' ? String : Number)(input);
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
|
|
42
|
+
const flowParser = require('flow-parser');
|
|
43
|
+
const _require = require('../parsers-commons'),
|
|
44
|
+
buildSchema = _require.buildSchema;
|
|
45
|
+
const _require2 = require('./Visitor'),
|
|
46
|
+
Visitor = _require2.Visitor;
|
|
47
|
+
const _require3 = require('./components'),
|
|
48
|
+
buildComponentSchema = _require3.buildComponentSchema;
|
|
49
|
+
const _require4 = require('../schema.js'),
|
|
50
|
+
wrapComponentSchema = _require4.wrapComponentSchema;
|
|
51
|
+
const _require5 = require('./modules'),
|
|
52
|
+
buildModuleSchema = _require5.buildModuleSchema;
|
|
53
|
+
const fs = require('fs');
|
|
54
|
+
const _require6 = require('../errors'),
|
|
42
55
|
UnsupportedObjectPropertyTypeAnnotationParserError =
|
|
43
|
-
|
|
56
|
+
_require6.UnsupportedObjectPropertyTypeAnnotationParserError;
|
|
44
57
|
class FlowParser {
|
|
45
58
|
constructor() {
|
|
46
59
|
_defineProperty(
|
|
@@ -49,37 +62,19 @@ class FlowParser {
|
|
|
49
62
|
'TypeParameterInstantiation',
|
|
50
63
|
);
|
|
51
64
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
switch (propertyOrIndex.type) {
|
|
55
|
-
case 'ObjectTypeProperty':
|
|
56
|
-
return propertyOrIndex.key.name;
|
|
57
|
-
case 'ObjectTypeIndexer':
|
|
58
|
-
// flow index name is optional
|
|
59
|
-
return (_propertyOrIndex$id$n =
|
|
60
|
-
(_propertyOrIndex$id = propertyOrIndex.id) === null ||
|
|
61
|
-
_propertyOrIndex$id === void 0
|
|
62
|
-
? void 0
|
|
63
|
-
: _propertyOrIndex$id.name) !== null &&
|
|
64
|
-
_propertyOrIndex$id$n !== void 0
|
|
65
|
-
? _propertyOrIndex$id$n
|
|
66
|
-
: 'key';
|
|
67
|
-
default:
|
|
68
|
-
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
|
|
69
|
-
hasteModuleName,
|
|
70
|
-
propertyOrIndex,
|
|
71
|
-
propertyOrIndex.type,
|
|
72
|
-
this.language(),
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
getMaybeEnumMemberType(maybeEnumDeclaration) {
|
|
77
|
-
return maybeEnumDeclaration.body.type
|
|
78
|
-
.replace('EnumNumberBody', 'NumberTypeAnnotation')
|
|
79
|
-
.replace('EnumStringBody', 'StringTypeAnnotation');
|
|
65
|
+
isProperty(property) {
|
|
66
|
+
return property.type === 'ObjectTypeProperty';
|
|
80
67
|
}
|
|
81
|
-
|
|
82
|
-
|
|
68
|
+
getKeyName(property, hasteModuleName) {
|
|
69
|
+
if (!this.isProperty(property)) {
|
|
70
|
+
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
|
|
71
|
+
hasteModuleName,
|
|
72
|
+
property,
|
|
73
|
+
property.type,
|
|
74
|
+
this.language(),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return property.key.name;
|
|
83
78
|
}
|
|
84
79
|
language() {
|
|
85
80
|
return 'Flow';
|
|
@@ -103,6 +98,152 @@ class FlowParser {
|
|
|
103
98
|
};
|
|
104
99
|
return [...new Set(membersTypes.map(remapLiteral))];
|
|
105
100
|
}
|
|
101
|
+
parseFile(filename) {
|
|
102
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
103
|
+
return this.parseString(contents, filename);
|
|
104
|
+
}
|
|
105
|
+
parseString(contents, filename) {
|
|
106
|
+
return buildSchema(
|
|
107
|
+
contents,
|
|
108
|
+
filename,
|
|
109
|
+
wrapComponentSchema,
|
|
110
|
+
buildComponentSchema,
|
|
111
|
+
buildModuleSchema,
|
|
112
|
+
Visitor,
|
|
113
|
+
this,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
parseModuleFixture(filename) {
|
|
117
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
118
|
+
return this.parseString(contents, 'path/NativeSampleTurboModule.js');
|
|
119
|
+
}
|
|
120
|
+
getAst(contents) {
|
|
121
|
+
return flowParser.parse(contents, {
|
|
122
|
+
enums: true,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
getFunctionTypeAnnotationParameters(functionTypeAnnotation) {
|
|
126
|
+
return functionTypeAnnotation.params;
|
|
127
|
+
}
|
|
128
|
+
getFunctionNameFromParameter(parameter) {
|
|
129
|
+
return parameter.name;
|
|
130
|
+
}
|
|
131
|
+
getParameterName(parameter) {
|
|
132
|
+
return parameter.name.name;
|
|
133
|
+
}
|
|
134
|
+
getParameterTypeAnnotation(parameter) {
|
|
135
|
+
return parameter.typeAnnotation;
|
|
136
|
+
}
|
|
137
|
+
getFunctionTypeAnnotationReturnType(functionTypeAnnotation) {
|
|
138
|
+
return functionTypeAnnotation.returnType;
|
|
139
|
+
}
|
|
140
|
+
parseEnumMembersType(typeAnnotation) {
|
|
141
|
+
const enumMembersType =
|
|
142
|
+
typeAnnotation.type === 'EnumStringBody'
|
|
143
|
+
? 'StringTypeAnnotation'
|
|
144
|
+
: typeAnnotation.type === 'EnumNumberBody'
|
|
145
|
+
? 'NumberTypeAnnotation'
|
|
146
|
+
: null;
|
|
147
|
+
if (!enumMembersType) {
|
|
148
|
+
throw new Error(
|
|
149
|
+
`Unknown enum type annotation type. Got: ${typeAnnotation.type}. Expected: EnumStringBody or EnumNumberBody.`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return enumMembersType;
|
|
153
|
+
}
|
|
154
|
+
validateEnumMembersSupported(typeAnnotation, enumMembersType) {
|
|
155
|
+
if (!typeAnnotation.members || typeAnnotation.members.length === 0) {
|
|
156
|
+
// passing mixed members to flow would result in a flow error
|
|
157
|
+
// if the tool is launched ignoring that error, the enum would appear like not having enums
|
|
158
|
+
throw new Error(
|
|
159
|
+
'Enums should have at least one member and member values can not be mixed- they all must be either blank, number, or string values.',
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
typeAnnotation.members.forEach(member => {
|
|
163
|
+
if (
|
|
164
|
+
enumMembersType === 'StringTypeAnnotation' &&
|
|
165
|
+
(!member.init || typeof member.init.value === 'string')
|
|
166
|
+
) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (
|
|
170
|
+
enumMembersType === 'NumberTypeAnnotation' &&
|
|
171
|
+
member.init &&
|
|
172
|
+
typeof member.init.value === 'number'
|
|
173
|
+
) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
throw new Error(
|
|
177
|
+
'Enums can not be mixed- they all must be either blank, number, or string values.',
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
parseEnumMembers(typeAnnotation) {
|
|
182
|
+
return typeAnnotation.members.map(member => {
|
|
183
|
+
var _member$init$value, _member$init;
|
|
184
|
+
return {
|
|
185
|
+
name: member.id.name,
|
|
186
|
+
value:
|
|
187
|
+
(_member$init$value =
|
|
188
|
+
(_member$init = member.init) === null || _member$init === void 0
|
|
189
|
+
? void 0
|
|
190
|
+
: _member$init.value) !== null && _member$init$value !== void 0
|
|
191
|
+
? _member$init$value
|
|
192
|
+
: member.id.name,
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
isModuleInterface(node) {
|
|
197
|
+
return (
|
|
198
|
+
node.type === 'InterfaceDeclaration' &&
|
|
199
|
+
node.extends.length === 1 &&
|
|
200
|
+
node.extends[0].type === 'InterfaceExtends' &&
|
|
201
|
+
node.extends[0].id.name === 'TurboModule'
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
extractAnnotatedElement(typeAnnotation, types) {
|
|
205
|
+
return types[typeAnnotation.typeParameters.params[0].id.name];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias
|
|
210
|
+
* declaration type. Unfortunately, we don't have those types, because flow-parser
|
|
211
|
+
* generates them, and flow-parser is not type-safe. In the future, we should find
|
|
212
|
+
* a way to get these types from our flow parser library.
|
|
213
|
+
*
|
|
214
|
+
* TODO(T71778680): Flow type AST Nodes
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
getTypes(ast) {
|
|
218
|
+
return ast.body.reduce((types, node) => {
|
|
219
|
+
if (
|
|
220
|
+
node.type === 'ExportNamedDeclaration' &&
|
|
221
|
+
node.exportKind === 'type'
|
|
222
|
+
) {
|
|
223
|
+
if (
|
|
224
|
+
node.declaration != null &&
|
|
225
|
+
(node.declaration.type === 'TypeAlias' ||
|
|
226
|
+
node.declaration.type === 'InterfaceDeclaration')
|
|
227
|
+
) {
|
|
228
|
+
types[node.declaration.id.name] = node.declaration;
|
|
229
|
+
}
|
|
230
|
+
} else if (
|
|
231
|
+
node.type === 'ExportNamedDeclaration' &&
|
|
232
|
+
node.exportKind === 'value' &&
|
|
233
|
+
node.declaration &&
|
|
234
|
+
node.declaration.type === 'EnumDeclaration'
|
|
235
|
+
) {
|
|
236
|
+
types[node.declaration.id.name] = node.declaration;
|
|
237
|
+
} else if (
|
|
238
|
+
node.type === 'TypeAlias' ||
|
|
239
|
+
node.type === 'InterfaceDeclaration' ||
|
|
240
|
+
node.type === 'EnumDeclaration'
|
|
241
|
+
) {
|
|
242
|
+
types[node.id.name] = node;
|
|
243
|
+
}
|
|
244
|
+
return types;
|
|
245
|
+
}, {});
|
|
246
|
+
}
|
|
106
247
|
}
|
|
107
248
|
module.exports = {
|
|
108
249
|
FlowParser,
|