@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
|
@@ -16,9 +16,22 @@ import type {
|
|
|
16
16
|
EventTypeAnnotation,
|
|
17
17
|
} from '../../../CodegenSchema.js';
|
|
18
18
|
import type {TypeDeclarationMap} from '../../utils';
|
|
19
|
+
import type {Parser} from '../../parser';
|
|
19
20
|
const {flattenProperties} = require('./componentsUtils');
|
|
20
21
|
const {parseTopLevelType} = require('../parseTopLevelType');
|
|
21
|
-
|
|
22
|
+
const {
|
|
23
|
+
throwIfEventHasNoName,
|
|
24
|
+
throwIfBubblingTypeIsNull,
|
|
25
|
+
throwIfArgumentPropsAreNull,
|
|
26
|
+
} = require('../../error-utils');
|
|
27
|
+
const {getEventArgument} = require('../../parsers-commons');
|
|
28
|
+
const {
|
|
29
|
+
emitBoolProp,
|
|
30
|
+
emitDoubleProp,
|
|
31
|
+
emitFloatProp,
|
|
32
|
+
emitStringProp,
|
|
33
|
+
emitInt32Prop,
|
|
34
|
+
} = require('../../parsers-primitives');
|
|
22
35
|
function getPropertyType(
|
|
23
36
|
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
24
37
|
* LTI update could not be added via codemod */
|
|
@@ -27,6 +40,7 @@ function getPropertyType(
|
|
|
27
40
|
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
28
41
|
* LTI update could not be added via codemod */
|
|
29
42
|
annotation,
|
|
43
|
+
parser: Parser,
|
|
30
44
|
): NamedShape<EventTypeAnnotation> {
|
|
31
45
|
const topLevelType = parseTopLevelType(annotation);
|
|
32
46
|
const typeAnnotation = topLevelType.type;
|
|
@@ -38,71 +52,126 @@ function getPropertyType(
|
|
|
38
52
|
|
|
39
53
|
switch (type) {
|
|
40
54
|
case 'TSBooleanKeyword':
|
|
55
|
+
return emitBoolProp(name, optional);
|
|
56
|
+
case 'TSStringKeyword':
|
|
57
|
+
return emitStringProp(name, optional);
|
|
58
|
+
case 'Int32':
|
|
59
|
+
return emitInt32Prop(name, optional);
|
|
60
|
+
case 'Double':
|
|
61
|
+
return emitDoubleProp(name, optional);
|
|
62
|
+
case 'Float':
|
|
63
|
+
return emitFloatProp(name, optional);
|
|
64
|
+
case 'TSTypeLiteral':
|
|
41
65
|
return {
|
|
42
66
|
name,
|
|
43
67
|
optional,
|
|
44
68
|
typeAnnotation: {
|
|
45
|
-
type: '
|
|
69
|
+
type: 'ObjectTypeAnnotation',
|
|
70
|
+
properties: typeAnnotation.members.map(member =>
|
|
71
|
+
buildPropertiesForEvent(member, parser),
|
|
72
|
+
),
|
|
46
73
|
},
|
|
47
74
|
};
|
|
48
|
-
case '
|
|
75
|
+
case 'TSUnionType':
|
|
49
76
|
return {
|
|
50
77
|
name,
|
|
51
78
|
optional,
|
|
52
79
|
typeAnnotation: {
|
|
53
|
-
type: '
|
|
80
|
+
type: 'StringEnumTypeAnnotation',
|
|
81
|
+
options: typeAnnotation.types.map(option => option.literal.value),
|
|
54
82
|
},
|
|
55
83
|
};
|
|
56
|
-
case '
|
|
84
|
+
case 'UnsafeMixed':
|
|
57
85
|
return {
|
|
58
86
|
name,
|
|
59
87
|
optional,
|
|
60
88
|
typeAnnotation: {
|
|
61
|
-
type: '
|
|
89
|
+
type: 'MixedTypeAnnotation',
|
|
62
90
|
},
|
|
63
91
|
};
|
|
64
|
-
case '
|
|
92
|
+
case 'TSArrayType':
|
|
65
93
|
return {
|
|
66
94
|
name,
|
|
67
95
|
optional,
|
|
68
|
-
typeAnnotation:
|
|
69
|
-
type: 'DoubleTypeAnnotation',
|
|
70
|
-
},
|
|
96
|
+
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
|
|
71
97
|
};
|
|
98
|
+
default:
|
|
99
|
+
(type: empty);
|
|
100
|
+
throw new Error(`Unable to determine event type for "${name}": ${type}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function extractArrayElementType(
|
|
105
|
+
typeAnnotation: $FlowFixMe,
|
|
106
|
+
name: string,
|
|
107
|
+
parser: Parser,
|
|
108
|
+
): EventTypeAnnotation {
|
|
109
|
+
const type = extractTypeFromTypeAnnotation(typeAnnotation);
|
|
110
|
+
|
|
111
|
+
switch (type) {
|
|
112
|
+
case 'TSParenthesizedType':
|
|
113
|
+
return extractArrayElementType(
|
|
114
|
+
typeAnnotation.typeAnnotation,
|
|
115
|
+
name,
|
|
116
|
+
parser,
|
|
117
|
+
);
|
|
118
|
+
case 'TSBooleanKeyword':
|
|
119
|
+
return {type: 'BooleanTypeAnnotation'};
|
|
120
|
+
case 'TSStringKeyword':
|
|
121
|
+
return {type: 'StringTypeAnnotation'};
|
|
72
122
|
case 'Float':
|
|
73
123
|
return {
|
|
74
|
-
|
|
75
|
-
optional,
|
|
76
|
-
typeAnnotation: {
|
|
77
|
-
type: 'FloatTypeAnnotation',
|
|
78
|
-
},
|
|
124
|
+
type: 'FloatTypeAnnotation',
|
|
79
125
|
};
|
|
80
|
-
case '
|
|
126
|
+
case 'Int32':
|
|
81
127
|
return {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
128
|
+
type: 'Int32TypeAnnotation',
|
|
129
|
+
};
|
|
130
|
+
case 'TSNumberKeyword':
|
|
131
|
+
case 'Double':
|
|
132
|
+
return {
|
|
133
|
+
type: 'DoubleTypeAnnotation',
|
|
88
134
|
};
|
|
89
|
-
|
|
90
135
|
case 'TSUnionType':
|
|
91
136
|
return {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
137
|
+
type: 'StringEnumTypeAnnotation',
|
|
138
|
+
options: typeAnnotation.types.map(option => option.literal.value),
|
|
139
|
+
};
|
|
140
|
+
case 'TSTypeLiteral':
|
|
141
|
+
return {
|
|
142
|
+
type: 'ObjectTypeAnnotation',
|
|
143
|
+
properties: typeAnnotation.members.map(member =>
|
|
144
|
+
buildPropertiesForEvent(member, parser),
|
|
145
|
+
),
|
|
146
|
+
};
|
|
147
|
+
case 'TSArrayType':
|
|
148
|
+
return {
|
|
149
|
+
type: 'ArrayTypeAnnotation',
|
|
150
|
+
elementType: extractArrayElementType(
|
|
151
|
+
typeAnnotation.elementType,
|
|
152
|
+
name,
|
|
153
|
+
parser,
|
|
154
|
+
),
|
|
98
155
|
};
|
|
99
156
|
default:
|
|
100
|
-
(
|
|
101
|
-
|
|
157
|
+
throw new Error(
|
|
158
|
+
`Unrecognized ${type} for Array ${name} in events.\n${JSON.stringify(
|
|
159
|
+
typeAnnotation,
|
|
160
|
+
null,
|
|
161
|
+
2,
|
|
162
|
+
)}`,
|
|
163
|
+
);
|
|
102
164
|
}
|
|
103
165
|
}
|
|
104
166
|
|
|
167
|
+
function extractTypeFromTypeAnnotation(typeAnnotation: $FlowFixMe): string {
|
|
168
|
+
return typeAnnotation.type === 'TSTypeReference'
|
|
169
|
+
? typeAnnotation.typeName.name
|
|
170
|
+
: typeAnnotation.type;
|
|
171
|
+
}
|
|
172
|
+
|
|
105
173
|
function findEventArgumentsAndType(
|
|
174
|
+
parser: Parser,
|
|
106
175
|
typeAnnotation: $FlowFixMe,
|
|
107
176
|
types: TypeDeclarationMap,
|
|
108
177
|
bubblingType: void | 'direct' | 'bubble',
|
|
@@ -114,7 +183,7 @@ function findEventArgumentsAndType(
|
|
|
114
183
|
} {
|
|
115
184
|
if (typeAnnotation.type === 'TSInterfaceDeclaration') {
|
|
116
185
|
return {
|
|
117
|
-
argumentProps: flattenProperties([typeAnnotation], types),
|
|
186
|
+
argumentProps: flattenProperties([typeAnnotation], types, parser),
|
|
118
187
|
paperTopLevelNameDeprecated: paperName,
|
|
119
188
|
bubblingType,
|
|
120
189
|
};
|
|
@@ -128,12 +197,11 @@ function findEventArgumentsAndType(
|
|
|
128
197
|
};
|
|
129
198
|
}
|
|
130
199
|
|
|
131
|
-
|
|
132
|
-
throw new Error("typeAnnotation of event doesn't have a name");
|
|
133
|
-
}
|
|
200
|
+
throwIfEventHasNoName(typeAnnotation, parser);
|
|
134
201
|
const name = typeAnnotation.typeName.name;
|
|
135
202
|
if (name === 'Readonly') {
|
|
136
203
|
return findEventArgumentsAndType(
|
|
204
|
+
parser,
|
|
137
205
|
typeAnnotation.typeParameters.params[0],
|
|
138
206
|
types,
|
|
139
207
|
bubblingType,
|
|
@@ -147,8 +215,8 @@ function findEventArgumentsAndType(
|
|
|
147
215
|
: null;
|
|
148
216
|
|
|
149
217
|
switch (typeAnnotation.typeParameters.params[0].type) {
|
|
150
|
-
case
|
|
151
|
-
case
|
|
218
|
+
case parser.nullLiteralTypeAnnotation:
|
|
219
|
+
case parser.undefinedLiteralTypeAnnotation:
|
|
152
220
|
return {
|
|
153
221
|
argumentProps: [],
|
|
154
222
|
bubblingType: eventType,
|
|
@@ -156,6 +224,7 @@ function findEventArgumentsAndType(
|
|
|
156
224
|
};
|
|
157
225
|
default:
|
|
158
226
|
return findEventArgumentsAndType(
|
|
227
|
+
parser,
|
|
159
228
|
typeAnnotation.typeParameters.params[0],
|
|
160
229
|
types,
|
|
161
230
|
eventType,
|
|
@@ -168,6 +237,7 @@ function findEventArgumentsAndType(
|
|
|
168
237
|
elementType = elementType.typeAnnotation;
|
|
169
238
|
}
|
|
170
239
|
return findEventArgumentsAndType(
|
|
240
|
+
parser,
|
|
171
241
|
elementType,
|
|
172
242
|
types,
|
|
173
243
|
bubblingType,
|
|
@@ -182,23 +252,17 @@ function findEventArgumentsAndType(
|
|
|
182
252
|
}
|
|
183
253
|
}
|
|
184
254
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
255
|
+
function buildPropertiesForEvent(
|
|
256
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
257
|
+
* LTI update could not be added via codemod */
|
|
258
|
+
property,
|
|
259
|
+
parser: Parser,
|
|
260
|
+
): NamedShape<EventTypeAnnotation> {
|
|
188
261
|
const name = property.key.name;
|
|
189
|
-
const optional = property
|
|
262
|
+
const optional = parser.isOptionalProperty(property);
|
|
190
263
|
let typeAnnotation = property.typeAnnotation.typeAnnotation;
|
|
191
264
|
|
|
192
|
-
return getPropertyType(name, optional, typeAnnotation);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
196
|
-
* LTI update could not be added via codemod */
|
|
197
|
-
function getEventArgument(argumentProps, name: $FlowFixMe) {
|
|
198
|
-
return {
|
|
199
|
-
type: 'ObjectTypeAnnotation',
|
|
200
|
-
properties: argumentProps.map(buildPropertiesForEvent),
|
|
201
|
-
};
|
|
265
|
+
return getPropertyType(name, optional, typeAnnotation, parser);
|
|
202
266
|
}
|
|
203
267
|
|
|
204
268
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
@@ -207,7 +271,8 @@ type EventTypeAST = Object;
|
|
|
207
271
|
function buildEventSchema(
|
|
208
272
|
types: TypeDeclarationMap,
|
|
209
273
|
property: EventTypeAST,
|
|
210
|
-
|
|
274
|
+
parser: Parser,
|
|
275
|
+
): ?EventTypeShape {
|
|
211
276
|
// unpack WithDefault, (T) or T|U
|
|
212
277
|
const topLevelType = parseTopLevelType(
|
|
213
278
|
property.typeAnnotation.typeAnnotation,
|
|
@@ -218,43 +283,54 @@ function buildEventSchema(
|
|
|
218
283
|
const typeAnnotation = topLevelType.type;
|
|
219
284
|
const optional = property.optional || topLevelType.optional;
|
|
220
285
|
const {argumentProps, bubblingType, paperTopLevelNameDeprecated} =
|
|
221
|
-
findEventArgumentsAndType(typeAnnotation, types);
|
|
286
|
+
findEventArgumentsAndType(parser, typeAnnotation, types);
|
|
222
287
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
if (paperTopLevelNameDeprecated != null) {
|
|
229
|
-
return {
|
|
230
|
-
name,
|
|
231
|
-
optional,
|
|
232
|
-
bubblingType,
|
|
233
|
-
paperTopLevelNameDeprecated,
|
|
234
|
-
typeAnnotation: {
|
|
235
|
-
type: 'EventTypeAnnotation',
|
|
236
|
-
argument: getEventArgument(argumentProps, name),
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
}
|
|
288
|
+
const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
|
|
289
|
+
argumentProps,
|
|
290
|
+
name,
|
|
291
|
+
);
|
|
292
|
+
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
|
|
240
293
|
|
|
294
|
+
if (paperTopLevelNameDeprecated != null) {
|
|
241
295
|
return {
|
|
242
296
|
name,
|
|
243
297
|
optional,
|
|
244
|
-
bubblingType,
|
|
298
|
+
bubblingType: nonNullableBubblingType,
|
|
299
|
+
paperTopLevelNameDeprecated,
|
|
245
300
|
typeAnnotation: {
|
|
246
301
|
type: 'EventTypeAnnotation',
|
|
247
|
-
argument: getEventArgument(
|
|
302
|
+
argument: getEventArgument(
|
|
303
|
+
nonNullableArgumentProps,
|
|
304
|
+
buildPropertiesForEvent,
|
|
305
|
+
parser,
|
|
306
|
+
),
|
|
248
307
|
},
|
|
249
308
|
};
|
|
250
309
|
}
|
|
310
|
+
|
|
311
|
+
return {
|
|
312
|
+
name,
|
|
313
|
+
optional,
|
|
314
|
+
bubblingType: nonNullableBubblingType,
|
|
315
|
+
typeAnnotation: {
|
|
316
|
+
type: 'EventTypeAnnotation',
|
|
317
|
+
argument: getEventArgument(
|
|
318
|
+
nonNullableArgumentProps,
|
|
319
|
+
buildPropertiesForEvent,
|
|
320
|
+
parser,
|
|
321
|
+
),
|
|
322
|
+
},
|
|
323
|
+
};
|
|
251
324
|
}
|
|
252
325
|
|
|
253
326
|
function getEvents(
|
|
254
327
|
eventTypeAST: $ReadOnlyArray<EventTypeAST>,
|
|
255
328
|
types: TypeDeclarationMap,
|
|
329
|
+
parser: Parser,
|
|
256
330
|
): $ReadOnlyArray<EventTypeShape> {
|
|
257
|
-
return eventTypeAST
|
|
331
|
+
return eventTypeAST
|
|
332
|
+
.map(property => buildEventSchema(types, property, parser))
|
|
333
|
+
.filter(Boolean);
|
|
258
334
|
}
|
|
259
335
|
|
|
260
336
|
module.exports = {
|
|
@@ -12,28 +12,6 @@
|
|
|
12
12
|
|
|
13
13
|
const _require = require('../parseTopLevelType'),
|
|
14
14
|
parseTopLevelType = _require.parseTopLevelType;
|
|
15
|
-
const _require2 = require('./componentsUtils.js'),
|
|
16
|
-
flattenProperties = _require2.flattenProperties;
|
|
17
|
-
function extendsForProp(prop, types) {
|
|
18
|
-
if (!prop.expression) {
|
|
19
|
-
console.log('null', prop);
|
|
20
|
-
}
|
|
21
|
-
const name = prop.expression.name;
|
|
22
|
-
if (types[name] != null) {
|
|
23
|
-
// This type is locally defined in the file
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
switch (name) {
|
|
27
|
-
case 'ViewProps':
|
|
28
|
-
return {
|
|
29
|
-
type: 'ReactNativeBuiltInType',
|
|
30
|
-
knownTypeName: 'ReactNativeCoreViewProps',
|
|
31
|
-
};
|
|
32
|
-
default: {
|
|
33
|
-
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
15
|
function isEvent(typeAnnotation) {
|
|
38
16
|
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
39
17
|
return false;
|
|
@@ -41,35 +19,12 @@ function isEvent(typeAnnotation) {
|
|
|
41
19
|
const eventNames = new Set(['BubblingEventHandler', 'DirectEventHandler']);
|
|
42
20
|
return eventNames.has(typeAnnotation.typeName.name);
|
|
43
21
|
}
|
|
44
|
-
function isProp(name, typeAnnotation) {
|
|
45
|
-
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
const isStyle =
|
|
49
|
-
name === 'style' &&
|
|
50
|
-
typeAnnotation.type === 'GenericTypeAnnotation' &&
|
|
51
|
-
typeAnnotation.typeName.name === 'ViewStyleProp';
|
|
52
|
-
return !isStyle;
|
|
53
|
-
}
|
|
54
22
|
|
|
55
23
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
56
24
|
|
|
57
|
-
function categorizeProps(typeDefinition, types,
|
|
58
|
-
|
|
25
|
+
function categorizeProps(typeDefinition, types, events) {
|
|
26
|
+
// find events
|
|
59
27
|
for (const prop of typeDefinition) {
|
|
60
|
-
// find extends
|
|
61
|
-
if (prop.type === 'TSExpressionWithTypeArguments') {
|
|
62
|
-
const extend = extendsForProp(prop, types);
|
|
63
|
-
if (extend) {
|
|
64
|
-
extendsProps.push(extend);
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
remaining.push(prop);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// find events and props
|
|
72
|
-
for (const prop of flattenProperties(remaining, types)) {
|
|
73
28
|
if (prop.type === 'TSPropertySignature') {
|
|
74
29
|
const topLevelType = parseTopLevelType(
|
|
75
30
|
prop.typeAnnotation.typeAnnotation,
|
|
@@ -77,8 +32,6 @@ function categorizeProps(typeDefinition, types, extendsProps, props, events) {
|
|
|
77
32
|
);
|
|
78
33
|
if (isEvent(topLevelType.type)) {
|
|
79
34
|
events.push(prop);
|
|
80
|
-
} else if (isProp(prop.key.name, prop)) {
|
|
81
|
-
props.push(prop);
|
|
82
35
|
}
|
|
83
36
|
}
|
|
84
37
|
}
|
|
@@ -10,33 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
'use strict';
|
|
12
12
|
|
|
13
|
-
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
|
|
14
13
|
import type {TypeDeclarationMap} from '../../utils';
|
|
15
14
|
const {parseTopLevelType} = require('../parseTopLevelType');
|
|
16
|
-
const {flattenProperties} = require('./componentsUtils.js');
|
|
17
|
-
|
|
18
|
-
function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) {
|
|
19
|
-
if (!prop.expression) {
|
|
20
|
-
console.log('null', prop);
|
|
21
|
-
}
|
|
22
|
-
const name = prop.expression.name;
|
|
23
|
-
|
|
24
|
-
if (types[name] != null) {
|
|
25
|
-
// This type is locally defined in the file
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
switch (name) {
|
|
30
|
-
case 'ViewProps':
|
|
31
|
-
return {
|
|
32
|
-
type: 'ReactNativeBuiltInType',
|
|
33
|
-
knownTypeName: 'ReactNativeCoreViewProps',
|
|
34
|
-
};
|
|
35
|
-
default: {
|
|
36
|
-
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
15
|
|
|
41
16
|
function isEvent(typeAnnotation: $FlowFixMe): boolean {
|
|
42
17
|
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
@@ -46,43 +21,16 @@ function isEvent(typeAnnotation: $FlowFixMe): boolean {
|
|
|
46
21
|
return eventNames.has(typeAnnotation.typeName.name);
|
|
47
22
|
}
|
|
48
23
|
|
|
49
|
-
function isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
|
|
50
|
-
if (typeAnnotation.type !== 'TSTypeReference') {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
const isStyle =
|
|
54
|
-
name === 'style' &&
|
|
55
|
-
typeAnnotation.type === 'GenericTypeAnnotation' &&
|
|
56
|
-
typeAnnotation.typeName.name === 'ViewStyleProp';
|
|
57
|
-
return !isStyle;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
24
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
61
25
|
type PropsAST = Object;
|
|
62
26
|
|
|
63
27
|
function categorizeProps(
|
|
64
28
|
typeDefinition: $ReadOnlyArray<PropsAST>,
|
|
65
29
|
types: TypeDeclarationMap,
|
|
66
|
-
extendsProps: Array<ExtendsPropsShape>,
|
|
67
|
-
props: Array<PropsAST>,
|
|
68
30
|
events: Array<PropsAST>,
|
|
69
31
|
): void {
|
|
70
|
-
|
|
32
|
+
// find events
|
|
71
33
|
for (const prop of typeDefinition) {
|
|
72
|
-
// find extends
|
|
73
|
-
if (prop.type === 'TSExpressionWithTypeArguments') {
|
|
74
|
-
const extend = extendsForProp(prop, types);
|
|
75
|
-
if (extend) {
|
|
76
|
-
extendsProps.push(extend);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
remaining.push(prop);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// find events and props
|
|
85
|
-
for (const prop of flattenProperties(remaining, types)) {
|
|
86
34
|
if (prop.type === 'TSPropertySignature') {
|
|
87
35
|
const topLevelType = parseTopLevelType(
|
|
88
36
|
prop.typeAnnotation.typeAnnotation,
|
|
@@ -91,8 +39,6 @@ function categorizeProps(
|
|
|
91
39
|
|
|
92
40
|
if (isEvent(topLevelType.type)) {
|
|
93
41
|
events.push(prop);
|
|
94
|
-
} else if (isProp(prop.key.name, prop)) {
|
|
95
|
-
props.push(prop);
|
|
96
42
|
}
|
|
97
43
|
}
|
|
98
44
|
}
|
|
@@ -16,120 +16,10 @@ const _require2 = require('./events'),
|
|
|
16
16
|
getEvents = _require2.getEvents;
|
|
17
17
|
const _require3 = require('./extends'),
|
|
18
18
|
categorizeProps = _require3.categorizeProps;
|
|
19
|
-
const _require4 = require('
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const _require6 = require('../../error-utils'),
|
|
24
|
-
throwIfMoreThanOneCodegenNativecommands =
|
|
25
|
-
_require6.throwIfMoreThanOneCodegenNativecommands;
|
|
26
|
-
const _require7 = require('../../parsers-commons'),
|
|
27
|
-
createComponentConfig = _require7.createComponentConfig,
|
|
28
|
-
findNativeComponentType = _require7.findNativeComponentType,
|
|
29
|
-
getCommandOptions = _require7.getCommandOptions,
|
|
30
|
-
getOptions = _require7.getOptions;
|
|
31
|
-
|
|
32
|
-
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
|
|
33
|
-
function findComponentConfig(ast, parser) {
|
|
34
|
-
const foundConfigs = [];
|
|
35
|
-
const defaultExports = ast.body.filter(
|
|
36
|
-
node => node.type === 'ExportDefaultDeclaration',
|
|
37
|
-
);
|
|
38
|
-
defaultExports.forEach(statement =>
|
|
39
|
-
findNativeComponentType(statement, foundConfigs, parser),
|
|
40
|
-
);
|
|
41
|
-
if (foundConfigs.length === 0) {
|
|
42
|
-
throw new Error('Could not find component config for native component');
|
|
43
|
-
}
|
|
44
|
-
if (foundConfigs.length > 1) {
|
|
45
|
-
throw new Error('Only one component is supported per file');
|
|
46
|
-
}
|
|
47
|
-
const foundConfig = foundConfigs[0];
|
|
48
|
-
const namedExports = ast.body.filter(
|
|
49
|
-
node => node.type === 'ExportNamedDeclaration',
|
|
50
|
-
);
|
|
51
|
-
const commandsTypeNames = namedExports
|
|
52
|
-
.map(statement => {
|
|
53
|
-
let callExpression;
|
|
54
|
-
let calleeName;
|
|
55
|
-
try {
|
|
56
|
-
callExpression = statement.declaration.declarations[0].init;
|
|
57
|
-
calleeName = callExpression.callee.name;
|
|
58
|
-
} catch (e) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (calleeName !== 'codegenNativeCommands') {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// const statement.declaration.declarations[0].init
|
|
66
|
-
if (callExpression.arguments.length !== 1) {
|
|
67
|
-
throw new Error(
|
|
68
|
-
'codegenNativeCommands must be passed options including the supported commands',
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
const typeArgumentParam = callExpression.typeParameters.params[0];
|
|
72
|
-
if (typeArgumentParam.type !== 'TSTypeReference') {
|
|
73
|
-
throw new Error(
|
|
74
|
-
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
commandTypeName: typeArgumentParam.typeName.name,
|
|
79
|
-
commandOptionsExpression: callExpression.arguments[0],
|
|
80
|
-
};
|
|
81
|
-
})
|
|
82
|
-
.filter(Boolean);
|
|
83
|
-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
84
|
-
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
85
|
-
}
|
|
86
|
-
function getCommandProperties(
|
|
87
|
-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
88
|
-
* LTI update could not be added via codemod */
|
|
89
|
-
commandTypeName,
|
|
90
|
-
types,
|
|
91
|
-
commandOptions,
|
|
92
|
-
) {
|
|
93
|
-
if (commandTypeName == null) {
|
|
94
|
-
return [];
|
|
95
|
-
}
|
|
96
|
-
const typeAlias = types[commandTypeName];
|
|
97
|
-
if (typeAlias.type !== 'TSInterfaceDeclaration') {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
let properties;
|
|
103
|
-
try {
|
|
104
|
-
properties = typeAlias.body.body;
|
|
105
|
-
} catch (e) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
const typeScriptPropertyNames = properties
|
|
111
|
-
.map(property => property && property.key && property.key.name)
|
|
112
|
-
.filter(Boolean);
|
|
113
|
-
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
114
|
-
throw new Error(
|
|
115
|
-
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
if (
|
|
119
|
-
commandOptions.supportedCommands.length !==
|
|
120
|
-
typeScriptPropertyNames.length ||
|
|
121
|
-
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
122
|
-
typeScriptPropertyNames.includes(supportedCommand),
|
|
123
|
-
)
|
|
124
|
-
) {
|
|
125
|
-
throw new Error(
|
|
126
|
-
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${typeScriptPropertyNames.join(
|
|
127
|
-
', ',
|
|
128
|
-
)}`,
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
return properties;
|
|
132
|
-
}
|
|
19
|
+
const _require4 = require('../../parsers-commons'),
|
|
20
|
+
getOptions = _require4.getOptions,
|
|
21
|
+
findComponentConfig = _require4.findComponentConfig,
|
|
22
|
+
getCommandProperties = _require4.getCommandProperties;
|
|
133
23
|
|
|
134
24
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
135
25
|
|
|
@@ -138,30 +28,17 @@ function buildComponentSchema(ast, parser) {
|
|
|
138
28
|
const _findComponentConfig = findComponentConfig(ast, parser),
|
|
139
29
|
componentName = _findComponentConfig.componentName,
|
|
140
30
|
propsTypeName = _findComponentConfig.propsTypeName,
|
|
141
|
-
commandTypeName = _findComponentConfig.commandTypeName,
|
|
142
|
-
commandOptionsExpression = _findComponentConfig.commandOptionsExpression,
|
|
143
31
|
optionsExpression = _findComponentConfig.optionsExpression;
|
|
144
32
|
const types = parser.getTypes(ast);
|
|
145
|
-
const propProperties = getProperties(propsTypeName, types);
|
|
146
|
-
const
|
|
147
|
-
const commandProperties = getCommandProperties(
|
|
148
|
-
commandTypeName,
|
|
149
|
-
types,
|
|
150
|
-
commandOptions,
|
|
151
|
-
);
|
|
33
|
+
const propProperties = parser.getProperties(propsTypeName, types);
|
|
34
|
+
const commandProperties = getCommandProperties(ast, parser);
|
|
152
35
|
const options = getOptions(optionsExpression);
|
|
153
|
-
const extendsProps = [];
|
|
154
|
-
const componentPropAsts = [];
|
|
155
36
|
const componentEventAsts = [];
|
|
156
|
-
categorizeProps(
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
extendsProps
|
|
160
|
-
|
|
161
|
-
componentEventAsts,
|
|
162
|
-
);
|
|
163
|
-
const props = getProps(componentPropAsts, types);
|
|
164
|
-
const events = getEvents(componentEventAsts, types);
|
|
37
|
+
categorizeProps(propProperties, types, componentEventAsts);
|
|
38
|
+
const _parser$getProps = parser.getProps(propProperties, types),
|
|
39
|
+
props = _parser$getProps.props,
|
|
40
|
+
extendsProps = _parser$getProps.extendsProps;
|
|
41
|
+
const events = getEvents(componentEventAsts, types, parser);
|
|
165
42
|
const commands = getCommands(commandProperties, types);
|
|
166
43
|
return {
|
|
167
44
|
filename: componentName,
|