@react-native/codegen 0.72.5 → 0.73.0-nightly-20230603-fd9e295be
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/README.md +2 -2
- package/lib/CodegenSchema.d.ts +15 -4
- package/lib/CodegenSchema.js.flow +33 -21
- package/lib/generators/components/CppHelpers.js +39 -2
- package/lib/generators/components/CppHelpers.js.flow +54 -5
- package/lib/generators/components/GenerateEventEmitterCpp.js +238 -55
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +233 -55
- package/lib/generators/components/GenerateEventEmitterH.js +77 -28
- package/lib/generators/components/GenerateEventEmitterH.js.flow +87 -32
- package/lib/generators/components/GeneratePropsH.js +1 -2
- package/lib/generators/components/GeneratePropsH.js.flow +1 -2
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +0 -1
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -5
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +4 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +4 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +2 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +2 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js +114 -1
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +114 -1
- package/lib/generators/modules/GenerateModuleCpp.js +3 -3
- package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
- package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
- package/lib/parsers/error-utils.js +44 -0
- package/lib/parsers/error-utils.js.flow +59 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +51 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +51 -0
- package/lib/parsers/flow/components/componentsUtils.js +6 -6
- package/lib/parsers/flow/components/componentsUtils.js.flow +10 -10
- package/lib/parsers/flow/components/events.js +157 -78
- package/lib/parsers/flow/components/events.js.flow +158 -77
- package/lib/parsers/flow/components/index.js +28 -105
- package/lib/parsers/flow/components/index.js.flow +21 -110
- package/lib/parsers/flow/modules/index.js +39 -49
- package/lib/parsers/flow/modules/index.js.flow +22 -31
- package/lib/parsers/flow/parser.d.ts +9 -2
- package/lib/parsers/flow/parser.js +167 -16
- package/lib/parsers/flow/parser.js.flow +209 -6
- package/lib/parsers/flow/utils.js +0 -63
- package/lib/parsers/flow/utils.js.flow +1 -77
- package/lib/parsers/parser.js.flow +141 -1
- package/lib/parsers/parserMock.js +165 -2
- package/lib/parsers/parserMock.js.flow +216 -2
- package/lib/parsers/parsers-commons.js +128 -9
- package/lib/parsers/parsers-commons.js.flow +182 -12
- package/lib/parsers/parsers-primitives.js +84 -35
- package/lib/parsers/parsers-primitives.js.flow +110 -35
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +51 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +51 -0
- package/lib/parsers/typescript/components/componentsUtils.js +31 -7
- package/lib/parsers/typescript/components/componentsUtils.js.flow +31 -14
- package/lib/parsers/typescript/components/events.js +142 -67
- package/lib/parsers/typescript/components/events.js.flow +149 -68
- package/lib/parsers/typescript/components/extends.js +2 -49
- package/lib/parsers/typescript/components/extends.js.flow +1 -55
- package/lib/parsers/typescript/components/index.js +29 -109
- package/lib/parsers/typescript/components/index.js.flow +21 -116
- package/lib/parsers/typescript/modules/index.js +41 -50
- package/lib/parsers/typescript/modules/index.js.flow +23 -31
- package/lib/parsers/typescript/parser.d.ts +9 -2
- package/lib/parsers/typescript/parser.js +193 -7
- package/lib/parsers/typescript/parser.js.flow +235 -6
- package/lib/parsers/utils.js.flow +6 -0
- package/package.json +16 -5
- package/lib/parsers/flow/components/extends.js +0 -52
- package/lib/parsers/flow/components/extends.js.flow +0 -66
- package/lib/parsers/flow/components/props.js +0 -47
- package/lib/parsers/flow/components/props.js.flow +0 -60
- package/lib/parsers/typescript/components/props.js +0 -39
- package/lib/parsers/typescript/components/props.js.flow +0 -48
- package/lib/parsers/typescript/utils.js +0 -90
- package/lib/parsers/typescript/utils.js.flow +0 -104
|
@@ -20,9 +20,60 @@ import type {
|
|
|
20
20
|
NativeModuleEnumMembers,
|
|
21
21
|
NativeModuleAliasMap,
|
|
22
22
|
NativeModuleEnumMap,
|
|
23
|
+
PropTypeAnnotation,
|
|
24
|
+
ExtendsPropsShape,
|
|
23
25
|
} from '../CodegenSchema';
|
|
24
26
|
import type {ParserType} from './errors';
|
|
25
|
-
import type {
|
|
27
|
+
import type {
|
|
28
|
+
ParserErrorCapturer,
|
|
29
|
+
TypeDeclarationMap,
|
|
30
|
+
PropAST,
|
|
31
|
+
ASTNode,
|
|
32
|
+
TypeResolutionStatus,
|
|
33
|
+
} from './utils';
|
|
34
|
+
|
|
35
|
+
export type GetTypeAnnotationFN = (
|
|
36
|
+
name: string,
|
|
37
|
+
annotation: $FlowFixMe | ASTNode,
|
|
38
|
+
defaultValue: $FlowFixMe | void,
|
|
39
|
+
withNullDefault: boolean,
|
|
40
|
+
types: TypeDeclarationMap,
|
|
41
|
+
parser: Parser,
|
|
42
|
+
buildSchema: (
|
|
43
|
+
property: PropAST,
|
|
44
|
+
types: TypeDeclarationMap,
|
|
45
|
+
parser: Parser,
|
|
46
|
+
) => $FlowFixMe,
|
|
47
|
+
) => $FlowFixMe;
|
|
48
|
+
|
|
49
|
+
export type SchemaInfo = {
|
|
50
|
+
name: string,
|
|
51
|
+
optional: boolean,
|
|
52
|
+
typeAnnotation: $FlowFixMe,
|
|
53
|
+
defaultValue: $FlowFixMe,
|
|
54
|
+
withNullDefault: boolean,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type GetSchemaInfoFN = (
|
|
58
|
+
property: PropAST,
|
|
59
|
+
types: TypeDeclarationMap,
|
|
60
|
+
) => ?SchemaInfo;
|
|
61
|
+
|
|
62
|
+
export type BuildSchemaFN<T> = (
|
|
63
|
+
property: PropAST,
|
|
64
|
+
types: TypeDeclarationMap,
|
|
65
|
+
parser: Parser,
|
|
66
|
+
) => ?NamedShape<T>;
|
|
67
|
+
|
|
68
|
+
export type ResolveTypeAnnotationFN = (
|
|
69
|
+
typeAnnotation: $FlowFixMe,
|
|
70
|
+
types: TypeDeclarationMap,
|
|
71
|
+
parser: Parser,
|
|
72
|
+
) => {
|
|
73
|
+
nullable: boolean,
|
|
74
|
+
typeAnnotation: $FlowFixMe,
|
|
75
|
+
typeResolutionStatus: TypeResolutionStatus,
|
|
76
|
+
};
|
|
26
77
|
|
|
27
78
|
/**
|
|
28
79
|
* This is the main interface for Parsers of various languages.
|
|
@@ -34,6 +85,31 @@ export interface Parser {
|
|
|
34
85
|
*/
|
|
35
86
|
typeParameterInstantiation: string;
|
|
36
87
|
|
|
88
|
+
/**
|
|
89
|
+
* TypeAlias property of the Parser
|
|
90
|
+
*/
|
|
91
|
+
typeAlias: string;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* enumDeclaration Property of the Parser
|
|
95
|
+
*/
|
|
96
|
+
enumDeclaration: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* InterfaceDeclaration property of the Parser
|
|
100
|
+
*/
|
|
101
|
+
interfaceDeclaration: string;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* This is the NullLiteralTypeAnnotation value
|
|
105
|
+
*/
|
|
106
|
+
nullLiteralTypeAnnotation: string;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* UndefinedLiteralTypeAnnotation property of the Parser
|
|
110
|
+
*/
|
|
111
|
+
undefinedLiteralTypeAnnotation: string;
|
|
112
|
+
|
|
37
113
|
/**
|
|
38
114
|
* Given a declaration, it returns true if it is a property
|
|
39
115
|
*/
|
|
@@ -161,6 +237,11 @@ export interface Parser {
|
|
|
161
237
|
*/
|
|
162
238
|
isModuleInterface(node: $FlowFixMe): boolean;
|
|
163
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Given a type name, it returns true if it is a generic type annotation
|
|
242
|
+
*/
|
|
243
|
+
isGenericTypeAnnotation(type: $FlowFixMe): boolean;
|
|
244
|
+
|
|
164
245
|
/**
|
|
165
246
|
* Given a typeAnnotation, it returns the annotated element.
|
|
166
247
|
* @parameter typeAnnotation: the annotation for a type.
|
|
@@ -236,4 +317,63 @@ export interface Parser {
|
|
|
236
317
|
* @returns: the properties of annotated element.
|
|
237
318
|
*/
|
|
238
319
|
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe;
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Given a typeAlias, it returns an array of properties.
|
|
323
|
+
* @parameter typeAlias: the type alias.
|
|
324
|
+
* @returns: an array of properties.
|
|
325
|
+
*/
|
|
326
|
+
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe>;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Given a keyword convert it to TypeAnnotation.
|
|
330
|
+
* @parameter keyword
|
|
331
|
+
* @returns: converted TypeAnnotation to Keywords
|
|
332
|
+
*/
|
|
333
|
+
convertKeywordToTypeAnnotation(keyword: string): string;
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Given a prop return its arguments.
|
|
337
|
+
* @parameter prop
|
|
338
|
+
* @returns: Arguments of the prop
|
|
339
|
+
*/
|
|
340
|
+
argumentForProp(prop: PropAST): $FlowFixMe;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Given a prop return its name.
|
|
344
|
+
* @parameter prop
|
|
345
|
+
* @returns: name property
|
|
346
|
+
*/
|
|
347
|
+
nameForArgument(prop: PropAST): $FlowFixMe;
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Given a property return if it is optional.
|
|
351
|
+
* @parameter property
|
|
352
|
+
* @returns: a boolean specifying if the Property is optional
|
|
353
|
+
*/
|
|
354
|
+
isOptionalProperty(property: $FlowFixMe): boolean;
|
|
355
|
+
|
|
356
|
+
getGetTypeAnnotationFN(): GetTypeAnnotationFN;
|
|
357
|
+
|
|
358
|
+
getGetSchemaInfoFN(): GetSchemaInfoFN;
|
|
359
|
+
|
|
360
|
+
getResolvedTypeAnnotation(
|
|
361
|
+
typeAnnotation: $FlowFixMe,
|
|
362
|
+
types: TypeDeclarationMap,
|
|
363
|
+
parser: Parser,
|
|
364
|
+
): {
|
|
365
|
+
nullable: boolean,
|
|
366
|
+
typeAnnotation: $FlowFixMe,
|
|
367
|
+
typeResolutionStatus: TypeResolutionStatus,
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN;
|
|
371
|
+
|
|
372
|
+
getProps(
|
|
373
|
+
typeDefinition: $ReadOnlyArray<PropAST>,
|
|
374
|
+
types: TypeDeclarationMap,
|
|
375
|
+
): {
|
|
376
|
+
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
377
|
+
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
|
|
378
|
+
};
|
|
239
379
|
}
|
|
@@ -38,11 +38,17 @@ function _toPrimitive(input, hint) {
|
|
|
38
38
|
}
|
|
39
39
|
return (hint === 'string' ? String : Number)(input);
|
|
40
40
|
}
|
|
41
|
+
import invariant from 'invariant';
|
|
42
|
+
const _require = require('./typescript/components/componentsUtils'),
|
|
43
|
+
flattenProperties = _require.flattenProperties;
|
|
44
|
+
const _require2 = require('./parsers-commons'),
|
|
45
|
+
buildPropSchema = _require2.buildPropSchema;
|
|
46
|
+
|
|
41
47
|
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
|
|
42
48
|
const flowParser = require('flow-parser');
|
|
43
|
-
const
|
|
49
|
+
const _require3 = require('./errors'),
|
|
44
50
|
UnsupportedObjectPropertyTypeAnnotationParserError =
|
|
45
|
-
|
|
51
|
+
_require3.UnsupportedObjectPropertyTypeAnnotationParserError;
|
|
46
52
|
const schemaMock = {
|
|
47
53
|
modules: {
|
|
48
54
|
StringPropNativeComponentView: {
|
|
@@ -65,6 +71,19 @@ export class MockedParser {
|
|
|
65
71
|
'typeParameterInstantiation',
|
|
66
72
|
'TypeParameterInstantiation',
|
|
67
73
|
);
|
|
74
|
+
_defineProperty(this, 'typeAlias', 'TypeAlias');
|
|
75
|
+
_defineProperty(this, 'enumDeclaration', 'EnumDeclaration');
|
|
76
|
+
_defineProperty(this, 'interfaceDeclaration', 'InterfaceDeclaration');
|
|
77
|
+
_defineProperty(
|
|
78
|
+
this,
|
|
79
|
+
'nullLiteralTypeAnnotation',
|
|
80
|
+
'NullLiteralTypeAnnotation',
|
|
81
|
+
);
|
|
82
|
+
_defineProperty(
|
|
83
|
+
this,
|
|
84
|
+
'undefinedLiteralTypeAnnotation',
|
|
85
|
+
'VoidLiteralTypeAnnotation',
|
|
86
|
+
);
|
|
68
87
|
}
|
|
69
88
|
isProperty(property) {
|
|
70
89
|
return property.type === 'ObjectTypeProperty';
|
|
@@ -158,6 +177,9 @@ export class MockedParser {
|
|
|
158
177
|
node.extends[0].id.name === 'TurboModule'
|
|
159
178
|
);
|
|
160
179
|
}
|
|
180
|
+
isGenericTypeAnnotation(type) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
161
183
|
extractAnnotatedElement(typeAnnotation, types) {
|
|
162
184
|
return types[typeAnnotation.typeParameters.params[0].id.name];
|
|
163
185
|
}
|
|
@@ -208,4 +230,145 @@ export class MockedParser {
|
|
|
208
230
|
getAnnotatedElementProperties(annotatedElement) {
|
|
209
231
|
return annotatedElement.right.properties;
|
|
210
232
|
}
|
|
233
|
+
bodyProperties(typeAlias) {
|
|
234
|
+
return typeAlias.body.properties;
|
|
235
|
+
}
|
|
236
|
+
convertKeywordToTypeAnnotation(keyword) {
|
|
237
|
+
return keyword;
|
|
238
|
+
}
|
|
239
|
+
argumentForProp(prop) {
|
|
240
|
+
return prop.expression;
|
|
241
|
+
}
|
|
242
|
+
nameForArgument(prop) {
|
|
243
|
+
return prop.expression.name;
|
|
244
|
+
}
|
|
245
|
+
isOptionalProperty(property) {
|
|
246
|
+
return property.optional || false;
|
|
247
|
+
}
|
|
248
|
+
getGetTypeAnnotationFN() {
|
|
249
|
+
return () => {
|
|
250
|
+
return {};
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
getGetSchemaInfoFN() {
|
|
254
|
+
return () => {
|
|
255
|
+
return {
|
|
256
|
+
name: 'MockedSchema',
|
|
257
|
+
optional: false,
|
|
258
|
+
typeAnnotation: 'BooleanTypeAnnotation',
|
|
259
|
+
defaultValue: false,
|
|
260
|
+
withNullDefault: false,
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
getResolveTypeAnnotationFN() {
|
|
265
|
+
return () => {
|
|
266
|
+
return {
|
|
267
|
+
nullable: false,
|
|
268
|
+
typeAnnotation: null,
|
|
269
|
+
typeResolutionStatus: {
|
|
270
|
+
successful: false,
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
getResolvedTypeAnnotation(typeAnnotation, types) {
|
|
276
|
+
invariant(
|
|
277
|
+
typeAnnotation != null,
|
|
278
|
+
'resolveTypeAnnotation(): typeAnnotation cannot be null',
|
|
279
|
+
);
|
|
280
|
+
let node = typeAnnotation;
|
|
281
|
+
let nullable = false;
|
|
282
|
+
let typeResolutionStatus = {
|
|
283
|
+
successful: false,
|
|
284
|
+
};
|
|
285
|
+
for (;;) {
|
|
286
|
+
if (node.type === 'NullableTypeAnnotation') {
|
|
287
|
+
nullable = true;
|
|
288
|
+
node = node.typeAnnotation;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (node.type !== 'GenericTypeAnnotation') {
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
const resolvedTypeAnnotation = types[node.id.name];
|
|
295
|
+
if (resolvedTypeAnnotation == null) {
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
switch (resolvedTypeAnnotation.type) {
|
|
299
|
+
case 'TypeAlias': {
|
|
300
|
+
typeResolutionStatus = {
|
|
301
|
+
successful: true,
|
|
302
|
+
type: 'alias',
|
|
303
|
+
name: node.id.name,
|
|
304
|
+
};
|
|
305
|
+
node = resolvedTypeAnnotation.right;
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
case 'EnumDeclaration': {
|
|
309
|
+
typeResolutionStatus = {
|
|
310
|
+
successful: true,
|
|
311
|
+
type: 'enum',
|
|
312
|
+
name: node.id.name,
|
|
313
|
+
};
|
|
314
|
+
node = resolvedTypeAnnotation.body;
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
default: {
|
|
318
|
+
throw new TypeError(
|
|
319
|
+
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
nullable: nullable,
|
|
326
|
+
typeAnnotation: node,
|
|
327
|
+
typeResolutionStatus,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
getExtendsProps(typeDefinition, types) {
|
|
331
|
+
return typeDefinition
|
|
332
|
+
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
|
|
333
|
+
.map(prop => this.extendsForProp(prop, types, this))
|
|
334
|
+
.filter(Boolean);
|
|
335
|
+
}
|
|
336
|
+
extendsForProp(prop, types, parser) {
|
|
337
|
+
const argument = this.argumentForProp(prop);
|
|
338
|
+
if (!argument) {
|
|
339
|
+
console.log('null', prop);
|
|
340
|
+
}
|
|
341
|
+
const name = parser.nameForArgument(prop);
|
|
342
|
+
if (types[name] != null) {
|
|
343
|
+
// This type is locally defined in the file
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
switch (name) {
|
|
347
|
+
case 'ViewProps':
|
|
348
|
+
return {
|
|
349
|
+
type: 'ReactNativeBuiltInType',
|
|
350
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
351
|
+
};
|
|
352
|
+
default: {
|
|
353
|
+
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
removeKnownExtends(typeDefinition, types) {
|
|
358
|
+
return typeDefinition.filter(
|
|
359
|
+
prop =>
|
|
360
|
+
prop.type !== 'ObjectTypeSpreadProperty' ||
|
|
361
|
+
this.extendsForProp(prop, types, this) === null,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
getProps(typeDefinition, types) {
|
|
365
|
+
const nonExtendsProps = this.removeKnownExtends(typeDefinition, types);
|
|
366
|
+
const props = flattenProperties(nonExtendsProps, types)
|
|
367
|
+
.map(property => buildPropSchema(property, types, this))
|
|
368
|
+
.filter(Boolean);
|
|
369
|
+
return {
|
|
370
|
+
props,
|
|
371
|
+
extendsProps: this.getExtendsProps(typeDefinition, types),
|
|
372
|
+
};
|
|
373
|
+
}
|
|
211
374
|
}
|
|
@@ -10,8 +10,17 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {
|
|
13
|
+
import type {
|
|
14
|
+
GetSchemaInfoFN,
|
|
15
|
+
GetTypeAnnotationFN,
|
|
16
|
+
Parser,
|
|
17
|
+
ResolveTypeAnnotationFN,
|
|
18
|
+
} from './parser';
|
|
14
19
|
import type {ParserType} from './errors';
|
|
20
|
+
type ExtendsForProp = null | {
|
|
21
|
+
type: 'ReactNativeBuiltInType',
|
|
22
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
23
|
+
};
|
|
15
24
|
import type {
|
|
16
25
|
UnionTypeAnnotationMemberType,
|
|
17
26
|
SchemaType,
|
|
@@ -22,8 +31,20 @@ import type {
|
|
|
22
31
|
NativeModuleEnumMembers,
|
|
23
32
|
NativeModuleAliasMap,
|
|
24
33
|
NativeModuleEnumMap,
|
|
34
|
+
PropTypeAnnotation,
|
|
35
|
+
ExtendsPropsShape,
|
|
25
36
|
} from '../CodegenSchema';
|
|
26
|
-
import type {
|
|
37
|
+
import type {
|
|
38
|
+
ParserErrorCapturer,
|
|
39
|
+
PropAST,
|
|
40
|
+
TypeDeclarationMap,
|
|
41
|
+
TypeResolutionStatus,
|
|
42
|
+
} from './utils';
|
|
43
|
+
import invariant from 'invariant';
|
|
44
|
+
|
|
45
|
+
const {flattenProperties} = require('./typescript/components/componentsUtils');
|
|
46
|
+
|
|
47
|
+
const {buildPropSchema} = require('./parsers-commons');
|
|
27
48
|
|
|
28
49
|
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
|
|
29
50
|
const flowParser = require('flow-parser');
|
|
@@ -49,6 +70,11 @@ const schemaMock = {
|
|
|
49
70
|
|
|
50
71
|
export class MockedParser implements Parser {
|
|
51
72
|
typeParameterInstantiation: string = 'TypeParameterInstantiation';
|
|
73
|
+
typeAlias: string = 'TypeAlias';
|
|
74
|
+
enumDeclaration: string = 'EnumDeclaration';
|
|
75
|
+
interfaceDeclaration: string = 'InterfaceDeclaration';
|
|
76
|
+
nullLiteralTypeAnnotation: string = 'NullLiteralTypeAnnotation';
|
|
77
|
+
undefinedLiteralTypeAnnotation: string = 'VoidLiteralTypeAnnotation';
|
|
52
78
|
|
|
53
79
|
isProperty(property: $FlowFixMe): boolean {
|
|
54
80
|
return property.type === 'ObjectTypeProperty';
|
|
@@ -172,6 +198,10 @@ export class MockedParser implements Parser {
|
|
|
172
198
|
);
|
|
173
199
|
}
|
|
174
200
|
|
|
201
|
+
isGenericTypeAnnotation(type: $FlowFixMe): boolean {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
|
|
175
205
|
extractAnnotatedElement(
|
|
176
206
|
typeAnnotation: $FlowFixMe,
|
|
177
207
|
types: TypeDeclarationMap,
|
|
@@ -231,4 +261,188 @@ export class MockedParser implements Parser {
|
|
|
231
261
|
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
|
|
232
262
|
return annotatedElement.right.properties;
|
|
233
263
|
}
|
|
264
|
+
|
|
265
|
+
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
|
|
266
|
+
return typeAlias.body.properties;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
convertKeywordToTypeAnnotation(keyword: string): string {
|
|
270
|
+
return keyword;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
argumentForProp(prop: PropAST): $FlowFixMe {
|
|
274
|
+
return prop.expression;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
nameForArgument(prop: PropAST): $FlowFixMe {
|
|
278
|
+
return prop.expression.name;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
isOptionalProperty(property: $FlowFixMe): boolean {
|
|
282
|
+
return property.optional || false;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
|
|
286
|
+
return () => {
|
|
287
|
+
return {};
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
getGetSchemaInfoFN(): GetSchemaInfoFN {
|
|
292
|
+
return () => {
|
|
293
|
+
return {
|
|
294
|
+
name: 'MockedSchema',
|
|
295
|
+
optional: false,
|
|
296
|
+
typeAnnotation: 'BooleanTypeAnnotation',
|
|
297
|
+
defaultValue: false,
|
|
298
|
+
withNullDefault: false,
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
getResolveTypeAnnotationFN(): ResolveTypeAnnotationFN {
|
|
304
|
+
return () => {
|
|
305
|
+
return {
|
|
306
|
+
nullable: false,
|
|
307
|
+
typeAnnotation: null,
|
|
308
|
+
typeResolutionStatus: {successful: false},
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
getResolvedTypeAnnotation(
|
|
314
|
+
typeAnnotation: $FlowFixMe,
|
|
315
|
+
types: TypeDeclarationMap,
|
|
316
|
+
): {
|
|
317
|
+
nullable: boolean,
|
|
318
|
+
typeAnnotation: $FlowFixMe,
|
|
319
|
+
typeResolutionStatus: TypeResolutionStatus,
|
|
320
|
+
} {
|
|
321
|
+
invariant(
|
|
322
|
+
typeAnnotation != null,
|
|
323
|
+
'resolveTypeAnnotation(): typeAnnotation cannot be null',
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
let node = typeAnnotation;
|
|
327
|
+
let nullable = false;
|
|
328
|
+
let typeResolutionStatus: TypeResolutionStatus = {
|
|
329
|
+
successful: false,
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
for (;;) {
|
|
333
|
+
if (node.type === 'NullableTypeAnnotation') {
|
|
334
|
+
nullable = true;
|
|
335
|
+
node = node.typeAnnotation;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (node.type !== 'GenericTypeAnnotation') {
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const resolvedTypeAnnotation = types[node.id.name];
|
|
344
|
+
if (resolvedTypeAnnotation == null) {
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
switch (resolvedTypeAnnotation.type) {
|
|
349
|
+
case 'TypeAlias': {
|
|
350
|
+
typeResolutionStatus = {
|
|
351
|
+
successful: true,
|
|
352
|
+
type: 'alias',
|
|
353
|
+
name: node.id.name,
|
|
354
|
+
};
|
|
355
|
+
node = resolvedTypeAnnotation.right;
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
case 'EnumDeclaration': {
|
|
359
|
+
typeResolutionStatus = {
|
|
360
|
+
successful: true,
|
|
361
|
+
type: 'enum',
|
|
362
|
+
name: node.id.name,
|
|
363
|
+
};
|
|
364
|
+
node = resolvedTypeAnnotation.body;
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
default: {
|
|
368
|
+
throw new TypeError(
|
|
369
|
+
`A non GenericTypeAnnotation must be a type declaration ('TypeAlias') or enum ('EnumDeclaration'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return {
|
|
376
|
+
nullable: nullable,
|
|
377
|
+
typeAnnotation: node,
|
|
378
|
+
typeResolutionStatus,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
getExtendsProps(
|
|
383
|
+
typeDefinition: $ReadOnlyArray<PropAST>,
|
|
384
|
+
types: TypeDeclarationMap,
|
|
385
|
+
): $ReadOnlyArray<ExtendsPropsShape> {
|
|
386
|
+
return typeDefinition
|
|
387
|
+
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
|
|
388
|
+
.map(prop => this.extendsForProp(prop, types, this))
|
|
389
|
+
.filter(Boolean);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
extendsForProp(
|
|
393
|
+
prop: PropAST,
|
|
394
|
+
types: TypeDeclarationMap,
|
|
395
|
+
parser: Parser,
|
|
396
|
+
): ExtendsForProp {
|
|
397
|
+
const argument = this.argumentForProp(prop);
|
|
398
|
+
if (!argument) {
|
|
399
|
+
console.log('null', prop);
|
|
400
|
+
}
|
|
401
|
+
const name = parser.nameForArgument(prop);
|
|
402
|
+
|
|
403
|
+
if (types[name] != null) {
|
|
404
|
+
// This type is locally defined in the file
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
switch (name) {
|
|
409
|
+
case 'ViewProps':
|
|
410
|
+
return {
|
|
411
|
+
type: 'ReactNativeBuiltInType',
|
|
412
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
413
|
+
};
|
|
414
|
+
default: {
|
|
415
|
+
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
removeKnownExtends(
|
|
421
|
+
typeDefinition: $ReadOnlyArray<PropAST>,
|
|
422
|
+
types: TypeDeclarationMap,
|
|
423
|
+
): $ReadOnlyArray<PropAST> {
|
|
424
|
+
return typeDefinition.filter(
|
|
425
|
+
prop =>
|
|
426
|
+
prop.type !== 'ObjectTypeSpreadProperty' ||
|
|
427
|
+
this.extendsForProp(prop, types, this) === null,
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
getProps(
|
|
432
|
+
typeDefinition: $ReadOnlyArray<PropAST>,
|
|
433
|
+
types: TypeDeclarationMap,
|
|
434
|
+
): {
|
|
435
|
+
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
436
|
+
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
|
|
437
|
+
} {
|
|
438
|
+
const nonExtendsProps = this.removeKnownExtends(typeDefinition, types);
|
|
439
|
+
const props = flattenProperties(nonExtendsProps, types)
|
|
440
|
+
.map(property => buildPropSchema(property, types, this))
|
|
441
|
+
.filter(Boolean);
|
|
442
|
+
|
|
443
|
+
return {
|
|
444
|
+
props,
|
|
445
|
+
extendsProps: this.getExtendsProps(typeDefinition, types),
|
|
446
|
+
};
|
|
447
|
+
}
|
|
234
448
|
}
|