@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
|
@@ -16,9 +16,30 @@ 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 {
|
|
28
|
+
getEventArgument,
|
|
29
|
+
buildPropertiesForEvent,
|
|
30
|
+
handleEventHandler,
|
|
31
|
+
emitBuildEventSchema,
|
|
32
|
+
} = require('../../parsers-commons');
|
|
33
|
+
const {
|
|
34
|
+
emitBoolProp,
|
|
35
|
+
emitDoubleProp,
|
|
36
|
+
emitFloatProp,
|
|
37
|
+
emitMixedProp,
|
|
38
|
+
emitStringProp,
|
|
39
|
+
emitInt32Prop,
|
|
40
|
+
emitObjectProp,
|
|
41
|
+
emitUnionProp,
|
|
42
|
+
} = require('../../parsers-primitives');
|
|
22
43
|
function getPropertyType(
|
|
23
44
|
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
24
45
|
* LTI update could not be added via codemod */
|
|
@@ -27,82 +48,128 @@ function getPropertyType(
|
|
|
27
48
|
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
28
49
|
* LTI update could not be added via codemod */
|
|
29
50
|
annotation,
|
|
51
|
+
parser: Parser,
|
|
30
52
|
): NamedShape<EventTypeAnnotation> {
|
|
31
53
|
const topLevelType = parseTopLevelType(annotation);
|
|
32
54
|
const typeAnnotation = topLevelType.type;
|
|
33
55
|
const optional = optionalProperty || topLevelType.optional;
|
|
34
56
|
const type =
|
|
35
57
|
typeAnnotation.type === 'TSTypeReference'
|
|
36
|
-
? typeAnnotation
|
|
58
|
+
? parser.getTypeAnnotationName(typeAnnotation)
|
|
37
59
|
: typeAnnotation.type;
|
|
38
60
|
|
|
39
61
|
switch (type) {
|
|
40
62
|
case 'TSBooleanKeyword':
|
|
63
|
+
return emitBoolProp(name, optional);
|
|
64
|
+
case 'TSStringKeyword':
|
|
65
|
+
return emitStringProp(name, optional);
|
|
66
|
+
case 'Int32':
|
|
67
|
+
return emitInt32Prop(name, optional);
|
|
68
|
+
case 'Double':
|
|
69
|
+
return emitDoubleProp(name, optional);
|
|
70
|
+
case 'Float':
|
|
71
|
+
return emitFloatProp(name, optional);
|
|
72
|
+
case 'TSTypeLiteral':
|
|
73
|
+
return emitObjectProp(
|
|
74
|
+
name,
|
|
75
|
+
optional,
|
|
76
|
+
parser,
|
|
77
|
+
typeAnnotation,
|
|
78
|
+
extractArrayElementType,
|
|
79
|
+
);
|
|
80
|
+
case 'TSUnionType':
|
|
81
|
+
return emitUnionProp(name, optional, parser, typeAnnotation);
|
|
82
|
+
case 'UnsafeMixed':
|
|
83
|
+
return emitMixedProp(name, optional);
|
|
84
|
+
case 'TSArrayType':
|
|
41
85
|
return {
|
|
42
86
|
name,
|
|
43
87
|
optional,
|
|
44
|
-
typeAnnotation:
|
|
45
|
-
type: 'BooleanTypeAnnotation',
|
|
46
|
-
},
|
|
88
|
+
typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
|
|
47
89
|
};
|
|
90
|
+
default:
|
|
91
|
+
throw new Error(`Unable to determine event type for "${name}": ${type}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function extractArrayElementType(
|
|
96
|
+
typeAnnotation: $FlowFixMe,
|
|
97
|
+
name: string,
|
|
98
|
+
parser: Parser,
|
|
99
|
+
): EventTypeAnnotation {
|
|
100
|
+
const type = extractTypeFromTypeAnnotation(typeAnnotation, parser);
|
|
101
|
+
|
|
102
|
+
switch (type) {
|
|
103
|
+
case 'TSParenthesizedType':
|
|
104
|
+
return extractArrayElementType(
|
|
105
|
+
typeAnnotation.typeAnnotation,
|
|
106
|
+
name,
|
|
107
|
+
parser,
|
|
108
|
+
);
|
|
109
|
+
case 'TSBooleanKeyword':
|
|
110
|
+
return {type: 'BooleanTypeAnnotation'};
|
|
48
111
|
case 'TSStringKeyword':
|
|
112
|
+
return {type: 'StringTypeAnnotation'};
|
|
113
|
+
case 'Float':
|
|
49
114
|
return {
|
|
50
|
-
|
|
51
|
-
optional,
|
|
52
|
-
typeAnnotation: {
|
|
53
|
-
type: 'StringTypeAnnotation',
|
|
54
|
-
},
|
|
115
|
+
type: 'FloatTypeAnnotation',
|
|
55
116
|
};
|
|
56
117
|
case 'Int32':
|
|
57
118
|
return {
|
|
58
|
-
|
|
59
|
-
optional,
|
|
60
|
-
typeAnnotation: {
|
|
61
|
-
type: 'Int32TypeAnnotation',
|
|
62
|
-
},
|
|
119
|
+
type: 'Int32TypeAnnotation',
|
|
63
120
|
};
|
|
121
|
+
case 'TSNumberKeyword':
|
|
64
122
|
case 'Double':
|
|
65
123
|
return {
|
|
66
|
-
|
|
67
|
-
optional,
|
|
68
|
-
typeAnnotation: {
|
|
69
|
-
type: 'DoubleTypeAnnotation',
|
|
70
|
-
},
|
|
124
|
+
type: 'DoubleTypeAnnotation',
|
|
71
125
|
};
|
|
72
|
-
case '
|
|
126
|
+
case 'TSUnionType':
|
|
73
127
|
return {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
},
|
|
128
|
+
type: 'StringEnumTypeAnnotation',
|
|
129
|
+
options: typeAnnotation.types.map(option =>
|
|
130
|
+
parser.getLiteralValue(option),
|
|
131
|
+
),
|
|
79
132
|
};
|
|
80
133
|
case 'TSTypeLiteral':
|
|
81
134
|
return {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
135
|
+
type: 'ObjectTypeAnnotation',
|
|
136
|
+
properties: parser
|
|
137
|
+
.getObjectProperties(typeAnnotation)
|
|
138
|
+
.map(member =>
|
|
139
|
+
buildPropertiesForEvent(member, parser, getPropertyType),
|
|
140
|
+
),
|
|
88
141
|
};
|
|
89
|
-
|
|
90
|
-
case 'TSUnionType':
|
|
142
|
+
case 'TSArrayType':
|
|
91
143
|
return {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
144
|
+
type: 'ArrayTypeAnnotation',
|
|
145
|
+
elementType: extractArrayElementType(
|
|
146
|
+
typeAnnotation.elementType,
|
|
147
|
+
name,
|
|
148
|
+
parser,
|
|
149
|
+
),
|
|
98
150
|
};
|
|
99
151
|
default:
|
|
100
|
-
(
|
|
101
|
-
|
|
152
|
+
throw new Error(
|
|
153
|
+
`Unrecognized ${type} for Array ${name} in events.\n${JSON.stringify(
|
|
154
|
+
typeAnnotation,
|
|
155
|
+
null,
|
|
156
|
+
2,
|
|
157
|
+
)}`,
|
|
158
|
+
);
|
|
102
159
|
}
|
|
103
160
|
}
|
|
104
161
|
|
|
162
|
+
function extractTypeFromTypeAnnotation(
|
|
163
|
+
typeAnnotation: $FlowFixMe,
|
|
164
|
+
parser: Parser,
|
|
165
|
+
): string {
|
|
166
|
+
return typeAnnotation.type === 'TSTypeReference'
|
|
167
|
+
? parser.getTypeAnnotationName(typeAnnotation)
|
|
168
|
+
: typeAnnotation.type;
|
|
169
|
+
}
|
|
170
|
+
|
|
105
171
|
function findEventArgumentsAndType(
|
|
172
|
+
parser: Parser,
|
|
106
173
|
typeAnnotation: $FlowFixMe,
|
|
107
174
|
types: TypeDeclarationMap,
|
|
108
175
|
bubblingType: void | 'direct' | 'bubble',
|
|
@@ -114,7 +181,7 @@ function findEventArgumentsAndType(
|
|
|
114
181
|
} {
|
|
115
182
|
if (typeAnnotation.type === 'TSInterfaceDeclaration') {
|
|
116
183
|
return {
|
|
117
|
-
argumentProps: flattenProperties([typeAnnotation], types),
|
|
184
|
+
argumentProps: flattenProperties([typeAnnotation], types, parser),
|
|
118
185
|
paperTopLevelNameDeprecated: paperName,
|
|
119
186
|
bubblingType,
|
|
120
187
|
};
|
|
@@ -122,52 +189,37 @@ function findEventArgumentsAndType(
|
|
|
122
189
|
|
|
123
190
|
if (typeAnnotation.type === 'TSTypeLiteral') {
|
|
124
191
|
return {
|
|
125
|
-
argumentProps: typeAnnotation
|
|
192
|
+
argumentProps: parser.getObjectProperties(typeAnnotation),
|
|
126
193
|
paperTopLevelNameDeprecated: paperName,
|
|
127
194
|
bubblingType,
|
|
128
195
|
};
|
|
129
196
|
}
|
|
130
197
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
const name = typeAnnotation.typeName.name;
|
|
198
|
+
throwIfEventHasNoName(typeAnnotation, parser);
|
|
199
|
+
const name = parser.getTypeAnnotationName(typeAnnotation);
|
|
135
200
|
if (name === 'Readonly') {
|
|
136
201
|
return findEventArgumentsAndType(
|
|
202
|
+
parser,
|
|
137
203
|
typeAnnotation.typeParameters.params[0],
|
|
138
204
|
types,
|
|
139
205
|
bubblingType,
|
|
140
206
|
paperName,
|
|
141
207
|
);
|
|
142
208
|
} else if (name === 'BubblingEventHandler' || name === 'DirectEventHandler') {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
typeAnnotation
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
case 'TSNullKeyword':
|
|
151
|
-
case 'TSUndefinedKeyword':
|
|
152
|
-
return {
|
|
153
|
-
argumentProps: [],
|
|
154
|
-
bubblingType: eventType,
|
|
155
|
-
paperTopLevelNameDeprecated,
|
|
156
|
-
};
|
|
157
|
-
default:
|
|
158
|
-
return findEventArgumentsAndType(
|
|
159
|
-
typeAnnotation.typeParameters.params[0],
|
|
160
|
-
types,
|
|
161
|
-
eventType,
|
|
162
|
-
paperTopLevelNameDeprecated,
|
|
163
|
-
);
|
|
164
|
-
}
|
|
209
|
+
return handleEventHandler(
|
|
210
|
+
name,
|
|
211
|
+
typeAnnotation,
|
|
212
|
+
parser,
|
|
213
|
+
types,
|
|
214
|
+
findEventArgumentsAndType,
|
|
215
|
+
);
|
|
165
216
|
} else if (types[name]) {
|
|
166
217
|
let elementType = types[name];
|
|
167
218
|
if (elementType.type === 'TSTypeAliasDeclaration') {
|
|
168
219
|
elementType = elementType.typeAnnotation;
|
|
169
220
|
}
|
|
170
221
|
return findEventArgumentsAndType(
|
|
222
|
+
parser,
|
|
171
223
|
elementType,
|
|
172
224
|
types,
|
|
173
225
|
bubblingType,
|
|
@@ -182,32 +234,14 @@ function findEventArgumentsAndType(
|
|
|
182
234
|
}
|
|
183
235
|
}
|
|
184
236
|
|
|
185
|
-
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
186
|
-
* LTI update could not be added via codemod */
|
|
187
|
-
function buildPropertiesForEvent(property): NamedShape<EventTypeAnnotation> {
|
|
188
|
-
const name = property.key.name;
|
|
189
|
-
const optional = property.optional || false;
|
|
190
|
-
let typeAnnotation = property.typeAnnotation.typeAnnotation;
|
|
191
|
-
|
|
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
|
-
};
|
|
202
|
-
}
|
|
203
|
-
|
|
204
237
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
205
238
|
type EventTypeAST = Object;
|
|
206
239
|
|
|
207
240
|
function buildEventSchema(
|
|
208
241
|
types: TypeDeclarationMap,
|
|
209
242
|
property: EventTypeAST,
|
|
210
|
-
|
|
243
|
+
parser: Parser,
|
|
244
|
+
): ?EventTypeShape {
|
|
211
245
|
// unpack WithDefault, (T) or T|U
|
|
212
246
|
const topLevelType = parseTopLevelType(
|
|
213
247
|
property.typeAnnotation.typeAnnotation,
|
|
@@ -218,45 +252,40 @@ function buildEventSchema(
|
|
|
218
252
|
const typeAnnotation = topLevelType.type;
|
|
219
253
|
const optional = property.optional || topLevelType.optional;
|
|
220
254
|
const {argumentProps, bubblingType, paperTopLevelNameDeprecated} =
|
|
221
|
-
findEventArgumentsAndType(typeAnnotation, types);
|
|
255
|
+
findEventArgumentsAndType(parser, typeAnnotation, types);
|
|
222
256
|
|
|
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
|
-
}
|
|
257
|
+
const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
|
|
258
|
+
argumentProps,
|
|
259
|
+
name,
|
|
260
|
+
);
|
|
261
|
+
const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
|
|
240
262
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
263
|
+
const argument = getEventArgument(
|
|
264
|
+
nonNullableArgumentProps,
|
|
265
|
+
parser,
|
|
266
|
+
getPropertyType,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return emitBuildEventSchema(
|
|
270
|
+
paperTopLevelNameDeprecated,
|
|
271
|
+
name,
|
|
272
|
+
optional,
|
|
273
|
+
nonNullableBubblingType,
|
|
274
|
+
argument,
|
|
275
|
+
);
|
|
251
276
|
}
|
|
252
277
|
|
|
253
278
|
function getEvents(
|
|
254
279
|
eventTypeAST: $ReadOnlyArray<EventTypeAST>,
|
|
255
280
|
types: TypeDeclarationMap,
|
|
281
|
+
parser: Parser,
|
|
256
282
|
): $ReadOnlyArray<EventTypeShape> {
|
|
257
|
-
return eventTypeAST
|
|
283
|
+
return eventTypeAST
|
|
284
|
+
.map(property => buildEventSchema(types, property, parser))
|
|
285
|
+
.filter(Boolean);
|
|
258
286
|
}
|
|
259
287
|
|
|
260
288
|
module.exports = {
|
|
261
289
|
getEvents,
|
|
290
|
+
extractArrayElementType,
|
|
262
291
|
};
|
|
@@ -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,114 +16,29 @@ 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
|
-
propertyNames = _require7.propertyNames,
|
|
30
|
-
getCommandOptions = _require7.getCommandOptions,
|
|
31
|
-
getOptions = _require7.getOptions,
|
|
32
|
-
getCommandTypeNameAndOptionsExpression =
|
|
33
|
-
_require7.getCommandTypeNameAndOptionsExpression;
|
|
34
|
-
|
|
35
|
-
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
|
|
36
|
-
function findComponentConfig(ast, parser) {
|
|
37
|
-
const foundConfigs = [];
|
|
38
|
-
const defaultExports = ast.body.filter(
|
|
39
|
-
node => node.type === 'ExportDefaultDeclaration',
|
|
40
|
-
);
|
|
41
|
-
defaultExports.forEach(statement =>
|
|
42
|
-
findNativeComponentType(statement, foundConfigs, parser),
|
|
43
|
-
);
|
|
44
|
-
if (foundConfigs.length === 0) {
|
|
45
|
-
throw new Error('Could not find component config for native component');
|
|
46
|
-
}
|
|
47
|
-
if (foundConfigs.length > 1) {
|
|
48
|
-
throw new Error('Only one component is supported per file');
|
|
49
|
-
}
|
|
50
|
-
const foundConfig = foundConfigs[0];
|
|
51
|
-
const namedExports = ast.body.filter(
|
|
52
|
-
node => node.type === 'ExportNamedDeclaration',
|
|
53
|
-
);
|
|
54
|
-
const commandsTypeNames = namedExports
|
|
55
|
-
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
|
|
56
|
-
.filter(Boolean);
|
|
57
|
-
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
58
|
-
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
59
|
-
}
|
|
60
|
-
function getCommandProperties(ast, parser) {
|
|
61
|
-
const _findComponentConfig = findComponentConfig(ast, parser),
|
|
62
|
-
commandTypeName = _findComponentConfig.commandTypeName,
|
|
63
|
-
commandOptionsExpression = _findComponentConfig.commandOptionsExpression;
|
|
64
|
-
if (commandTypeName == null) {
|
|
65
|
-
return [];
|
|
66
|
-
}
|
|
67
|
-
const types = parser.getTypes(ast);
|
|
68
|
-
const typeAlias = types[commandTypeName];
|
|
69
|
-
if (typeAlias.type !== 'TSInterfaceDeclaration') {
|
|
70
|
-
throw new Error(
|
|
71
|
-
`The type argument for codegenNativeCommands must be an interface, received ${typeAlias.type}`,
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
const properties = parser.bodyProperties(typeAlias);
|
|
75
|
-
if (!properties) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`Failed to find type definition for "${commandTypeName}", please check that you have a valid codegen typescript file`,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
const typeScriptPropertyNames = propertyNames(properties);
|
|
81
|
-
const commandOptions = getCommandOptions(commandOptionsExpression);
|
|
82
|
-
if (commandOptions == null || commandOptions.supportedCommands == null) {
|
|
83
|
-
throw new Error(
|
|
84
|
-
'codegenNativeCommands must be given an options object with supportedCommands array',
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
if (
|
|
88
|
-
commandOptions.supportedCommands.length !==
|
|
89
|
-
typeScriptPropertyNames.length ||
|
|
90
|
-
!commandOptions.supportedCommands.every(supportedCommand =>
|
|
91
|
-
typeScriptPropertyNames.includes(supportedCommand),
|
|
92
|
-
)
|
|
93
|
-
) {
|
|
94
|
-
throw new Error(
|
|
95
|
-
`codegenNativeCommands expected the same supportedCommands specified in the ${commandTypeName} interface: ${typeScriptPropertyNames.join(
|
|
96
|
-
', ',
|
|
97
|
-
)}`,
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
return properties;
|
|
101
|
-
}
|
|
19
|
+
const _require4 = require('../../parsers-commons'),
|
|
20
|
+
getOptions = _require4.getOptions,
|
|
21
|
+
findComponentConfig = _require4.findComponentConfig,
|
|
22
|
+
getCommandProperties = _require4.getCommandProperties;
|
|
102
23
|
|
|
103
24
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
104
25
|
|
|
105
26
|
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
|
|
106
27
|
function buildComponentSchema(ast, parser) {
|
|
107
|
-
const
|
|
108
|
-
componentName =
|
|
109
|
-
propsTypeName =
|
|
110
|
-
optionsExpression =
|
|
28
|
+
const _findComponentConfig = findComponentConfig(ast, parser),
|
|
29
|
+
componentName = _findComponentConfig.componentName,
|
|
30
|
+
propsTypeName = _findComponentConfig.propsTypeName,
|
|
31
|
+
optionsExpression = _findComponentConfig.optionsExpression;
|
|
111
32
|
const types = parser.getTypes(ast);
|
|
112
|
-
const propProperties = getProperties(propsTypeName, types);
|
|
33
|
+
const propProperties = parser.getProperties(propsTypeName, types);
|
|
113
34
|
const commandProperties = getCommandProperties(ast, parser);
|
|
114
35
|
const options = getOptions(optionsExpression);
|
|
115
|
-
const extendsProps = [];
|
|
116
|
-
const componentPropAsts = [];
|
|
117
36
|
const componentEventAsts = [];
|
|
118
|
-
categorizeProps(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
extendsProps
|
|
122
|
-
|
|
123
|
-
componentEventAsts,
|
|
124
|
-
);
|
|
125
|
-
const props = getProps(componentPropAsts, types);
|
|
126
|
-
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);
|
|
127
42
|
const commands = getCommands(commandProperties, types);
|
|
128
43
|
return {
|
|
129
44
|
filename: componentName,
|