@react-native/codegen 0.72.5 → 0.73.0-nightly-20230605-5062553c6
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 +12 -19
- package/lib/parsers/flow/components/componentsUtils.js.flow +16 -27
- package/lib/parsers/flow/components/events.js +157 -83
- package/lib/parsers/flow/components/events.js.flow +158 -82
- package/lib/parsers/flow/components/index.js +10 -129
- package/lib/parsers/flow/components/index.js.flow +10 -150
- 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 +177 -16
- package/lib/parsers/flow/parser.js.flow +220 -6
- package/lib/parsers/flow/utils.js +0 -63
- package/lib/parsers/flow/utils.js.flow +1 -77
- package/lib/parsers/parser.js.flow +143 -1
- package/lib/parsers/parserMock.js +175 -2
- package/lib/parsers/parserMock.js.flow +227 -2
- package/lib/parsers/parsers-commons.js +169 -9
- package/lib/parsers/parsers-commons.js.flow +233 -12
- package/lib/parsers/parsers-primitives.js +94 -35
- package/lib/parsers/parsers-primitives.js.flow +124 -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 +44 -46
- package/lib/parsers/typescript/components/componentsUtils.js.flow +44 -51
- package/lib/parsers/typescript/components/events.js +142 -72
- package/lib/parsers/typescript/components/events.js.flow +149 -73
- 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 +11 -134
- package/lib/parsers/typescript/components/index.js.flow +11 -156
- package/lib/parsers/typescript/modules/index.js +43 -51
- package/lib/parsers/typescript/modules/index.js.flow +25 -32
- package/lib/parsers/typescript/parser.d.ts +9 -2
- package/lib/parsers/typescript/parser.js +224 -7
- package/lib/parsers/typescript/parser.js.flow +262 -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
|
@@ -10,174 +10,34 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
import type {Parser} from '../../parser';
|
|
13
|
-
import type {TypeDeclarationMap} from '../../utils';
|
|
14
|
-
import type {CommandOptions} from '../../parsers-commons';
|
|
15
13
|
import type {ComponentSchemaBuilderConfig} from '../../schema.js';
|
|
16
14
|
|
|
17
15
|
const {getCommands} = require('./commands');
|
|
18
16
|
const {getEvents} = require('./events');
|
|
19
|
-
const {getExtendsProps, removeKnownExtends} = require('./extends');
|
|
20
|
-
const {getProps} = require('./props');
|
|
21
|
-
const {getProperties} = require('./componentsUtils.js');
|
|
22
|
-
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
|
|
23
17
|
const {
|
|
24
|
-
createComponentConfig,
|
|
25
|
-
findNativeComponentType,
|
|
26
|
-
getCommandOptions,
|
|
27
18
|
getOptions,
|
|
19
|
+
findComponentConfig,
|
|
20
|
+
getCommandProperties,
|
|
28
21
|
} = require('../../parsers-commons');
|
|
29
22
|
|
|
30
|
-
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
|
|
31
|
-
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
|
|
32
|
-
const foundConfigs: Array<{[string]: string}> = [];
|
|
33
|
-
|
|
34
|
-
const defaultExports = ast.body.filter(
|
|
35
|
-
node => node.type === 'ExportDefaultDeclaration',
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
defaultExports.forEach(statement => {
|
|
39
|
-
findNativeComponentType(statement, foundConfigs, parser);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
if (foundConfigs.length === 0) {
|
|
43
|
-
throw new Error('Could not find component config for native component');
|
|
44
|
-
}
|
|
45
|
-
if (foundConfigs.length > 1) {
|
|
46
|
-
throw new Error('Only one component is supported per file');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const foundConfig = foundConfigs[0];
|
|
50
|
-
|
|
51
|
-
const namedExports = ast.body.filter(
|
|
52
|
-
node => node.type === 'ExportNamedDeclaration',
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const commandsTypeNames = namedExports
|
|
56
|
-
.map(statement => {
|
|
57
|
-
let callExpression;
|
|
58
|
-
let calleeName;
|
|
59
|
-
try {
|
|
60
|
-
callExpression = statement.declaration.declarations[0].init;
|
|
61
|
-
calleeName = callExpression.callee.name;
|
|
62
|
-
} catch (e) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (calleeName !== 'codegenNativeCommands') {
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// const statement.declaration.declarations[0].init
|
|
71
|
-
if (callExpression.arguments.length !== 1) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
'codegenNativeCommands must be passed options including the supported commands',
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const typeArgumentParam = callExpression.typeArguments.params[0];
|
|
78
|
-
|
|
79
|
-
if (typeArgumentParam.type !== 'GenericTypeAnnotation') {
|
|
80
|
-
throw new Error(
|
|
81
|
-
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
commandTypeName: typeArgumentParam.id.name,
|
|
87
|
-
commandOptionsExpression: callExpression.arguments[0],
|
|
88
|
-
};
|
|
89
|
-
})
|
|
90
|
-
.filter(Boolean);
|
|
91
|
-
|
|
92
|
-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
93
|
-
|
|
94
|
-
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function getCommandProperties(
|
|
98
|
-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
99
|
-
* LTI update could not be added via codemod */
|
|
100
|
-
commandTypeName,
|
|
101
|
-
types: TypeDeclarationMap,
|
|
102
|
-
commandOptions: ?CommandOptions,
|
|
103
|
-
) {
|
|
104
|
-
if (commandTypeName == null) {
|
|
105
|
-
return [];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const typeAlias = types[commandTypeName];
|
|
109
|
-
|
|
110
|
-
if (typeAlias.type !== 'InterfaceDeclaration') {
|
|
111
|
-
throw new Error(
|
|
112
|
-
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
let properties;
|
|
117
|
-
try {
|
|
118
|
-
properties = typeAlias.body.properties;
|
|
119
|
-
} catch (e) {
|
|
120
|
-
throw new Error(
|
|
121
|
-
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen flow file`,
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const flowPropertyNames = properties
|
|
126
|
-
.map(property => property && property.key && property.key.name)
|
|
127
|
-
.filter(Boolean);
|
|
128
|
-
|
|
129
|
-
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
130
|
-
throw new Error(
|
|
131
|
-
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (
|
|
136
|
-
commandOptions.supportedCommands.length !== flowPropertyNames.length ||
|
|
137
|
-
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
138
|
-
flowPropertyNames.includes(supportedCommand),
|
|
139
|
-
)
|
|
140
|
-
) {
|
|
141
|
-
throw new Error(
|
|
142
|
-
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${flowPropertyNames.join(
|
|
143
|
-
', ',
|
|
144
|
-
)}`,
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return properties;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
23
|
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST
|
|
152
24
|
function buildComponentSchema(
|
|
153
25
|
ast: $FlowFixMe,
|
|
154
26
|
parser: Parser,
|
|
155
27
|
): ComponentSchemaBuilderConfig {
|
|
156
|
-
const {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
commandOptionsExpression,
|
|
161
|
-
optionsExpression,
|
|
162
|
-
} = findComponentConfig(ast, parser);
|
|
28
|
+
const {componentName, propsTypeName, optionsExpression} = findComponentConfig(
|
|
29
|
+
ast,
|
|
30
|
+
parser,
|
|
31
|
+
);
|
|
163
32
|
|
|
164
33
|
const types = parser.getTypes(ast);
|
|
165
34
|
|
|
166
|
-
const propProperties = getProperties(propsTypeName, types);
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
const commandProperties = getCommandProperties(
|
|
170
|
-
commandTypeName,
|
|
171
|
-
types,
|
|
172
|
-
commandOptions,
|
|
173
|
-
);
|
|
35
|
+
const propProperties = parser.getProperties(propsTypeName, types);
|
|
36
|
+
const commandProperties = getCommandProperties(ast, parser);
|
|
37
|
+
const {extendsProps, props} = parser.getProps(propProperties, types);
|
|
174
38
|
|
|
175
|
-
const extendsProps = getExtendsProps(propProperties, types);
|
|
176
39
|
const options = getOptions(optionsExpression);
|
|
177
|
-
|
|
178
|
-
const nonExtendsProps = removeKnownExtends(propProperties, types);
|
|
179
|
-
const props = getProps(nonExtendsProps, types);
|
|
180
|
-
const events = getEvents(propProperties, types);
|
|
40
|
+
const events = getEvents(propProperties, types, parser);
|
|
181
41
|
const commands = getCommands(commandProperties, types);
|
|
182
42
|
|
|
183
43
|
return {
|
|
@@ -78,33 +78,26 @@ function _iterableToArrayLimit(arr, i) {
|
|
|
78
78
|
function _arrayWithHoles(arr) {
|
|
79
79
|
if (Array.isArray(arr)) return arr;
|
|
80
80
|
}
|
|
81
|
-
const _require = require('
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
unwrapNullable = _require2.unwrapNullable,
|
|
85
|
-
wrapNullable = _require2.wrapNullable,
|
|
81
|
+
const _require = require('../../parsers-commons'),
|
|
82
|
+
unwrapNullable = _require.unwrapNullable,
|
|
83
|
+
wrapNullable = _require.wrapNullable,
|
|
86
84
|
assertGenericTypeAnnotationHasExactlyOneTypeParameter =
|
|
87
|
-
|
|
88
|
-
parseObjectProperty =
|
|
89
|
-
const
|
|
90
|
-
emitArrayType =
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
emitUnion = _require3.emitUnion,
|
|
101
|
-
emitCommonTypes = _require3.emitCommonTypes,
|
|
102
|
-
typeAliasResolution = _require3.typeAliasResolution,
|
|
103
|
-
typeEnumResolution = _require3.typeEnumResolution;
|
|
104
|
-
const _require4 = require('../../errors'),
|
|
85
|
+
_require.assertGenericTypeAnnotationHasExactlyOneTypeParameter,
|
|
86
|
+
parseObjectProperty = _require.parseObjectProperty;
|
|
87
|
+
const _require2 = require('../../parsers-primitives'),
|
|
88
|
+
emitArrayType = _require2.emitArrayType,
|
|
89
|
+
emitFunction = _require2.emitFunction,
|
|
90
|
+
emitDictionary = _require2.emitDictionary,
|
|
91
|
+
emitPromise = _require2.emitPromise,
|
|
92
|
+
emitRootTag = _require2.emitRootTag,
|
|
93
|
+
emitUnion = _require2.emitUnion,
|
|
94
|
+
emitCommonTypes = _require2.emitCommonTypes,
|
|
95
|
+
typeAliasResolution = _require2.typeAliasResolution,
|
|
96
|
+
typeEnumResolution = _require2.typeEnumResolution;
|
|
97
|
+
const _require3 = require('../../errors'),
|
|
105
98
|
UnsupportedTypeAnnotationParserError =
|
|
106
|
-
|
|
107
|
-
UnsupportedGenericParserError =
|
|
99
|
+
_require3.UnsupportedTypeAnnotationParserError,
|
|
100
|
+
UnsupportedGenericParserError = _require3.UnsupportedGenericParserError;
|
|
108
101
|
function translateTypeAnnotation(
|
|
109
102
|
hasteModuleName,
|
|
110
103
|
/**
|
|
@@ -118,9 +111,11 @@ function translateTypeAnnotation(
|
|
|
118
111
|
cxxOnly,
|
|
119
112
|
parser,
|
|
120
113
|
) {
|
|
121
|
-
const
|
|
114
|
+
const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN();
|
|
115
|
+
const _resolveTypeAnnotatio = resolveTypeAnnotationFN(
|
|
122
116
|
flowTypeAnnotation,
|
|
123
117
|
types,
|
|
118
|
+
parser,
|
|
124
119
|
),
|
|
125
120
|
nullable = _resolveTypeAnnotatio.nullable,
|
|
126
121
|
typeAnnotation = _resolveTypeAnnotatio.typeAnnotation,
|
|
@@ -215,7 +210,7 @@ function translateTypeAnnotation(
|
|
|
215
210
|
// check the property type to prevent developers from using unsupported types
|
|
216
211
|
// the return value from `translateTypeAnnotation` is unused
|
|
217
212
|
const propertyType = indexers[0].value;
|
|
218
|
-
translateTypeAnnotation(
|
|
213
|
+
const valueType = translateTypeAnnotation(
|
|
219
214
|
hasteModuleName,
|
|
220
215
|
propertyType,
|
|
221
216
|
types,
|
|
@@ -226,7 +221,7 @@ function translateTypeAnnotation(
|
|
|
226
221
|
parser,
|
|
227
222
|
);
|
|
228
223
|
// no need to do further checking
|
|
229
|
-
return
|
|
224
|
+
return emitDictionary(nullable, valueType);
|
|
230
225
|
}
|
|
231
226
|
}
|
|
232
227
|
const objectTypeAnnotation = {
|
|
@@ -258,18 +253,6 @@ function translateTypeAnnotation(
|
|
|
258
253
|
nullable,
|
|
259
254
|
);
|
|
260
255
|
}
|
|
261
|
-
case 'BooleanTypeAnnotation': {
|
|
262
|
-
return emitBoolean(nullable);
|
|
263
|
-
}
|
|
264
|
-
case 'NumberTypeAnnotation': {
|
|
265
|
-
return emitNumber(nullable);
|
|
266
|
-
}
|
|
267
|
-
case 'VoidTypeAnnotation': {
|
|
268
|
-
return emitVoid(nullable);
|
|
269
|
-
}
|
|
270
|
-
case 'StringTypeAnnotation': {
|
|
271
|
-
return emitString(nullable);
|
|
272
|
-
}
|
|
273
256
|
case 'FunctionTypeAnnotation': {
|
|
274
257
|
return emitFunction(
|
|
275
258
|
nullable,
|
|
@@ -294,13 +277,6 @@ function translateTypeAnnotation(
|
|
|
294
277
|
memberType: 'StringTypeAnnotation',
|
|
295
278
|
});
|
|
296
279
|
}
|
|
297
|
-
case 'MixedTypeAnnotation': {
|
|
298
|
-
if (cxxOnly) {
|
|
299
|
-
return emitMixed(nullable);
|
|
300
|
-
} else {
|
|
301
|
-
return emitGenericObject(nullable);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
280
|
case 'EnumStringBody':
|
|
305
281
|
case 'EnumNumberBody': {
|
|
306
282
|
return typeEnumResolution(
|
|
@@ -313,11 +289,25 @@ function translateTypeAnnotation(
|
|
|
313
289
|
);
|
|
314
290
|
}
|
|
315
291
|
default: {
|
|
316
|
-
|
|
292
|
+
const commonType = emitCommonTypes(
|
|
317
293
|
hasteModuleName,
|
|
294
|
+
types,
|
|
318
295
|
typeAnnotation,
|
|
319
|
-
|
|
296
|
+
aliasMap,
|
|
297
|
+
enumMap,
|
|
298
|
+
tryParse,
|
|
299
|
+
cxxOnly,
|
|
300
|
+
nullable,
|
|
301
|
+
parser,
|
|
320
302
|
);
|
|
303
|
+
if (!commonType) {
|
|
304
|
+
throw new UnsupportedTypeAnnotationParserError(
|
|
305
|
+
hasteModuleName,
|
|
306
|
+
typeAnnotation,
|
|
307
|
+
parser.language(),
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
return commonType;
|
|
321
311
|
}
|
|
322
312
|
}
|
|
323
313
|
}
|
|
@@ -22,7 +22,6 @@ import type {
|
|
|
22
22
|
import type {Parser} from '../../parser';
|
|
23
23
|
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
|
|
24
24
|
|
|
25
|
-
const {resolveTypeAnnotation} = require('../utils');
|
|
26
25
|
const {
|
|
27
26
|
unwrapNullable,
|
|
28
27
|
wrapNullable,
|
|
@@ -31,15 +30,10 @@ const {
|
|
|
31
30
|
} = require('../../parsers-commons');
|
|
32
31
|
const {
|
|
33
32
|
emitArrayType,
|
|
34
|
-
emitBoolean,
|
|
35
33
|
emitFunction,
|
|
36
|
-
|
|
37
|
-
emitGenericObject,
|
|
34
|
+
emitDictionary,
|
|
38
35
|
emitPromise,
|
|
39
36
|
emitRootTag,
|
|
40
|
-
emitVoid,
|
|
41
|
-
emitString,
|
|
42
|
-
emitMixed,
|
|
43
37
|
emitUnion,
|
|
44
38
|
emitCommonTypes,
|
|
45
39
|
typeAliasResolution,
|
|
@@ -64,8 +58,9 @@ function translateTypeAnnotation(
|
|
|
64
58
|
cxxOnly: boolean,
|
|
65
59
|
parser: Parser,
|
|
66
60
|
): Nullable<NativeModuleTypeAnnotation> {
|
|
61
|
+
const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN();
|
|
67
62
|
const {nullable, typeAnnotation, typeResolutionStatus} =
|
|
68
|
-
|
|
63
|
+
resolveTypeAnnotationFN(flowTypeAnnotation, types, parser);
|
|
69
64
|
|
|
70
65
|
switch (typeAnnotation.type) {
|
|
71
66
|
case 'GenericTypeAnnotation': {
|
|
@@ -157,7 +152,7 @@ function translateTypeAnnotation(
|
|
|
157
152
|
// check the property type to prevent developers from using unsupported types
|
|
158
153
|
// the return value from `translateTypeAnnotation` is unused
|
|
159
154
|
const propertyType = indexers[0].value;
|
|
160
|
-
translateTypeAnnotation(
|
|
155
|
+
const valueType = translateTypeAnnotation(
|
|
161
156
|
hasteModuleName,
|
|
162
157
|
propertyType,
|
|
163
158
|
types,
|
|
@@ -168,7 +163,7 @@ function translateTypeAnnotation(
|
|
|
168
163
|
parser,
|
|
169
164
|
);
|
|
170
165
|
// no need to do further checking
|
|
171
|
-
return
|
|
166
|
+
return emitDictionary(nullable, valueType);
|
|
172
167
|
}
|
|
173
168
|
}
|
|
174
169
|
|
|
@@ -207,18 +202,6 @@ function translateTypeAnnotation(
|
|
|
207
202
|
nullable,
|
|
208
203
|
);
|
|
209
204
|
}
|
|
210
|
-
case 'BooleanTypeAnnotation': {
|
|
211
|
-
return emitBoolean(nullable);
|
|
212
|
-
}
|
|
213
|
-
case 'NumberTypeAnnotation': {
|
|
214
|
-
return emitNumber(nullable);
|
|
215
|
-
}
|
|
216
|
-
case 'VoidTypeAnnotation': {
|
|
217
|
-
return emitVoid(nullable);
|
|
218
|
-
}
|
|
219
|
-
case 'StringTypeAnnotation': {
|
|
220
|
-
return emitString(nullable);
|
|
221
|
-
}
|
|
222
205
|
case 'FunctionTypeAnnotation': {
|
|
223
206
|
return emitFunction(
|
|
224
207
|
nullable,
|
|
@@ -243,13 +226,6 @@ function translateTypeAnnotation(
|
|
|
243
226
|
memberType: 'StringTypeAnnotation',
|
|
244
227
|
});
|
|
245
228
|
}
|
|
246
|
-
case 'MixedTypeAnnotation': {
|
|
247
|
-
if (cxxOnly) {
|
|
248
|
-
return emitMixed(nullable);
|
|
249
|
-
} else {
|
|
250
|
-
return emitGenericObject(nullable);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
229
|
case 'EnumStringBody':
|
|
254
230
|
case 'EnumNumberBody': {
|
|
255
231
|
return typeEnumResolution(
|
|
@@ -262,11 +238,26 @@ function translateTypeAnnotation(
|
|
|
262
238
|
);
|
|
263
239
|
}
|
|
264
240
|
default: {
|
|
265
|
-
|
|
241
|
+
const commonType = emitCommonTypes(
|
|
266
242
|
hasteModuleName,
|
|
243
|
+
types,
|
|
267
244
|
typeAnnotation,
|
|
268
|
-
|
|
245
|
+
aliasMap,
|
|
246
|
+
enumMap,
|
|
247
|
+
tryParse,
|
|
248
|
+
cxxOnly,
|
|
249
|
+
nullable,
|
|
250
|
+
parser,
|
|
269
251
|
);
|
|
252
|
+
|
|
253
|
+
if (!commonType) {
|
|
254
|
+
throw new UnsupportedTypeAnnotationParserError(
|
|
255
|
+
hasteModuleName,
|
|
256
|
+
typeAnnotation,
|
|
257
|
+
parser.language(),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return commonType;
|
|
270
261
|
}
|
|
271
262
|
}
|
|
272
263
|
}
|
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type {Parser} from '../parser';
|
|
8
|
+
import type { Parser } from '../parser';
|
|
9
|
+
import type { SchemaType } from '../../CodegenSchema';
|
|
10
|
+
import type { ParserType } from '../errors';
|
|
9
11
|
|
|
10
|
-
export declare class FlowParser implements Parser{
|
|
12
|
+
export declare class FlowParser implements Parser {
|
|
13
|
+
language(): ParserType;
|
|
14
|
+
parseFile(filename: string): SchemaType;
|
|
15
|
+
parseString(contents: string, filename?: string): SchemaType;
|
|
16
|
+
parseModuleFixture(filename: string): SchemaType;
|
|
17
|
+
}
|
|
@@ -38,23 +38,27 @@ function _toPrimitive(input, hint) {
|
|
|
38
38
|
}
|
|
39
39
|
return (hint === 'string' ? String : Number)(input);
|
|
40
40
|
}
|
|
41
|
-
const
|
|
42
|
-
|
|
41
|
+
const invariant = require('invariant');
|
|
42
|
+
const _require = require('./components/componentsUtils'),
|
|
43
|
+
getSchemaInfo = _require.getSchemaInfo,
|
|
44
|
+
getTypeAnnotation = _require.getTypeAnnotation,
|
|
45
|
+
flattenProperties = _require.flattenProperties;
|
|
46
|
+
const _require2 = require('./modules'),
|
|
47
|
+
flowTranslateTypeAnnotation = _require2.flowTranslateTypeAnnotation;
|
|
43
48
|
|
|
44
49
|
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
|
|
45
50
|
const flowParser = require('flow-parser');
|
|
46
|
-
const
|
|
47
|
-
buildSchema =
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
resolveTypeAnnotation = _require7.resolveTypeAnnotation;
|
|
51
|
+
const _require3 = require('../parsers-commons'),
|
|
52
|
+
buildSchema = _require3.buildSchema,
|
|
53
|
+
buildPropSchema = _require3.buildPropSchema;
|
|
54
|
+
const _require4 = require('../parsers-primitives'),
|
|
55
|
+
Visitor = _require4.Visitor;
|
|
56
|
+
const _require5 = require('./components'),
|
|
57
|
+
buildComponentSchema = _require5.buildComponentSchema;
|
|
58
|
+
const _require6 = require('../schema.js'),
|
|
59
|
+
wrapComponentSchema = _require6.wrapComponentSchema;
|
|
60
|
+
const _require7 = require('../parsers-commons.js'),
|
|
61
|
+
buildModuleSchema = _require7.buildModuleSchema;
|
|
58
62
|
const fs = require('fs');
|
|
59
63
|
const _require8 = require('../errors'),
|
|
60
64
|
UnsupportedObjectPropertyTypeAnnotationParserError =
|
|
@@ -66,6 +70,19 @@ class FlowParser {
|
|
|
66
70
|
'typeParameterInstantiation',
|
|
67
71
|
'TypeParameterInstantiation',
|
|
68
72
|
);
|
|
73
|
+
_defineProperty(this, 'typeAlias', 'TypeAlias');
|
|
74
|
+
_defineProperty(this, 'enumDeclaration', 'EnumDeclaration');
|
|
75
|
+
_defineProperty(this, 'interfaceDeclaration', 'InterfaceDeclaration');
|
|
76
|
+
_defineProperty(
|
|
77
|
+
this,
|
|
78
|
+
'nullLiteralTypeAnnotation',
|
|
79
|
+
'NullLiteralTypeAnnotation',
|
|
80
|
+
);
|
|
81
|
+
_defineProperty(
|
|
82
|
+
this,
|
|
83
|
+
'undefinedLiteralTypeAnnotation',
|
|
84
|
+
'VoidLiteralTypeAnnotation',
|
|
85
|
+
);
|
|
69
86
|
}
|
|
70
87
|
isProperty(property) {
|
|
71
88
|
return property.type === 'ObjectTypeProperty';
|
|
@@ -85,7 +102,13 @@ class FlowParser {
|
|
|
85
102
|
return 'Flow';
|
|
86
103
|
}
|
|
87
104
|
nameForGenericTypeAnnotation(typeAnnotation) {
|
|
88
|
-
|
|
105
|
+
var _typeAnnotation$id;
|
|
106
|
+
return typeAnnotation === null || typeAnnotation === void 0
|
|
107
|
+
? void 0
|
|
108
|
+
: (_typeAnnotation$id = typeAnnotation.id) === null ||
|
|
109
|
+
_typeAnnotation$id === void 0
|
|
110
|
+
? void 0
|
|
111
|
+
: _typeAnnotation$id.name;
|
|
89
112
|
}
|
|
90
113
|
checkIfInvalidModule(typeArguments) {
|
|
91
114
|
return (
|
|
@@ -116,7 +139,6 @@ class FlowParser {
|
|
|
116
139
|
buildModuleSchema,
|
|
117
140
|
Visitor,
|
|
118
141
|
this,
|
|
119
|
-
resolveTypeAnnotation,
|
|
120
142
|
flowTranslateTypeAnnotation,
|
|
121
143
|
);
|
|
122
144
|
}
|
|
@@ -208,6 +230,9 @@ class FlowParser {
|
|
|
208
230
|
node.extends[0].id.name === 'TurboModule'
|
|
209
231
|
);
|
|
210
232
|
}
|
|
233
|
+
isGenericTypeAnnotation(type) {
|
|
234
|
+
return type === 'GenericTypeAnnotation';
|
|
235
|
+
}
|
|
211
236
|
extractAnnotatedElement(typeAnnotation, types) {
|
|
212
237
|
return types[typeAnnotation.typeParameters.params[0].id.name];
|
|
213
238
|
}
|
|
@@ -300,6 +325,142 @@ class FlowParser {
|
|
|
300
325
|
getAnnotatedElementProperties(annotatedElement) {
|
|
301
326
|
return annotatedElement.right.properties;
|
|
302
327
|
}
|
|
328
|
+
bodyProperties(typeAlias) {
|
|
329
|
+
return typeAlias.body.properties;
|
|
330
|
+
}
|
|
331
|
+
convertKeywordToTypeAnnotation(keyword) {
|
|
332
|
+
return keyword;
|
|
333
|
+
}
|
|
334
|
+
argumentForProp(prop) {
|
|
335
|
+
return prop.argument;
|
|
336
|
+
}
|
|
337
|
+
nameForArgument(prop) {
|
|
338
|
+
return prop.argument.id.name;
|
|
339
|
+
}
|
|
340
|
+
isOptionalProperty(property) {
|
|
341
|
+
return (
|
|
342
|
+
property.value.type === 'NullableTypeAnnotation' || property.optional
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
getGetSchemaInfoFN() {
|
|
346
|
+
return getSchemaInfo;
|
|
347
|
+
}
|
|
348
|
+
getGetTypeAnnotationFN() {
|
|
349
|
+
return getTypeAnnotation;
|
|
350
|
+
}
|
|
351
|
+
getResolvedTypeAnnotation(typeAnnotation, types, parser) {
|
|
352
|
+
invariant(
|
|
353
|
+
typeAnnotation != null,
|
|
354
|
+
'resolveTypeAnnotation(): typeAnnotation cannot be null',
|
|
355
|
+
);
|
|
356
|
+
let node = typeAnnotation;
|
|
357
|
+
let nullable = false;
|
|
358
|
+
let typeResolutionStatus = {
|
|
359
|
+
successful: false,
|
|
360
|
+
};
|
|
361
|
+
for (;;) {
|
|
362
|
+
if (node.type === 'NullableTypeAnnotation') {
|
|
363
|
+
nullable = true;
|
|
364
|
+
node = node.typeAnnotation;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (node.type !== 'GenericTypeAnnotation') {
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
const resolvedTypeAnnotation = types[node.id.name];
|
|
371
|
+
if (resolvedTypeAnnotation == null) {
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
switch (resolvedTypeAnnotation.type) {
|
|
375
|
+
case parser.typeAlias: {
|
|
376
|
+
typeResolutionStatus = {
|
|
377
|
+
successful: true,
|
|
378
|
+
type: 'alias',
|
|
379
|
+
name: node.id.name,
|
|
380
|
+
};
|
|
381
|
+
node = resolvedTypeAnnotation.right;
|
|
382
|
+
break;
|
|
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
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return {
|
|
401
|
+
nullable: nullable,
|
|
402
|
+
typeAnnotation: node,
|
|
403
|
+
typeResolutionStatus,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
getResolveTypeAnnotationFN() {
|
|
407
|
+
return (typeAnnotation, types, parser) =>
|
|
408
|
+
this.getResolvedTypeAnnotation(typeAnnotation, types, parser);
|
|
409
|
+
}
|
|
410
|
+
extendsForProp(prop, types, parser) {
|
|
411
|
+
const argument = this.argumentForProp(prop);
|
|
412
|
+
if (!argument) {
|
|
413
|
+
console.log('null', prop);
|
|
414
|
+
}
|
|
415
|
+
const name = parser.nameForArgument(prop);
|
|
416
|
+
if (types[name] != null) {
|
|
417
|
+
// This type is locally defined in the file
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
switch (name) {
|
|
421
|
+
case 'ViewProps':
|
|
422
|
+
return {
|
|
423
|
+
type: 'ReactNativeBuiltInType',
|
|
424
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
425
|
+
};
|
|
426
|
+
default: {
|
|
427
|
+
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
removeKnownExtends(typeDefinition, types) {
|
|
432
|
+
return typeDefinition.filter(
|
|
433
|
+
prop =>
|
|
434
|
+
prop.type !== 'ObjectTypeSpreadProperty' ||
|
|
435
|
+
this.extendsForProp(prop, types, this) === null,
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
getExtendsProps(typeDefinition, types) {
|
|
439
|
+
return typeDefinition
|
|
440
|
+
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
|
|
441
|
+
.map(prop => this.extendsForProp(prop, types, this))
|
|
442
|
+
.filter(Boolean);
|
|
443
|
+
}
|
|
444
|
+
getProps(typeDefinition, types) {
|
|
445
|
+
const nonExtendsProps = this.removeKnownExtends(typeDefinition, types);
|
|
446
|
+
const props = flattenProperties(nonExtendsProps, types, this)
|
|
447
|
+
.map(property => buildPropSchema(property, types, this))
|
|
448
|
+
.filter(Boolean);
|
|
449
|
+
return {
|
|
450
|
+
props,
|
|
451
|
+
extendsProps: this.getExtendsProps(typeDefinition, types),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
getProperties(typeName, types) {
|
|
455
|
+
const typeAlias = types[typeName];
|
|
456
|
+
try {
|
|
457
|
+
return typeAlias.right.typeParameters.params[0].properties;
|
|
458
|
+
} catch (e) {
|
|
459
|
+
throw new Error(
|
|
460
|
+
`Failed to find type definition for "${typeName}", please check that you have a valid codegen flow file`,
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
303
464
|
}
|
|
304
465
|
module.exports = {
|
|
305
466
|
FlowParser,
|