@react-native/codegen 0.73.0 → 0.74.0-nightly-20231003-5dedf277c
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/cli/combine/combine-js-to-schema-cli.js +7 -1
- package/lib/cli/combine/combine-js-to-schema-cli.js.flow +7 -1
- package/lib/generators/components/CppHelpers.js +39 -2
- package/lib/generators/components/CppHelpers.js.flow +54 -5
- package/lib/generators/components/GenerateEventEmitterCpp.js +222 -28
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +221 -34
- package/lib/generators/components/GenerateEventEmitterH.js +77 -26
- package/lib/generators/components/GenerateEventEmitterH.js.flow +87 -30
- package/lib/generators/components/GeneratePropsH.js +2 -3
- package/lib/generators/components/GeneratePropsH.js.flow +2 -3
- 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 +1 -1
- package/lib/generators/modules/GenerateModuleCpp.js.flow +1 -1
- package/lib/generators/modules/GenerateModuleH.js +44 -39
- package/lib/generators/modules/GenerateModuleH.js.flow +51 -39
- package/lib/generators/modules/GenerateModuleJavaSpec.js +9 -7
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +10 -6
- package/lib/generators/modules/GenerateModuleJniH.js +1 -1
- package/lib/generators/modules/GenerateModuleJniH.js.flow +1 -1
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +11 -3
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +13 -3
- package/lib/parsers/error-utils.js +44 -0
- package/lib/parsers/error-utils.js.flow +59 -0
- package/lib/parsers/errors.js +3 -9
- package/lib/parsers/errors.js.flow +3 -9
- 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 +22 -33
- package/lib/parsers/flow/components/componentsUtils.js.flow +24 -45
- package/lib/parsers/flow/components/events.js +151 -128
- package/lib/parsers/flow/components/events.js.flow +161 -136
- package/lib/parsers/flow/components/index.js +13 -94
- package/lib/parsers/flow/components/index.js.flow +5 -102
- package/lib/parsers/flow/modules/index.js +24 -24
- package/lib/parsers/flow/modules/index.js.flow +6 -6
- package/lib/parsers/flow/parser.d.ts +9 -2
- package/lib/parsers/flow/parser.js +179 -18
- package/lib/parsers/flow/parser.js.flow +231 -7
- package/lib/parsers/flow/utils.js +0 -63
- package/lib/parsers/flow/utils.js.flow +1 -77
- package/lib/parsers/parser.js.flow +176 -3
- package/lib/parsers/parserMock.js +202 -4
- package/lib/parsers/parserMock.js.flow +253 -4
- package/lib/parsers/parsers-commons.js +268 -10
- package/lib/parsers/parsers-commons.js.flow +365 -12
- package/lib/parsers/parsers-primitives.js +95 -1
- package/lib/parsers/parsers-primitives.js.flow +136 -1
- 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 +49 -57
- package/lib/parsers/typescript/components/componentsUtils.js.flow +48 -66
- package/lib/parsers/typescript/components/events.js +136 -115
- package/lib/parsers/typescript/components/events.js.flow +148 -119
- 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 +14 -99
- package/lib/parsers/typescript/components/index.js.flow +6 -106
- package/lib/parsers/typescript/modules/index.js +28 -26
- package/lib/parsers/typescript/modules/index.js.flow +9 -8
- package/lib/parsers/typescript/parser.d.ts +9 -2
- package/lib/parsers/typescript/parser.js +208 -14
- package/lib/parsers/typescript/parser.js.flow +247 -8
- package/lib/parsers/utils.js.flow +6 -0
- package/package.json +27 -16
- 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
|
@@ -9,110 +9,18 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
|
-
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
|
|
13
12
|
import type {Parser} from '../../parser';
|
|
14
13
|
import type {ComponentSchemaBuilderConfig} from '../../schema.js';
|
|
15
14
|
|
|
16
15
|
const {getCommands} = require('./commands');
|
|
17
16
|
const {getEvents} = require('./events');
|
|
18
17
|
const {categorizeProps} = require('./extends');
|
|
19
|
-
const {getProps} = require('./props');
|
|
20
|
-
const {getProperties} = require('./componentsUtils.js');
|
|
21
|
-
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
|
|
22
18
|
const {
|
|
23
|
-
createComponentConfig,
|
|
24
|
-
findNativeComponentType,
|
|
25
|
-
propertyNames,
|
|
26
|
-
getCommandOptions,
|
|
27
19
|
getOptions,
|
|
28
|
-
|
|
20
|
+
findComponentConfig,
|
|
21
|
+
getCommandProperties,
|
|
29
22
|
} = require('../../parsers-commons');
|
|
30
23
|
|
|
31
|
-
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
|
|
32
|
-
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
|
|
33
|
-
const foundConfigs: Array<{[string]: string}> = [];
|
|
34
|
-
|
|
35
|
-
const defaultExports = ast.body.filter(
|
|
36
|
-
node => node.type === 'ExportDefaultDeclaration',
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
defaultExports.forEach(statement =>
|
|
40
|
-
findNativeComponentType(statement, foundConfigs, parser),
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
if (foundConfigs.length === 0) {
|
|
44
|
-
throw new Error('Could not find component config for native component');
|
|
45
|
-
}
|
|
46
|
-
if (foundConfigs.length > 1) {
|
|
47
|
-
throw new Error('Only one component is supported per file');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const foundConfig = foundConfigs[0];
|
|
51
|
-
|
|
52
|
-
const namedExports = ast.body.filter(
|
|
53
|
-
node => node.type === 'ExportNamedDeclaration',
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const commandsTypeNames = namedExports
|
|
57
|
-
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
|
|
58
|
-
.filter(Boolean);
|
|
59
|
-
|
|
60
|
-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
61
|
-
|
|
62
|
-
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function getCommandProperties(ast: $FlowFixMe, parser: Parser) {
|
|
66
|
-
const {commandTypeName, commandOptionsExpression} = findComponentConfig(
|
|
67
|
-
ast,
|
|
68
|
-
parser,
|
|
69
|
-
);
|
|
70
|
-
if (commandTypeName == null) {
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const types = parser.getTypes(ast);
|
|
75
|
-
const typeAlias = types[commandTypeName];
|
|
76
|
-
|
|
77
|
-
if (typeAlias.type !== 'TSInterfaceDeclaration') {
|
|
78
|
-
throw new Error(
|
|
79
|
-
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const properties = parser.bodyProperties(typeAlias);
|
|
84
|
-
if (!properties) {
|
|
85
|
-
throw new Error(
|
|
86
|
-
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const typeScriptPropertyNames = propertyNames(properties);
|
|
91
|
-
|
|
92
|
-
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
93
|
-
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
94
|
-
throw new Error(
|
|
95
|
-
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
commandOptions.supportedCommands.length !==
|
|
101
|
-
typeScriptPropertyNames.length ||
|
|
102
|
-
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
103
|
-
typeScriptPropertyNames.includes(supportedCommand),
|
|
104
|
-
)
|
|
105
|
-
) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${typeScriptPropertyNames.join(
|
|
108
|
-
', ',
|
|
109
|
-
)}`,
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return properties;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
24
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
117
25
|
type PropsAST = Object;
|
|
118
26
|
|
|
@@ -128,24 +36,16 @@ function buildComponentSchema(
|
|
|
128
36
|
|
|
129
37
|
const types = parser.getTypes(ast);
|
|
130
38
|
|
|
131
|
-
const propProperties = getProperties(propsTypeName, types);
|
|
39
|
+
const propProperties = parser.getProperties(propsTypeName, types);
|
|
132
40
|
|
|
133
41
|
const commandProperties = getCommandProperties(ast, parser);
|
|
134
42
|
|
|
135
43
|
const options = getOptions(optionsExpression);
|
|
136
44
|
|
|
137
|
-
const extendsProps: Array<ExtendsPropsShape> = [];
|
|
138
|
-
const componentPropAsts: Array<PropsAST> = [];
|
|
139
45
|
const componentEventAsts: Array<PropsAST> = [];
|
|
140
|
-
categorizeProps(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
extendsProps,
|
|
144
|
-
componentPropAsts,
|
|
145
|
-
componentEventAsts,
|
|
146
|
-
);
|
|
147
|
-
const props = getProps(componentPropAsts, types);
|
|
148
|
-
const events = getEvents(componentEventAsts, types);
|
|
46
|
+
categorizeProps(propProperties, types, componentEventAsts);
|
|
47
|
+
const {props, extendsProps} = parser.getProps(propProperties, types);
|
|
48
|
+
const events = getEvents(componentEventAsts, types, parser);
|
|
149
49
|
const commands = getCommands(commandProperties, types);
|
|
150
50
|
|
|
151
51
|
return {
|
|
@@ -14,25 +14,23 @@ const _require = require('../parseTopLevelType'),
|
|
|
14
14
|
flattenIntersectionType = _require.flattenIntersectionType;
|
|
15
15
|
const _require2 = require('../components/componentsUtils'),
|
|
16
16
|
flattenProperties = _require2.flattenProperties;
|
|
17
|
-
const _require3 = require('
|
|
18
|
-
|
|
19
|
-
const _require4 = require('../../parsers-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const _require6 = require('../../errors'),
|
|
33
|
-
UnsupportedGenericParserError = _require6.UnsupportedGenericParserError,
|
|
17
|
+
const _require3 = require('../../parsers-commons'),
|
|
18
|
+
parseObjectProperty = _require3.parseObjectProperty;
|
|
19
|
+
const _require4 = require('../../parsers-primitives'),
|
|
20
|
+
emitArrayType = _require4.emitArrayType,
|
|
21
|
+
emitFunction = _require4.emitFunction,
|
|
22
|
+
emitDictionary = _require4.emitDictionary,
|
|
23
|
+
emitPromise = _require4.emitPromise,
|
|
24
|
+
emitRootTag = _require4.emitRootTag,
|
|
25
|
+
emitUnion = _require4.emitUnion,
|
|
26
|
+
emitCommonTypes = _require4.emitCommonTypes,
|
|
27
|
+
typeAliasResolution = _require4.typeAliasResolution,
|
|
28
|
+
typeEnumResolution = _require4.typeEnumResolution,
|
|
29
|
+
translateArrayTypeAnnotation = _require4.translateArrayTypeAnnotation;
|
|
30
|
+
const _require5 = require('../../errors'),
|
|
31
|
+
UnsupportedGenericParserError = _require5.UnsupportedGenericParserError,
|
|
34
32
|
UnsupportedTypeAnnotationParserError =
|
|
35
|
-
|
|
33
|
+
_require5.UnsupportedTypeAnnotationParserError;
|
|
36
34
|
function translateObjectTypeAnnotation(
|
|
37
35
|
hasteModuleName,
|
|
38
36
|
/**
|
|
@@ -168,13 +166,16 @@ function translateTypeAnnotation(
|
|
|
168
166
|
cxxOnly,
|
|
169
167
|
parser,
|
|
170
168
|
) {
|
|
171
|
-
const
|
|
169
|
+
const _parser$getResolvedTy = parser.getResolvedTypeAnnotation(
|
|
172
170
|
typeScriptTypeAnnotation,
|
|
173
171
|
types,
|
|
172
|
+
parser,
|
|
174
173
|
),
|
|
175
|
-
nullable =
|
|
176
|
-
typeAnnotation =
|
|
177
|
-
typeResolutionStatus =
|
|
174
|
+
nullable = _parser$getResolvedTy.nullable,
|
|
175
|
+
typeAnnotation = _parser$getResolvedTy.typeAnnotation,
|
|
176
|
+
typeResolutionStatus = _parser$getResolvedTy.typeResolutionStatus;
|
|
177
|
+
const resolveTypeaAnnotationFn = parser.getResolveTypeAnnotationFN();
|
|
178
|
+
resolveTypeaAnnotationFn(typeScriptTypeAnnotation, types, parser);
|
|
178
179
|
switch (typeAnnotation.type) {
|
|
179
180
|
case 'TSArrayType': {
|
|
180
181
|
return translateArrayTypeAnnotation(
|
|
@@ -217,7 +218,7 @@ function translateTypeAnnotation(
|
|
|
217
218
|
}
|
|
218
219
|
case 'TSTypeReference': {
|
|
219
220
|
return translateTypeReferenceAnnotation(
|
|
220
|
-
typeAnnotation
|
|
221
|
+
parser.getTypeAnnotationName(typeAnnotation),
|
|
221
222
|
nullable,
|
|
222
223
|
typeAnnotation,
|
|
223
224
|
hasteModuleName,
|
|
@@ -259,7 +260,7 @@ function translateTypeAnnotation(
|
|
|
259
260
|
return translateObjectTypeAnnotation(
|
|
260
261
|
hasteModuleName,
|
|
261
262
|
nullable,
|
|
262
|
-
flattenProperties([typeAnnotation], types),
|
|
263
|
+
flattenProperties([typeAnnotation], types, parser),
|
|
263
264
|
typeResolutionStatus,
|
|
264
265
|
baseTypes,
|
|
265
266
|
types,
|
|
@@ -277,6 +278,7 @@ function translateTypeAnnotation(
|
|
|
277
278
|
flattenProperties(
|
|
278
279
|
flattenIntersectionType(typeAnnotation, types),
|
|
279
280
|
types,
|
|
281
|
+
parser,
|
|
280
282
|
),
|
|
281
283
|
typeResolutionStatus,
|
|
282
284
|
[],
|
|
@@ -298,7 +300,7 @@ function translateTypeAnnotation(
|
|
|
298
300
|
// check the property type to prevent developers from using unsupported types
|
|
299
301
|
// the return value from `translateTypeAnnotation` is unused
|
|
300
302
|
const propertyType = indexSignatures[0].typeAnnotation;
|
|
301
|
-
translateTypeAnnotation(
|
|
303
|
+
const valueType = translateTypeAnnotation(
|
|
302
304
|
hasteModuleName,
|
|
303
305
|
propertyType,
|
|
304
306
|
types,
|
|
@@ -309,7 +311,7 @@ function translateTypeAnnotation(
|
|
|
309
311
|
parser,
|
|
310
312
|
);
|
|
311
313
|
// no need to do further checking
|
|
312
|
-
return
|
|
314
|
+
return emitDictionary(nullable, valueType);
|
|
313
315
|
}
|
|
314
316
|
}
|
|
315
317
|
return translateObjectTypeAnnotation(
|
|
@@ -28,14 +28,12 @@ import type {
|
|
|
28
28
|
const {flattenIntersectionType} = require('../parseTopLevelType');
|
|
29
29
|
const {flattenProperties} = require('../components/componentsUtils');
|
|
30
30
|
|
|
31
|
-
const {resolveTypeAnnotation} = require('../utils');
|
|
32
|
-
|
|
33
31
|
const {parseObjectProperty} = require('../../parsers-commons');
|
|
34
32
|
|
|
35
33
|
const {
|
|
36
34
|
emitArrayType,
|
|
37
35
|
emitFunction,
|
|
38
|
-
|
|
36
|
+
emitDictionary,
|
|
39
37
|
emitPromise,
|
|
40
38
|
emitRootTag,
|
|
41
39
|
emitUnion,
|
|
@@ -190,7 +188,9 @@ function translateTypeAnnotation(
|
|
|
190
188
|
parser: Parser,
|
|
191
189
|
): Nullable<NativeModuleTypeAnnotation> {
|
|
192
190
|
const {nullable, typeAnnotation, typeResolutionStatus} =
|
|
193
|
-
|
|
191
|
+
parser.getResolvedTypeAnnotation(typeScriptTypeAnnotation, types, parser);
|
|
192
|
+
const resolveTypeaAnnotationFn = parser.getResolveTypeAnnotationFN();
|
|
193
|
+
resolveTypeaAnnotationFn(typeScriptTypeAnnotation, types, parser);
|
|
194
194
|
|
|
195
195
|
switch (typeAnnotation.type) {
|
|
196
196
|
case 'TSArrayType': {
|
|
@@ -234,7 +234,7 @@ function translateTypeAnnotation(
|
|
|
234
234
|
}
|
|
235
235
|
case 'TSTypeReference': {
|
|
236
236
|
return translateTypeReferenceAnnotation(
|
|
237
|
-
typeAnnotation
|
|
237
|
+
parser.getTypeAnnotationName(typeAnnotation),
|
|
238
238
|
nullable,
|
|
239
239
|
typeAnnotation,
|
|
240
240
|
hasteModuleName,
|
|
@@ -270,7 +270,7 @@ function translateTypeAnnotation(
|
|
|
270
270
|
return translateObjectTypeAnnotation(
|
|
271
271
|
hasteModuleName,
|
|
272
272
|
nullable,
|
|
273
|
-
flattenProperties([typeAnnotation], types),
|
|
273
|
+
flattenProperties([typeAnnotation], types, parser),
|
|
274
274
|
typeResolutionStatus,
|
|
275
275
|
baseTypes,
|
|
276
276
|
types,
|
|
@@ -288,6 +288,7 @@ function translateTypeAnnotation(
|
|
|
288
288
|
flattenProperties(
|
|
289
289
|
flattenIntersectionType(typeAnnotation, types),
|
|
290
290
|
types,
|
|
291
|
+
parser,
|
|
291
292
|
),
|
|
292
293
|
typeResolutionStatus,
|
|
293
294
|
[],
|
|
@@ -309,7 +310,7 @@ function translateTypeAnnotation(
|
|
|
309
310
|
// check the property type to prevent developers from using unsupported types
|
|
310
311
|
// the return value from `translateTypeAnnotation` is unused
|
|
311
312
|
const propertyType = indexSignatures[0].typeAnnotation;
|
|
312
|
-
translateTypeAnnotation(
|
|
313
|
+
const valueType = translateTypeAnnotation(
|
|
313
314
|
hasteModuleName,
|
|
314
315
|
propertyType,
|
|
315
316
|
types,
|
|
@@ -320,7 +321,7 @@ function translateTypeAnnotation(
|
|
|
320
321
|
parser,
|
|
321
322
|
);
|
|
322
323
|
// no need to do further checking
|
|
323
|
-
return
|
|
324
|
+
return emitDictionary(nullable, valueType);
|
|
324
325
|
}
|
|
325
326
|
}
|
|
326
327
|
|
|
@@ -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 TypeScriptParser implements Parser{
|
|
12
|
+
export declare class TypeScriptParser implements Parser {
|
|
13
|
+
language(): ParserType;
|
|
14
|
+
parseFile(filename: string): SchemaType;
|
|
15
|
+
parseString(contents: string, filename?: string): SchemaType;
|
|
16
|
+
parseModuleFixture(filename: string): SchemaType;
|
|
17
|
+
}
|
|
@@ -38,24 +38,31 @@ function _toPrimitive(input, hint) {
|
|
|
38
38
|
}
|
|
39
39
|
return (hint === 'string' ? String : Number)(input);
|
|
40
40
|
}
|
|
41
|
+
const invariant = require('invariant');
|
|
41
42
|
const _require = require('./modules'),
|
|
42
43
|
typeScriptTranslateTypeAnnotation =
|
|
43
44
|
_require.typeScriptTranslateTypeAnnotation;
|
|
44
45
|
|
|
45
46
|
// $FlowFixMe[untyped-import] Use flow-types for @babel/parser
|
|
46
47
|
const babelParser = require('@babel/parser');
|
|
47
|
-
const _require2 = require('../parsers-
|
|
48
|
-
|
|
49
|
-
const _require3 = require('
|
|
50
|
-
|
|
51
|
-
const _require4 = require('
|
|
52
|
-
|
|
53
|
-
const _require5 = require('../
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
const _require2 = require('../parsers-primitives'),
|
|
49
|
+
Visitor = _require2.Visitor;
|
|
50
|
+
const _require3 = require('./components'),
|
|
51
|
+
buildComponentSchema = _require3.buildComponentSchema;
|
|
52
|
+
const _require4 = require('../schema.js'),
|
|
53
|
+
wrapComponentSchema = _require4.wrapComponentSchema;
|
|
54
|
+
const _require5 = require('../parsers-commons.js'),
|
|
55
|
+
buildSchema = _require5.buildSchema,
|
|
56
|
+
buildModuleSchema = _require5.buildModuleSchema,
|
|
57
|
+
extendsForProp = _require5.extendsForProp,
|
|
58
|
+
buildPropSchema = _require5.buildPropSchema,
|
|
59
|
+
handleGenericTypeAnnotation = _require5.handleGenericTypeAnnotation;
|
|
60
|
+
const _require6 = require('./parseTopLevelType'),
|
|
61
|
+
parseTopLevelType = _require6.parseTopLevelType;
|
|
62
|
+
const _require7 = require('./components/componentsUtils'),
|
|
63
|
+
getSchemaInfo = _require7.getSchemaInfo,
|
|
64
|
+
getTypeAnnotation = _require7.getTypeAnnotation,
|
|
65
|
+
flattenProperties = _require7.flattenProperties;
|
|
59
66
|
const fs = require('fs');
|
|
60
67
|
const _require8 = require('../errors'),
|
|
61
68
|
UnsupportedObjectPropertyTypeAnnotationParserError =
|
|
@@ -67,6 +74,15 @@ class TypeScriptParser {
|
|
|
67
74
|
'typeParameterInstantiation',
|
|
68
75
|
'TSTypeParameterInstantiation',
|
|
69
76
|
);
|
|
77
|
+
_defineProperty(this, 'typeAlias', 'TSTypeAliasDeclaration');
|
|
78
|
+
_defineProperty(this, 'enumDeclaration', 'TSEnumDeclaration');
|
|
79
|
+
_defineProperty(this, 'interfaceDeclaration', 'TSInterfaceDeclaration');
|
|
80
|
+
_defineProperty(this, 'nullLiteralTypeAnnotation', 'TSNullKeyword');
|
|
81
|
+
_defineProperty(
|
|
82
|
+
this,
|
|
83
|
+
'undefinedLiteralTypeAnnotation',
|
|
84
|
+
'TSUndefinedKeyword',
|
|
85
|
+
);
|
|
70
86
|
}
|
|
71
87
|
isProperty(property) {
|
|
72
88
|
return property.type === 'TSPropertySignature';
|
|
@@ -85,7 +101,7 @@ class TypeScriptParser {
|
|
|
85
101
|
language() {
|
|
86
102
|
return 'TypeScript';
|
|
87
103
|
}
|
|
88
|
-
|
|
104
|
+
getTypeAnnotationName(typeAnnotation) {
|
|
89
105
|
var _typeAnnotation$typeN;
|
|
90
106
|
return typeAnnotation === null || typeAnnotation === void 0
|
|
91
107
|
? void 0
|
|
@@ -125,7 +141,6 @@ class TypeScriptParser {
|
|
|
125
141
|
buildModuleSchema,
|
|
126
142
|
Visitor,
|
|
127
143
|
this,
|
|
128
|
-
resolveTypeAnnotation,
|
|
129
144
|
typeScriptTranslateTypeAnnotation,
|
|
130
145
|
);
|
|
131
146
|
}
|
|
@@ -333,6 +348,185 @@ class TypeScriptParser {
|
|
|
333
348
|
}
|
|
334
349
|
return keyword;
|
|
335
350
|
}
|
|
351
|
+
argumentForProp(prop) {
|
|
352
|
+
return prop.expression;
|
|
353
|
+
}
|
|
354
|
+
nameForArgument(prop) {
|
|
355
|
+
return prop.expression.name;
|
|
356
|
+
}
|
|
357
|
+
isOptionalProperty(property) {
|
|
358
|
+
return property.optional || false;
|
|
359
|
+
}
|
|
360
|
+
getGetSchemaInfoFN() {
|
|
361
|
+
return getSchemaInfo;
|
|
362
|
+
}
|
|
363
|
+
getTypeAnnotationFromProperty(property) {
|
|
364
|
+
return property.typeAnnotation.typeAnnotation;
|
|
365
|
+
}
|
|
366
|
+
getGetTypeAnnotationFN() {
|
|
367
|
+
return getTypeAnnotation;
|
|
368
|
+
}
|
|
369
|
+
getResolvedTypeAnnotation(
|
|
370
|
+
// TODO(T108222691): Use flow-types for @babel/parser
|
|
371
|
+
typeAnnotation,
|
|
372
|
+
types,
|
|
373
|
+
parser,
|
|
374
|
+
) {
|
|
375
|
+
invariant(
|
|
376
|
+
typeAnnotation != null,
|
|
377
|
+
'resolveTypeAnnotation(): typeAnnotation cannot be null',
|
|
378
|
+
);
|
|
379
|
+
let node =
|
|
380
|
+
typeAnnotation.type === 'TSTypeAnnotation'
|
|
381
|
+
? typeAnnotation.typeAnnotation
|
|
382
|
+
: typeAnnotation;
|
|
383
|
+
let nullable = false;
|
|
384
|
+
let typeResolutionStatus = {
|
|
385
|
+
successful: false,
|
|
386
|
+
};
|
|
387
|
+
for (;;) {
|
|
388
|
+
const topLevelType = parseTopLevelType(node);
|
|
389
|
+
nullable = nullable || topLevelType.optional;
|
|
390
|
+
node = topLevelType.type;
|
|
391
|
+
if (node.type !== 'TSTypeReference') {
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
const typeAnnotationName = this.getTypeAnnotationName(node);
|
|
395
|
+
const resolvedTypeAnnotation = types[typeAnnotationName];
|
|
396
|
+
if (resolvedTypeAnnotation == null) {
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
const _handleGenericTypeAnn = handleGenericTypeAnnotation(
|
|
400
|
+
node,
|
|
401
|
+
resolvedTypeAnnotation,
|
|
402
|
+
this,
|
|
403
|
+
),
|
|
404
|
+
typeAnnotationNode = _handleGenericTypeAnn.typeAnnotation,
|
|
405
|
+
status = _handleGenericTypeAnn.typeResolutionStatus;
|
|
406
|
+
typeResolutionStatus = status;
|
|
407
|
+
node = typeAnnotationNode;
|
|
408
|
+
}
|
|
409
|
+
return {
|
|
410
|
+
nullable: nullable,
|
|
411
|
+
typeAnnotation: node,
|
|
412
|
+
typeResolutionStatus,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
getResolveTypeAnnotationFN() {
|
|
416
|
+
return (typeAnnotation, types, parser) => {
|
|
417
|
+
return this.getResolvedTypeAnnotation(typeAnnotation, types, parser);
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
isEvent(typeAnnotation) {
|
|
421
|
+
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
const eventNames = new Set(['BubblingEventHandler', 'DirectEventHandler']);
|
|
425
|
+
return eventNames.has(this.getTypeAnnotationName(typeAnnotation));
|
|
426
|
+
}
|
|
427
|
+
isProp(name, typeAnnotation) {
|
|
428
|
+
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
const isStyle =
|
|
432
|
+
name === 'style' &&
|
|
433
|
+
typeAnnotation.type === 'GenericTypeAnnotation' &&
|
|
434
|
+
this.getTypeAnnotationName(typeAnnotation) === 'ViewStyleProp';
|
|
435
|
+
return !isStyle;
|
|
436
|
+
}
|
|
437
|
+
getProps(typeDefinition, types) {
|
|
438
|
+
const extendsProps = [];
|
|
439
|
+
const componentPropAsts = [];
|
|
440
|
+
const remaining = [];
|
|
441
|
+
for (const prop of typeDefinition) {
|
|
442
|
+
// find extends
|
|
443
|
+
if (prop.type === 'TSExpressionWithTypeArguments') {
|
|
444
|
+
const extend = extendsForProp(prop, types, this);
|
|
445
|
+
if (extend) {
|
|
446
|
+
extendsProps.push(extend);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
remaining.push(prop);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// find events and props
|
|
454
|
+
for (const prop of flattenProperties(remaining, types, this)) {
|
|
455
|
+
const topLevelType = parseTopLevelType(
|
|
456
|
+
prop.typeAnnotation.typeAnnotation,
|
|
457
|
+
types,
|
|
458
|
+
);
|
|
459
|
+
if (
|
|
460
|
+
prop.type === 'TSPropertySignature' &&
|
|
461
|
+
!this.isEvent(topLevelType.type) &&
|
|
462
|
+
this.isProp(prop.key.name, prop)
|
|
463
|
+
) {
|
|
464
|
+
componentPropAsts.push(prop);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
props: componentPropAsts
|
|
469
|
+
.map(property => buildPropSchema(property, types, this))
|
|
470
|
+
.filter(Boolean),
|
|
471
|
+
extendsProps,
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
getProperties(typeName, types) {
|
|
475
|
+
const alias = types[typeName];
|
|
476
|
+
if (!alias) {
|
|
477
|
+
throw new Error(
|
|
478
|
+
`Failed to find definition for "${typeName}", please check that you have a valid codegen typescript file`,
|
|
479
|
+
);
|
|
480
|
+
}
|
|
481
|
+
const aliasKind =
|
|
482
|
+
alias.type === 'TSInterfaceDeclaration' ? 'interface' : 'type';
|
|
483
|
+
try {
|
|
484
|
+
if (aliasKind === 'interface') {
|
|
485
|
+
var _alias$extends;
|
|
486
|
+
return [
|
|
487
|
+
...((_alias$extends = alias.extends) !== null &&
|
|
488
|
+
_alias$extends !== void 0
|
|
489
|
+
? _alias$extends
|
|
490
|
+
: []),
|
|
491
|
+
...alias.body.body,
|
|
492
|
+
];
|
|
493
|
+
}
|
|
494
|
+
return (
|
|
495
|
+
alias.typeAnnotation.members ||
|
|
496
|
+
alias.typeAnnotation.typeParameters.params[0].members ||
|
|
497
|
+
alias.typeAnnotation.typeParameters.params
|
|
498
|
+
);
|
|
499
|
+
} catch (e) {
|
|
500
|
+
throw new Error(
|
|
501
|
+
`Failed to find ${aliasKind} definition for "${typeName}", please check that you have a valid codegen typescript file`,
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
nextNodeForTypeAlias(typeAnnotation) {
|
|
506
|
+
return typeAnnotation.typeAnnotation;
|
|
507
|
+
}
|
|
508
|
+
nextNodeForEnum(typeAnnotation) {
|
|
509
|
+
return typeAnnotation;
|
|
510
|
+
}
|
|
511
|
+
genericTypeAnnotationErrorMessage(typeAnnotation) {
|
|
512
|
+
return `A non GenericTypeAnnotation must be a type declaration ('${this.typeAlias}'), an interface ('${this.interfaceDeclaration}'), or enum ('${this.enumDeclaration}'). Instead, got the unsupported ${typeAnnotation.type}.`;
|
|
513
|
+
}
|
|
514
|
+
extractTypeFromTypeAnnotation(typeAnnotation) {
|
|
515
|
+
return typeAnnotation.type === 'TSTypeReference'
|
|
516
|
+
? typeAnnotation.typeName.name
|
|
517
|
+
: typeAnnotation.type;
|
|
518
|
+
}
|
|
519
|
+
getObjectProperties(typeAnnotation) {
|
|
520
|
+
return typeAnnotation.members;
|
|
521
|
+
}
|
|
522
|
+
getLiteralValue(option) {
|
|
523
|
+
return option.literal.value;
|
|
524
|
+
}
|
|
525
|
+
getPaperTopLevelNameDeprecated(typeAnnotation) {
|
|
526
|
+
return typeAnnotation.typeParameters.params.length > 1
|
|
527
|
+
? typeAnnotation.typeParameters.params[1].literal.value
|
|
528
|
+
: null;
|
|
529
|
+
}
|
|
336
530
|
}
|
|
337
531
|
module.exports = {
|
|
338
532
|
TypeScriptParser,
|