@react-native/codegen 0.73.0-nightly-20230603-fd9e295be → 0.73.0-nightly-20230606-396cdac62
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/parsers/flow/components/componentsUtils.js +6 -13
- package/lib/parsers/flow/components/componentsUtils.js.flow +6 -17
- package/lib/parsers/flow/components/events.js +6 -19
- package/lib/parsers/flow/components/events.js.flow +5 -18
- package/lib/parsers/flow/components/index.js +9 -51
- package/lib/parsers/flow/components/index.js.flow +2 -53
- package/lib/parsers/flow/parser.js +41 -32
- package/lib/parsers/flow/parser.js.flow +42 -29
- package/lib/parsers/parser.js.flow +24 -0
- package/lib/parsers/parserMock.js +23 -1
- package/lib/parsers/parserMock.js.flow +28 -1
- package/lib/parsers/parsers-commons.js +98 -1
- package/lib/parsers/parsers-commons.js.flow +124 -1
- package/lib/parsers/parsers-primitives.js +20 -0
- package/lib/parsers/parsers-primitives.js.flow +28 -0
- package/lib/parsers/typescript/components/componentsUtils.js +13 -39
- package/lib/parsers/typescript/components/componentsUtils.js.flow +13 -37
- package/lib/parsers/typescript/components/events.js +7 -17
- package/lib/parsers/typescript/components/events.js.flow +6 -16
- package/lib/parsers/typescript/components/index.js +9 -52
- package/lib/parsers/typescript/components/index.js.flow +2 -52
- package/lib/parsers/typescript/modules/index.js +2 -1
- package/lib/parsers/typescript/modules/index.js.flow +2 -1
- package/lib/parsers/typescript/parser.js +75 -56
- package/lib/parsers/typescript/parser.js.flow +52 -37
- package/package.json +1 -1
|
@@ -12,16 +12,6 @@
|
|
|
12
12
|
|
|
13
13
|
const _require = require('../utils.js'),
|
|
14
14
|
getValueFromTypes = _require.getValueFromTypes;
|
|
15
|
-
function getProperties(typeName, types) {
|
|
16
|
-
const typeAlias = types[typeName];
|
|
17
|
-
try {
|
|
18
|
-
return typeAlias.right.typeParameters.params[0].properties;
|
|
19
|
-
} catch (e) {
|
|
20
|
-
throw new Error(
|
|
21
|
-
`Failed to find type definition for "${typeName}", please check that you have a valid codegen flow file`,
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
15
|
function getTypeAnnotationForArray(
|
|
26
16
|
name,
|
|
27
17
|
typeAnnotation,
|
|
@@ -53,6 +43,7 @@ function getTypeAnnotationForArray(
|
|
|
53
43
|
properties: flattenProperties(
|
|
54
44
|
objectType.typeParameters.params[0].properties,
|
|
55
45
|
types,
|
|
46
|
+
parser,
|
|
56
47
|
)
|
|
57
48
|
.map(prop => buildSchema(prop, types, parser))
|
|
58
49
|
.filter(Boolean),
|
|
@@ -72,6 +63,7 @@ function getTypeAnnotationForArray(
|
|
|
72
63
|
properties: flattenProperties(
|
|
73
64
|
nestedObjectType.typeParameters.params[0].properties,
|
|
74
65
|
types,
|
|
66
|
+
parser,
|
|
75
67
|
)
|
|
76
68
|
.map(prop => buildSchema(prop, types, parser))
|
|
77
69
|
.filter(Boolean),
|
|
@@ -170,15 +162,16 @@ function getTypeAnnotationForArray(
|
|
|
170
162
|
throw new Error(`Unknown property type for "${name}": ${type}`);
|
|
171
163
|
}
|
|
172
164
|
}
|
|
173
|
-
function flattenProperties(typeDefinition, types) {
|
|
165
|
+
function flattenProperties(typeDefinition, types, parser) {
|
|
174
166
|
return typeDefinition
|
|
175
167
|
.map(property => {
|
|
176
168
|
if (property.type === 'ObjectTypeProperty') {
|
|
177
169
|
return property;
|
|
178
170
|
} else if (property.type === 'ObjectTypeSpreadProperty') {
|
|
179
171
|
return flattenProperties(
|
|
180
|
-
getProperties(property.argument.id.name, types),
|
|
172
|
+
parser.getProperties(property.argument.id.name, types),
|
|
181
173
|
types,
|
|
174
|
+
parser,
|
|
182
175
|
);
|
|
183
176
|
}
|
|
184
177
|
})
|
|
@@ -238,6 +231,7 @@ function getTypeAnnotation(
|
|
|
238
231
|
properties: flattenProperties(
|
|
239
232
|
typeAnnotation.typeParameters.params[0].properties,
|
|
240
233
|
types,
|
|
234
|
+
parser,
|
|
241
235
|
)
|
|
242
236
|
.map(prop => buildSchema(prop, types, parser))
|
|
243
237
|
.filter(Boolean),
|
|
@@ -453,7 +447,6 @@ function getSchemaInfo(property, types) {
|
|
|
453
447
|
};
|
|
454
448
|
}
|
|
455
449
|
module.exports = {
|
|
456
|
-
getProperties,
|
|
457
450
|
getSchemaInfo,
|
|
458
451
|
getTypeAnnotation,
|
|
459
452
|
flattenProperties,
|
|
@@ -10,25 +10,10 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {NamedShape} from '../../../CodegenSchema.js';
|
|
14
13
|
const {getValueFromTypes} = require('../utils.js');
|
|
15
14
|
import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';
|
|
16
15
|
import type {BuildSchemaFN, Parser} from '../../parser';
|
|
17
16
|
|
|
18
|
-
function getProperties(
|
|
19
|
-
typeName: string,
|
|
20
|
-
types: TypeDeclarationMap,
|
|
21
|
-
): $FlowFixMe {
|
|
22
|
-
const typeAlias = types[typeName];
|
|
23
|
-
try {
|
|
24
|
-
return typeAlias.right.typeParameters.params[0].properties;
|
|
25
|
-
} catch (e) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
`Failed to find type definition for "${typeName}", please check that you have a valid codegen flow file`,
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
17
|
function getTypeAnnotationForArray<+T>(
|
|
33
18
|
name: string,
|
|
34
19
|
typeAnnotation: $FlowFixMe,
|
|
@@ -63,6 +48,7 @@ function getTypeAnnotationForArray<+T>(
|
|
|
63
48
|
properties: flattenProperties(
|
|
64
49
|
objectType.typeParameters.params[0].properties,
|
|
65
50
|
types,
|
|
51
|
+
parser,
|
|
66
52
|
)
|
|
67
53
|
.map(prop => buildSchema(prop, types, parser))
|
|
68
54
|
.filter(Boolean),
|
|
@@ -84,6 +70,7 @@ function getTypeAnnotationForArray<+T>(
|
|
|
84
70
|
properties: flattenProperties(
|
|
85
71
|
nestedObjectType.typeParameters.params[0].properties,
|
|
86
72
|
types,
|
|
73
|
+
parser,
|
|
87
74
|
)
|
|
88
75
|
.map(prop => buildSchema(prop, types, parser))
|
|
89
76
|
.filter(Boolean),
|
|
@@ -190,6 +177,7 @@ function getTypeAnnotationForArray<+T>(
|
|
|
190
177
|
function flattenProperties(
|
|
191
178
|
typeDefinition: $ReadOnlyArray<PropAST>,
|
|
192
179
|
types: TypeDeclarationMap,
|
|
180
|
+
parser: Parser,
|
|
193
181
|
): $ReadOnlyArray<PropAST> {
|
|
194
182
|
return typeDefinition
|
|
195
183
|
.map(property => {
|
|
@@ -197,8 +185,9 @@ function flattenProperties(
|
|
|
197
185
|
return property;
|
|
198
186
|
} else if (property.type === 'ObjectTypeSpreadProperty') {
|
|
199
187
|
return flattenProperties(
|
|
200
|
-
getProperties(property.argument.id.name, types),
|
|
188
|
+
parser.getProperties(property.argument.id.name, types),
|
|
201
189
|
types,
|
|
190
|
+
parser,
|
|
202
191
|
);
|
|
203
192
|
}
|
|
204
193
|
})
|
|
@@ -265,6 +254,7 @@ function getTypeAnnotation<+T>(
|
|
|
265
254
|
properties: flattenProperties(
|
|
266
255
|
typeAnnotation.typeParameters.params[0].properties,
|
|
267
256
|
types,
|
|
257
|
+
parser,
|
|
268
258
|
)
|
|
269
259
|
.map(prop => buildSchema(prop, types, parser))
|
|
270
260
|
.filter(Boolean),
|
|
@@ -503,7 +493,6 @@ function getSchemaInfo(
|
|
|
503
493
|
}
|
|
504
494
|
|
|
505
495
|
module.exports = {
|
|
506
|
-
getProperties,
|
|
507
496
|
getSchemaInfo,
|
|
508
497
|
getTypeAnnotation,
|
|
509
498
|
flattenProperties,
|
|
@@ -20,7 +20,9 @@ const _require3 = require('../../parsers-primitives'),
|
|
|
20
20
|
emitBoolProp = _require3.emitBoolProp,
|
|
21
21
|
emitDoubleProp = _require3.emitDoubleProp,
|
|
22
22
|
emitFloatProp = _require3.emitFloatProp,
|
|
23
|
-
|
|
23
|
+
emitMixedProp = _require3.emitMixedProp,
|
|
24
|
+
emitStringProp = _require3.emitStringProp,
|
|
25
|
+
emitInt32Prop = _require3.emitInt32Prop;
|
|
24
26
|
function getPropertyType(
|
|
25
27
|
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
26
28
|
* LTI update could not be added via codemod */
|
|
@@ -36,13 +38,7 @@ function getPropertyType(
|
|
|
36
38
|
case 'StringTypeAnnotation':
|
|
37
39
|
return emitStringProp(name, optional);
|
|
38
40
|
case 'Int32':
|
|
39
|
-
return
|
|
40
|
-
name,
|
|
41
|
-
optional,
|
|
42
|
-
typeAnnotation: {
|
|
43
|
-
type: 'Int32TypeAnnotation',
|
|
44
|
-
},
|
|
45
|
-
};
|
|
41
|
+
return emitInt32Prop(name, optional);
|
|
46
42
|
case 'Double':
|
|
47
43
|
return emitDoubleProp(name, optional);
|
|
48
44
|
case 'Float':
|
|
@@ -75,13 +71,7 @@ function getPropertyType(
|
|
|
75
71
|
},
|
|
76
72
|
};
|
|
77
73
|
case 'UnsafeMixed':
|
|
78
|
-
return
|
|
79
|
-
name,
|
|
80
|
-
optional,
|
|
81
|
-
typeAnnotation: {
|
|
82
|
-
type: 'MixedTypeAnnotation',
|
|
83
|
-
},
|
|
84
|
-
};
|
|
74
|
+
return emitMixedProp(name, optional);
|
|
85
75
|
case 'ArrayTypeAnnotation':
|
|
86
76
|
case '$ReadOnlyArray':
|
|
87
77
|
return {
|
|
@@ -233,10 +223,7 @@ function buildPropertiesForEvent(
|
|
|
233
223
|
) {
|
|
234
224
|
const name = property.key.name;
|
|
235
225
|
const optional = parser.isOptionalProperty(property);
|
|
236
|
-
|
|
237
|
-
property.value.type === 'NullableTypeAnnotation'
|
|
238
|
-
? property.value.typeAnnotation
|
|
239
|
-
: property.value;
|
|
226
|
+
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);
|
|
240
227
|
return getPropertyType(name, optional, typeAnnotation, parser);
|
|
241
228
|
}
|
|
242
229
|
function buildEventSchema(types, property, parser) {
|
|
@@ -27,7 +27,9 @@ const {
|
|
|
27
27
|
emitBoolProp,
|
|
28
28
|
emitDoubleProp,
|
|
29
29
|
emitFloatProp,
|
|
30
|
+
emitMixedProp,
|
|
30
31
|
emitStringProp,
|
|
32
|
+
emitInt32Prop,
|
|
31
33
|
} = require('../../parsers-primitives');
|
|
32
34
|
|
|
33
35
|
function getPropertyType(
|
|
@@ -46,13 +48,7 @@ function getPropertyType(
|
|
|
46
48
|
case 'StringTypeAnnotation':
|
|
47
49
|
return emitStringProp(name, optional);
|
|
48
50
|
case 'Int32':
|
|
49
|
-
return
|
|
50
|
-
name,
|
|
51
|
-
optional,
|
|
52
|
-
typeAnnotation: {
|
|
53
|
-
type: 'Int32TypeAnnotation',
|
|
54
|
-
},
|
|
55
|
-
};
|
|
51
|
+
return emitInt32Prop(name, optional);
|
|
56
52
|
case 'Double':
|
|
57
53
|
return emitDoubleProp(name, optional);
|
|
58
54
|
case 'Float':
|
|
@@ -85,13 +81,7 @@ function getPropertyType(
|
|
|
85
81
|
},
|
|
86
82
|
};
|
|
87
83
|
case 'UnsafeMixed':
|
|
88
|
-
return
|
|
89
|
-
name,
|
|
90
|
-
optional,
|
|
91
|
-
typeAnnotation: {
|
|
92
|
-
type: 'MixedTypeAnnotation',
|
|
93
|
-
},
|
|
94
|
-
};
|
|
84
|
+
return emitMixedProp(name, optional);
|
|
95
85
|
case 'ArrayTypeAnnotation':
|
|
96
86
|
case '$ReadOnlyArray':
|
|
97
87
|
return {
|
|
@@ -247,10 +237,7 @@ function buildPropertiesForEvent(
|
|
|
247
237
|
): NamedShape<EventTypeAnnotation> {
|
|
248
238
|
const name = property.key.name;
|
|
249
239
|
const optional = parser.isOptionalProperty(property);
|
|
250
|
-
|
|
251
|
-
property.value.type === 'NullableTypeAnnotation'
|
|
252
|
-
? property.value.typeAnnotation
|
|
253
|
-
: property.value;
|
|
240
|
+
const typeAnnotation = parser.getTypeAnnotationFromProperty(property);
|
|
254
241
|
|
|
255
242
|
return getPropertyType(name, optional, typeAnnotation, parser);
|
|
256
243
|
}
|
|
@@ -14,61 +14,19 @@ const _require = require('./commands'),
|
|
|
14
14
|
getCommands = _require.getCommands;
|
|
15
15
|
const _require2 = require('./events'),
|
|
16
16
|
getEvents = _require2.getEvents;
|
|
17
|
-
const _require3 = require('
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const _require5 = require('../../parsers-commons'),
|
|
22
|
-
propertyNames = _require5.propertyNames,
|
|
23
|
-
getCommandOptions = _require5.getCommandOptions,
|
|
24
|
-
getOptions = _require5.getOptions,
|
|
25
|
-
findComponentConfig = _require5.findComponentConfig;
|
|
26
|
-
function getCommandProperties(ast, parser) {
|
|
27
|
-
const _findComponentConfig = findComponentConfig(ast, parser),
|
|
28
|
-
commandTypeName = _findComponentConfig.commandTypeName,
|
|
29
|
-
commandOptionsExpression = _findComponentConfig.commandOptionsExpression;
|
|
30
|
-
if (commandTypeName == null) {
|
|
31
|
-
return [];
|
|
32
|
-
}
|
|
33
|
-
const types = parser.getTypes(ast);
|
|
34
|
-
const typeAlias = types[commandTypeName];
|
|
35
|
-
throwIfTypeAliasIsNotInterface(typeAlias, parser);
|
|
36
|
-
const properties = parser.bodyProperties(typeAlias);
|
|
37
|
-
if (!properties) {
|
|
38
|
-
throw new Error(
|
|
39
|
-
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`,
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
const flowPropertyNames = propertyNames(properties);
|
|
43
|
-
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
44
|
-
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
if (
|
|
50
|
-
commandOptions.supportedCommands.length !== flowPropertyNames.length ||
|
|
51
|
-
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
52
|
-
flowPropertyNames.includes(supportedCommand),
|
|
53
|
-
)
|
|
54
|
-
) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${flowPropertyNames.join(
|
|
57
|
-
', ',
|
|
58
|
-
)}`,
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
return properties;
|
|
62
|
-
}
|
|
17
|
+
const _require3 = require('../../parsers-commons'),
|
|
18
|
+
getOptions = _require3.getOptions,
|
|
19
|
+
findComponentConfig = _require3.findComponentConfig,
|
|
20
|
+
getCommandProperties = _require3.getCommandProperties;
|
|
63
21
|
|
|
64
22
|
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
|
|
65
23
|
function buildComponentSchema(ast, parser) {
|
|
66
|
-
const
|
|
67
|
-
componentName =
|
|
68
|
-
propsTypeName =
|
|
69
|
-
optionsExpression =
|
|
24
|
+
const _findComponentConfig = findComponentConfig(ast, parser),
|
|
25
|
+
componentName = _findComponentConfig.componentName,
|
|
26
|
+
propsTypeName = _findComponentConfig.propsTypeName,
|
|
27
|
+
optionsExpression = _findComponentConfig.optionsExpression;
|
|
70
28
|
const types = parser.getTypes(ast);
|
|
71
|
-
const propProperties = getProperties(propsTypeName, types);
|
|
29
|
+
const propProperties = parser.getProperties(propsTypeName, types);
|
|
72
30
|
const commandProperties = getCommandProperties(ast, parser);
|
|
73
31
|
const _parser$getProps = parser.getProps(propProperties, types),
|
|
74
32
|
extendsProps = _parser$getProps.extendsProps,
|
|
@@ -14,63 +14,12 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
|
|
|
14
14
|
|
|
15
15
|
const {getCommands} = require('./commands');
|
|
16
16
|
const {getEvents} = require('./events');
|
|
17
|
-
const {getProperties} = require('./componentsUtils.js');
|
|
18
|
-
const {throwIfTypeAliasIsNotInterface} = require('../../error-utils');
|
|
19
17
|
const {
|
|
20
|
-
propertyNames,
|
|
21
|
-
getCommandOptions,
|
|
22
18
|
getOptions,
|
|
23
19
|
findComponentConfig,
|
|
20
|
+
getCommandProperties,
|
|
24
21
|
} = require('../../parsers-commons');
|
|
25
22
|
|
|
26
|
-
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
|
|
27
|
-
const {commandTypeName, commandOptionsExpression} = findComponentConfig(
|
|
28
|
-
ast,
|
|
29
|
-
parser,
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
if (commandTypeName == null) {
|
|
33
|
-
return [];
|
|
34
|
-
}
|
|
35
|
-
const types = parser.getTypes(ast);
|
|
36
|
-
|
|
37
|
-
const typeAlias = types[commandTypeName];
|
|
38
|
-
|
|
39
|
-
throwIfTypeAliasIsNotInterface(typeAlias, parser);
|
|
40
|
-
|
|
41
|
-
const properties = parser.bodyProperties(typeAlias);
|
|
42
|
-
if (!properties) {
|
|
43
|
-
throw new Error(
|
|
44
|
-
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`,
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const flowPropertyNames = propertyNames(properties);
|
|
49
|
-
|
|
50
|
-
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
51
|
-
|
|
52
|
-
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
commandOptions.supportedCommands.length !== flowPropertyNames.length ||
|
|
60
|
-
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
61
|
-
flowPropertyNames.includes(supportedCommand),
|
|
62
|
-
)
|
|
63
|
-
) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${flowPropertyNames.join(
|
|
66
|
-
', ',
|
|
67
|
-
)}`,
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return properties;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
23
|
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
|
|
75
24
|
function buildComponentSchema(
|
|
76
25
|
ast: $FlowFixMe,
|
|
@@ -83,7 +32,7 @@ function buildComponentSchema(
|
|
|
83
32
|
|
|
84
33
|
const types = parser.getTypes(ast);
|
|
85
34
|
|
|
86
|
-
const propProperties = getProperties(propsTypeName, types);
|
|
35
|
+
const propProperties = parser.getProperties(propsTypeName, types);
|
|
87
36
|
const commandProperties = getCommandProperties(ast, parser);
|
|
88
37
|
const {extendsProps, props} = parser.getProps(propProperties, types);
|
|
89
38
|
|
|
@@ -50,19 +50,19 @@ const _require2 = require('./modules'),
|
|
|
50
50
|
const flowParser = require('flow-parser');
|
|
51
51
|
const _require3 = require('../parsers-commons'),
|
|
52
52
|
buildSchema = _require3.buildSchema,
|
|
53
|
-
buildPropSchema = _require3.buildPropSchema
|
|
53
|
+
buildPropSchema = _require3.buildPropSchema,
|
|
54
|
+
buildModuleSchema = _require3.buildModuleSchema,
|
|
55
|
+
handleGenericTypeAnnotation = _require3.handleGenericTypeAnnotation;
|
|
54
56
|
const _require4 = require('../parsers-primitives'),
|
|
55
57
|
Visitor = _require4.Visitor;
|
|
56
58
|
const _require5 = require('./components'),
|
|
57
59
|
buildComponentSchema = _require5.buildComponentSchema;
|
|
58
60
|
const _require6 = require('../schema.js'),
|
|
59
61
|
wrapComponentSchema = _require6.wrapComponentSchema;
|
|
60
|
-
const _require7 = require('../parsers-commons.js'),
|
|
61
|
-
buildModuleSchema = _require7.buildModuleSchema;
|
|
62
62
|
const fs = require('fs');
|
|
63
|
-
const
|
|
63
|
+
const _require7 = require('../errors'),
|
|
64
64
|
UnsupportedObjectPropertyTypeAnnotationParserError =
|
|
65
|
-
|
|
65
|
+
_require7.UnsupportedObjectPropertyTypeAnnotationParserError;
|
|
66
66
|
class FlowParser {
|
|
67
67
|
constructor() {
|
|
68
68
|
_defineProperty(
|
|
@@ -345,6 +345,11 @@ class FlowParser {
|
|
|
345
345
|
getGetSchemaInfoFN() {
|
|
346
346
|
return getSchemaInfo;
|
|
347
347
|
}
|
|
348
|
+
getTypeAnnotationFromProperty(property) {
|
|
349
|
+
return property.value.type === 'NullableTypeAnnotation'
|
|
350
|
+
? property.value.typeAnnotation
|
|
351
|
+
: property.value;
|
|
352
|
+
}
|
|
348
353
|
getGetTypeAnnotationFN() {
|
|
349
354
|
return getTypeAnnotation;
|
|
350
355
|
}
|
|
@@ -367,35 +372,20 @@ class FlowParser {
|
|
|
367
372
|
if (node.type !== 'GenericTypeAnnotation') {
|
|
368
373
|
break;
|
|
369
374
|
}
|
|
370
|
-
const
|
|
375
|
+
const typeAnnotationName = this.nameForGenericTypeAnnotation(node);
|
|
376
|
+
const resolvedTypeAnnotation = types[typeAnnotationName];
|
|
371
377
|
if (resolvedTypeAnnotation == null) {
|
|
372
378
|
break;
|
|
373
379
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
case parser.enumDeclaration: {
|
|
385
|
-
typeResolutionStatus = {
|
|
386
|
-
successful: true,
|
|
387
|
-
type: 'enum',
|
|
388
|
-
name: node.id.name,
|
|
389
|
-
};
|
|
390
|
-
node = resolvedTypeAnnotation.body;
|
|
391
|
-
break;
|
|
392
|
-
}
|
|
393
|
-
default: {
|
|
394
|
-
throw new TypeError(
|
|
395
|
-
`A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}') or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
|
|
396
|
-
);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
380
|
+
const _handleGenericTypeAnn = handleGenericTypeAnnotation(
|
|
381
|
+
node,
|
|
382
|
+
resolvedTypeAnnotation,
|
|
383
|
+
this,
|
|
384
|
+
),
|
|
385
|
+
typeAnnotationNode = _handleGenericTypeAnn.typeAnnotation,
|
|
386
|
+
status = _handleGenericTypeAnn.typeResolutionStatus;
|
|
387
|
+
typeResolutionStatus = status;
|
|
388
|
+
node = typeAnnotationNode;
|
|
399
389
|
}
|
|
400
390
|
return {
|
|
401
391
|
nullable: nullable,
|
|
@@ -443,7 +433,7 @@ class FlowParser {
|
|
|
443
433
|
}
|
|
444
434
|
getProps(typeDefinition, types) {
|
|
445
435
|
const nonExtendsProps = this.removeKnownExtends(typeDefinition, types);
|
|
446
|
-
const props = flattenProperties(nonExtendsProps, types)
|
|
436
|
+
const props = flattenProperties(nonExtendsProps, types, this)
|
|
447
437
|
.map(property => buildPropSchema(property, types, this))
|
|
448
438
|
.filter(Boolean);
|
|
449
439
|
return {
|
|
@@ -451,6 +441,25 @@ class FlowParser {
|
|
|
451
441
|
extendsProps: this.getExtendsProps(typeDefinition, types),
|
|
452
442
|
};
|
|
453
443
|
}
|
|
444
|
+
getProperties(typeName, types) {
|
|
445
|
+
const typeAlias = types[typeName];
|
|
446
|
+
try {
|
|
447
|
+
return typeAlias.right.typeParameters.params[0].properties;
|
|
448
|
+
} catch (e) {
|
|
449
|
+
throw new Error(
|
|
450
|
+
`Failed to find type definition for "${typeName}", please check that you have a valid codegen flow file`,
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
nextNodeForTypeAlias(typeAnnotation) {
|
|
455
|
+
return typeAnnotation.right;
|
|
456
|
+
}
|
|
457
|
+
nextNodeForEnum(typeAnnotation) {
|
|
458
|
+
return typeAnnotation.body;
|
|
459
|
+
}
|
|
460
|
+
genericTypeAnnotationErrorMessage(typeAnnotation) {
|
|
461
|
+
return `A non GenericTypeAnnotation must be a type declaration ('${this.typeAlias}') or enum ('${this.enumDeclaration}'). Instead, got the unsupported ${typeAnnotation.type}.`;
|
|
462
|
+
}
|
|
454
463
|
}
|
|
455
464
|
module.exports = {
|
|
456
465
|
FlowParser,
|
|
@@ -55,11 +55,15 @@ const {flowTranslateTypeAnnotation} = require('./modules');
|
|
|
55
55
|
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
|
|
56
56
|
const flowParser = require('flow-parser');
|
|
57
57
|
|
|
58
|
-
const {
|
|
58
|
+
const {
|
|
59
|
+
buildSchema,
|
|
60
|
+
buildPropSchema,
|
|
61
|
+
buildModuleSchema,
|
|
62
|
+
handleGenericTypeAnnotation,
|
|
63
|
+
} = require('../parsers-commons');
|
|
59
64
|
const {Visitor} = require('../parsers-primitives');
|
|
60
65
|
const {buildComponentSchema} = require('./components');
|
|
61
66
|
const {wrapComponentSchema} = require('../schema.js');
|
|
62
|
-
const {buildModuleSchema} = require('../parsers-commons.js');
|
|
63
67
|
|
|
64
68
|
const fs = require('fs');
|
|
65
69
|
|
|
@@ -376,6 +380,12 @@ class FlowParser implements Parser {
|
|
|
376
380
|
return getSchemaInfo;
|
|
377
381
|
}
|
|
378
382
|
|
|
383
|
+
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe {
|
|
384
|
+
return property.value.type === 'NullableTypeAnnotation'
|
|
385
|
+
? property.value.typeAnnotation
|
|
386
|
+
: property.value;
|
|
387
|
+
}
|
|
388
|
+
|
|
379
389
|
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
|
|
380
390
|
return getTypeAnnotation;
|
|
381
391
|
}
|
|
@@ -411,36 +421,16 @@ class FlowParser implements Parser {
|
|
|
411
421
|
break;
|
|
412
422
|
}
|
|
413
423
|
|
|
414
|
-
const
|
|
424
|
+
const typeAnnotationName = this.nameForGenericTypeAnnotation(node);
|
|
425
|
+
const resolvedTypeAnnotation = types[typeAnnotationName];
|
|
415
426
|
if (resolvedTypeAnnotation == null) {
|
|
416
427
|
break;
|
|
417
428
|
}
|
|
418
429
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
type: 'alias',
|
|
424
|
-
name: node.id.name,
|
|
425
|
-
};
|
|
426
|
-
node = resolvedTypeAnnotation.right;
|
|
427
|
-
break;
|
|
428
|
-
}
|
|
429
|
-
case parser.enumDeclaration: {
|
|
430
|
-
typeResolutionStatus = {
|
|
431
|
-
successful: true,
|
|
432
|
-
type: 'enum',
|
|
433
|
-
name: node.id.name,
|
|
434
|
-
};
|
|
435
|
-
node = resolvedTypeAnnotation.body;
|
|
436
|
-
break;
|
|
437
|
-
}
|
|
438
|
-
default: {
|
|
439
|
-
throw new TypeError(
|
|
440
|
-
`A non GenericTypeAnnotation must be a type declaration ('${parser.typeAlias}') or enum ('${parser.enumDeclaration}'). Instead, got the unsupported ${resolvedTypeAnnotation.type}.`,
|
|
441
|
-
);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
430
|
+
const {typeAnnotation: typeAnnotationNode, typeResolutionStatus: status} =
|
|
431
|
+
handleGenericTypeAnnotation(node, resolvedTypeAnnotation, this);
|
|
432
|
+
typeResolutionStatus = status;
|
|
433
|
+
node = typeAnnotationNode;
|
|
444
434
|
}
|
|
445
435
|
|
|
446
436
|
return {
|
|
@@ -511,7 +501,7 @@ class FlowParser implements Parser {
|
|
|
511
501
|
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
|
|
512
502
|
} {
|
|
513
503
|
const nonExtendsProps = this.removeKnownExtends(typeDefinition, types);
|
|
514
|
-
const props = flattenProperties(nonExtendsProps, types)
|
|
504
|
+
const props = flattenProperties(nonExtendsProps, types, this)
|
|
515
505
|
.map(property => buildPropSchema(property, types, this))
|
|
516
506
|
.filter(Boolean);
|
|
517
507
|
|
|
@@ -520,6 +510,29 @@ class FlowParser implements Parser {
|
|
|
520
510
|
extendsProps: this.getExtendsProps(typeDefinition, types),
|
|
521
511
|
};
|
|
522
512
|
}
|
|
513
|
+
|
|
514
|
+
getProperties(typeName: string, types: TypeDeclarationMap): $FlowFixMe {
|
|
515
|
+
const typeAlias = types[typeName];
|
|
516
|
+
try {
|
|
517
|
+
return typeAlias.right.typeParameters.params[0].properties;
|
|
518
|
+
} catch (e) {
|
|
519
|
+
throw new Error(
|
|
520
|
+
`Failed to find type definition for "${typeName}", please check that you have a valid codegen flow file`,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
nextNodeForTypeAlias(typeAnnotation: $FlowFixMe): $FlowFixMe {
|
|
526
|
+
return typeAnnotation.right;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
nextNodeForEnum(typeAnnotation: $FlowFixMe): $FlowFixMe {
|
|
530
|
+
return typeAnnotation.body;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string {
|
|
534
|
+
return `A non GenericTypeAnnotation must be a type declaration ('${this.typeAlias}') or enum ('${this.enumDeclaration}'). Instead, got the unsupported ${typeAnnotation.type}.`;
|
|
535
|
+
}
|
|
523
536
|
}
|
|
524
537
|
|
|
525
538
|
module.exports = {
|
|
@@ -357,6 +357,13 @@ export interface Parser {
|
|
|
357
357
|
|
|
358
358
|
getGetSchemaInfoFN(): GetSchemaInfoFN;
|
|
359
359
|
|
|
360
|
+
/**
|
|
361
|
+
* Given a property return the type annotation.
|
|
362
|
+
* @parameter property
|
|
363
|
+
* @returns: the annotation for a type in the AST.
|
|
364
|
+
*/
|
|
365
|
+
getTypeAnnotationFromProperty(property: PropAST): $FlowFixMe;
|
|
366
|
+
|
|
360
367
|
getResolvedTypeAnnotation(
|
|
361
368
|
typeAnnotation: $FlowFixMe,
|
|
362
369
|
types: TypeDeclarationMap,
|
|
@@ -376,4 +383,21 @@ export interface Parser {
|
|
|
376
383
|
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
377
384
|
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
|
|
378
385
|
};
|
|
386
|
+
|
|
387
|
+
getProperties(typeName: string, types: TypeDeclarationMap): $FlowFixMe;
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Given a typeAlias, it returns the next node.
|
|
391
|
+
*/
|
|
392
|
+
nextNodeForTypeAlias(typeAnnotation: $FlowFixMe): $FlowFixMe;
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Given an enum Declaration, it returns the next node.
|
|
396
|
+
*/
|
|
397
|
+
nextNodeForEnum(typeAnnotation: $FlowFixMe): $FlowFixMe;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Given a unsupported typeAnnotation, returns an error message.
|
|
401
|
+
*/
|
|
402
|
+
genericTypeAnnotationErrorMessage(typeAnnotation: $FlowFixMe): string;
|
|
379
403
|
}
|