@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.
Files changed (72) hide show
  1. package/README.md +2 -2
  2. package/lib/CodegenSchema.d.ts +15 -4
  3. package/lib/CodegenSchema.js.flow +33 -21
  4. package/lib/generators/components/CppHelpers.js +39 -2
  5. package/lib/generators/components/CppHelpers.js.flow +54 -5
  6. package/lib/generators/components/GenerateEventEmitterCpp.js +238 -55
  7. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +233 -55
  8. package/lib/generators/components/GenerateEventEmitterH.js +77 -28
  9. package/lib/generators/components/GenerateEventEmitterH.js.flow +87 -32
  10. package/lib/generators/components/GeneratePropsH.js +1 -2
  11. package/lib/generators/components/GeneratePropsH.js.flow +1 -2
  12. package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +0 -1
  13. package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -5
  14. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +4 -0
  15. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +4 -0
  16. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +2 -0
  17. package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +2 -0
  18. package/lib/generators/components/__test_fixtures__/fixtures.js +114 -1
  19. package/lib/generators/components/__test_fixtures__/fixtures.js.flow +114 -1
  20. package/lib/generators/modules/GenerateModuleCpp.js +3 -3
  21. package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
  22. package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
  23. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
  24. package/lib/parsers/error-utils.js +44 -0
  25. package/lib/parsers/error-utils.js.flow +59 -0
  26. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +51 -0
  27. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +51 -0
  28. package/lib/parsers/flow/components/componentsUtils.js +12 -19
  29. package/lib/parsers/flow/components/componentsUtils.js.flow +16 -27
  30. package/lib/parsers/flow/components/events.js +157 -83
  31. package/lib/parsers/flow/components/events.js.flow +158 -82
  32. package/lib/parsers/flow/components/index.js +10 -129
  33. package/lib/parsers/flow/components/index.js.flow +10 -150
  34. package/lib/parsers/flow/modules/index.js +39 -49
  35. package/lib/parsers/flow/modules/index.js.flow +22 -31
  36. package/lib/parsers/flow/parser.d.ts +9 -2
  37. package/lib/parsers/flow/parser.js +177 -16
  38. package/lib/parsers/flow/parser.js.flow +220 -6
  39. package/lib/parsers/flow/utils.js +0 -63
  40. package/lib/parsers/flow/utils.js.flow +1 -77
  41. package/lib/parsers/parser.js.flow +143 -1
  42. package/lib/parsers/parserMock.js +175 -2
  43. package/lib/parsers/parserMock.js.flow +227 -2
  44. package/lib/parsers/parsers-commons.js +169 -9
  45. package/lib/parsers/parsers-commons.js.flow +233 -12
  46. package/lib/parsers/parsers-primitives.js +94 -35
  47. package/lib/parsers/parsers-primitives.js.flow +124 -35
  48. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +51 -0
  49. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +51 -0
  50. package/lib/parsers/typescript/components/componentsUtils.js +44 -46
  51. package/lib/parsers/typescript/components/componentsUtils.js.flow +44 -51
  52. package/lib/parsers/typescript/components/events.js +142 -72
  53. package/lib/parsers/typescript/components/events.js.flow +149 -73
  54. package/lib/parsers/typescript/components/extends.js +2 -49
  55. package/lib/parsers/typescript/components/extends.js.flow +1 -55
  56. package/lib/parsers/typescript/components/index.js +11 -134
  57. package/lib/parsers/typescript/components/index.js.flow +11 -156
  58. package/lib/parsers/typescript/modules/index.js +43 -51
  59. package/lib/parsers/typescript/modules/index.js.flow +25 -32
  60. package/lib/parsers/typescript/parser.d.ts +9 -2
  61. package/lib/parsers/typescript/parser.js +224 -7
  62. package/lib/parsers/typescript/parser.js.flow +262 -6
  63. package/lib/parsers/utils.js.flow +6 -0
  64. package/package.json +16 -5
  65. package/lib/parsers/flow/components/extends.js +0 -52
  66. package/lib/parsers/flow/components/extends.js.flow +0 -66
  67. package/lib/parsers/flow/components/props.js +0 -47
  68. package/lib/parsers/flow/components/props.js.flow +0 -60
  69. package/lib/parsers/typescript/components/props.js +0 -39
  70. package/lib/parsers/typescript/components/props.js.flow +0 -48
  71. package/lib/parsers/typescript/utils.js +0 -90
  72. package/lib/parsers/typescript/utils.js.flow +0 -104
@@ -9,43 +9,12 @@
9
9
  */
10
10
 
11
11
  'use strict';
12
- import type {ASTNode} from '../utils';
13
- import type {NamedShape} from '../../../CodegenSchema.js';
14
12
  const {
15
13
  parseTopLevelType,
16
14
  flattenIntersectionType,
17
15
  } = require('../parseTopLevelType');
18
- import type {TypeDeclarationMap} from '../../utils';
19
-
20
- function getProperties(
21
- typeName: string,
22
- types: TypeDeclarationMap,
23
- ): $FlowFixMe {
24
- const alias = types[typeName];
25
- if (!alias) {
26
- throw new Error(
27
- `Failed to find definition for "${typeName}", please check that you have a valid codegen typescript file`,
28
- );
29
- }
30
- const aliasKind =
31
- alias.type === 'TSInterfaceDeclaration' ? 'interface' : 'type';
32
-
33
- try {
34
- if (aliasKind === 'interface') {
35
- return [...(alias.extends ?? []), ...alias.body.body];
36
- }
37
-
38
- return (
39
- alias.typeAnnotation.members ||
40
- alias.typeAnnotation.typeParameters.params[0].members ||
41
- alias.typeAnnotation.typeParameters.params
42
- );
43
- } catch (e) {
44
- throw new Error(
45
- `Failed to find ${aliasKind} definition for "${typeName}", please check that you have a valid codegen typescript file`,
46
- );
47
- }
48
- }
16
+ import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';
17
+ import type {BuildSchemaFN, Parser} from '../../parser';
49
18
 
50
19
  function getUnionOfLiterals(
51
20
  name: string,
@@ -111,7 +80,8 @@ function detectArrayType<T>(
111
80
  typeAnnotation: $FlowFixMe | ASTNode,
112
81
  defaultValue: $FlowFixMe | void,
113
82
  types: TypeDeclarationMap,
114
- buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
83
+ parser: Parser,
84
+ buildSchema: BuildSchemaFN<T>,
115
85
  ): $FlowFixMe {
116
86
  // Covers: readonly T[]
117
87
  if (
@@ -126,6 +96,7 @@ function detectArrayType<T>(
126
96
  typeAnnotation.typeAnnotation.elementType,
127
97
  defaultValue,
128
98
  types,
99
+ parser,
129
100
  buildSchema,
130
101
  ),
131
102
  };
@@ -140,6 +111,7 @@ function detectArrayType<T>(
140
111
  typeAnnotation.elementType,
141
112
  defaultValue,
142
113
  types,
114
+ parser,
143
115
  buildSchema,
144
116
  ),
145
117
  };
@@ -158,6 +130,7 @@ function detectArrayType<T>(
158
130
  typeAnnotation.typeParameters.params[0],
159
131
  defaultValue,
160
132
  types,
133
+ parser,
161
134
  buildSchema,
162
135
  ),
163
136
  };
@@ -169,11 +142,12 @@ function detectArrayType<T>(
169
142
  function buildObjectType<T>(
170
143
  rawProperties: Array<$FlowFixMe>,
171
144
  types: TypeDeclarationMap,
172
- buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
145
+ parser: Parser,
146
+ buildSchema: BuildSchemaFN<T>,
173
147
  ): $FlowFixMe {
174
- const flattenedProperties = flattenProperties(rawProperties, types);
148
+ const flattenedProperties = flattenProperties(rawProperties, types, parser);
175
149
  const properties = flattenedProperties
176
- .map(prop => buildSchema(prop, types))
150
+ .map(prop => buildSchema(prop, types, parser))
177
151
  .filter(Boolean);
178
152
 
179
153
  return {
@@ -189,17 +163,24 @@ function getCommonTypeAnnotation<T>(
189
163
  typeAnnotation: $FlowFixMe,
190
164
  defaultValue: $FlowFixMe | void,
191
165
  types: TypeDeclarationMap,
192
- buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
166
+ parser: Parser,
167
+ buildSchema: BuildSchemaFN<T>,
193
168
  ): $FlowFixMe {
194
169
  switch (type) {
195
170
  case 'TSTypeLiteral':
196
- return buildObjectType(typeAnnotation.members, types, buildSchema);
171
+ return buildObjectType(
172
+ typeAnnotation.members,
173
+ types,
174
+ parser,
175
+ buildSchema,
176
+ );
197
177
  case 'TSInterfaceDeclaration':
198
- return buildObjectType([typeAnnotation], types, buildSchema);
178
+ return buildObjectType([typeAnnotation], types, parser, buildSchema);
199
179
  case 'TSIntersectionType':
200
180
  return buildObjectType(
201
181
  flattenIntersectionType(typeAnnotation, types),
202
182
  types,
183
+ parser,
203
184
  buildSchema,
204
185
  );
205
186
  case 'ImageSource':
@@ -276,7 +257,8 @@ function getTypeAnnotationForArray<T>(
276
257
  typeAnnotation: $FlowFixMe,
277
258
  defaultValue: $FlowFixMe | void,
278
259
  types: TypeDeclarationMap,
279
- buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
260
+ parser: Parser,
261
+ buildSchema: BuildSchemaFN<T>,
280
262
  ): $FlowFixMe {
281
263
  // unpack WithDefault, (T) or T|U
282
264
  const topLevelType = parseTopLevelType(typeAnnotation, types);
@@ -297,6 +279,7 @@ function getTypeAnnotationForArray<T>(
297
279
  extractedTypeAnnotation,
298
280
  defaultValue,
299
281
  types,
282
+ parser,
300
283
  buildSchema,
301
284
  );
302
285
  if (arrayType) {
@@ -322,6 +305,7 @@ function getTypeAnnotationForArray<T>(
322
305
  extractedTypeAnnotation,
323
306
  defaultValue,
324
307
  types,
308
+ parser,
325
309
  buildSchema,
326
310
  );
327
311
  if (common) {
@@ -370,8 +354,10 @@ function getTypeAnnotation<T>(
370
354
  name: string,
371
355
  annotation: $FlowFixMe | ASTNode,
372
356
  defaultValue: $FlowFixMe | void,
357
+ withNullDefault: boolean, // Just to make `getTypeAnnotation` signature match with the one from Flow
373
358
  types: TypeDeclarationMap,
374
- buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
359
+ parser: Parser,
360
+ buildSchema: BuildSchemaFN<T>,
375
361
  ): $FlowFixMe {
376
362
  // unpack WithDefault, (T) or T|U
377
363
  const topLevelType = parseTopLevelType(annotation, types);
@@ -381,6 +367,7 @@ function getTypeAnnotation<T>(
381
367
  typeAnnotation,
382
368
  defaultValue,
383
369
  types,
370
+ parser,
384
371
  buildSchema,
385
372
  );
386
373
  if (arrayType) {
@@ -400,6 +387,7 @@ function getTypeAnnotation<T>(
400
387
  typeAnnotation,
401
388
  defaultValue,
402
389
  types,
390
+ parser,
403
391
  buildSchema,
404
392
  );
405
393
  if (common) {
@@ -435,6 +423,7 @@ type SchemaInfo = {
435
423
  optional: boolean,
436
424
  typeAnnotation: $FlowFixMe,
437
425
  defaultValue: $FlowFixMe,
426
+ withNullDefault: boolean, // Just to make `getTypeAnnotation` signature match with the one from Flow
438
427
  };
439
428
 
440
429
  function getSchemaInfo(
@@ -460,12 +449,10 @@ function getSchemaInfo(
460
449
  optional: property.optional || topLevelType.optional,
461
450
  typeAnnotation: topLevelType.type,
462
451
  defaultValue: topLevelType.defaultValue,
452
+ withNullDefault: false, // Just to make `getTypeAnnotation` signature match with the one from Flow
463
453
  };
464
454
  }
465
455
 
466
- // $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
467
- type PropAST = Object;
468
-
469
456
  function verifyPropNotAlreadyDefined(
470
457
  props: $ReadOnlyArray<PropAST>,
471
458
  needleProp: PropAST,
@@ -480,6 +467,7 @@ function verifyPropNotAlreadyDefined(
480
467
  function flattenProperties(
481
468
  typeDefinition: $ReadOnlyArray<PropAST>,
482
469
  types: TypeDeclarationMap,
470
+ parser: Parser,
483
471
  ): $ReadOnlyArray<PropAST> {
484
472
  return typeDefinition
485
473
  .map(property => {
@@ -487,23 +475,29 @@ function flattenProperties(
487
475
  return property;
488
476
  } else if (property.type === 'TSTypeReference') {
489
477
  return flattenProperties(
490
- getProperties(property.typeName.name, types),
478
+ parser.getProperties(property.typeName.name, types),
491
479
  types,
480
+ parser,
492
481
  );
493
482
  } else if (
494
483
  property.type === 'TSExpressionWithTypeArguments' ||
495
484
  property.type === 'TSInterfaceHeritage'
496
485
  ) {
497
486
  return flattenProperties(
498
- getProperties(property.expression.name, types),
487
+ parser.getProperties(property.expression.name, types),
499
488
  types,
489
+ parser,
500
490
  );
501
491
  } else if (property.type === 'TSTypeLiteral') {
502
- return flattenProperties(property.members, types);
492
+ return flattenProperties(property.members, types, parser);
503
493
  } else if (property.type === 'TSInterfaceDeclaration') {
504
- return flattenProperties(getProperties(property.id.name, types), types);
494
+ return flattenProperties(
495
+ parser.getProperties(property.id.name, types),
496
+ types,
497
+ parser,
498
+ );
505
499
  } else if (property.type === 'TSIntersectionType') {
506
- return flattenProperties(property.types, types);
500
+ return flattenProperties(property.types, types, parser);
507
501
  } else {
508
502
  throw new Error(
509
503
  `${property.type} is not a supported object literal type.`,
@@ -527,7 +521,6 @@ function flattenProperties(
527
521
  }
528
522
 
529
523
  module.exports = {
530
- getProperties,
531
524
  getSchemaInfo,
532
525
  getTypeAnnotation,
533
526
  flattenProperties,
@@ -14,6 +14,18 @@ const _require = require('./componentsUtils'),
14
14
  flattenProperties = _require.flattenProperties;
15
15
  const _require2 = require('../parseTopLevelType'),
16
16
  parseTopLevelType = _require2.parseTopLevelType;
17
+ const _require3 = require('../../error-utils'),
18
+ throwIfEventHasNoName = _require3.throwIfEventHasNoName,
19
+ throwIfBubblingTypeIsNull = _require3.throwIfBubblingTypeIsNull,
20
+ throwIfArgumentPropsAreNull = _require3.throwIfArgumentPropsAreNull;
21
+ const _require4 = require('../../parsers-commons'),
22
+ getEventArgument = _require4.getEventArgument;
23
+ const _require5 = require('../../parsers-primitives'),
24
+ emitBoolProp = _require5.emitBoolProp,
25
+ emitDoubleProp = _require5.emitDoubleProp,
26
+ emitFloatProp = _require5.emitFloatProp,
27
+ emitStringProp = _require5.emitStringProp,
28
+ emitInt32Prop = _require5.emitInt32Prop;
17
29
  function getPropertyType(
18
30
  /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
19
31
  * LTI update could not be added via codemod */
@@ -22,6 +34,7 @@ function getPropertyType(
22
34
  /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
23
35
  * LTI update could not be added via codemod */
24
36
  annotation,
37
+ parser,
25
38
  ) {
26
39
  const topLevelType = parseTopLevelType(annotation);
27
40
  const typeAnnotation = topLevelType.type;
@@ -32,69 +45,122 @@ function getPropertyType(
32
45
  : typeAnnotation.type;
33
46
  switch (type) {
34
47
  case 'TSBooleanKeyword':
48
+ return emitBoolProp(name, optional);
49
+ case 'TSStringKeyword':
50
+ return emitStringProp(name, optional);
51
+ case 'Int32':
52
+ return emitInt32Prop(name, optional);
53
+ case 'Double':
54
+ return emitDoubleProp(name, optional);
55
+ case 'Float':
56
+ return emitFloatProp(name, optional);
57
+ case 'TSTypeLiteral':
35
58
  return {
36
59
  name,
37
60
  optional,
38
61
  typeAnnotation: {
39
- type: 'BooleanTypeAnnotation',
62
+ type: 'ObjectTypeAnnotation',
63
+ properties: typeAnnotation.members.map(member =>
64
+ buildPropertiesForEvent(member, parser),
65
+ ),
40
66
  },
41
67
  };
42
- case 'TSStringKeyword':
68
+ case 'TSUnionType':
43
69
  return {
44
70
  name,
45
71
  optional,
46
72
  typeAnnotation: {
47
- type: 'StringTypeAnnotation',
73
+ type: 'StringEnumTypeAnnotation',
74
+ options: typeAnnotation.types.map(option => option.literal.value),
48
75
  },
49
76
  };
50
- case 'Int32':
77
+ case 'UnsafeMixed':
51
78
  return {
52
79
  name,
53
80
  optional,
54
81
  typeAnnotation: {
55
- type: 'Int32TypeAnnotation',
82
+ type: 'MixedTypeAnnotation',
56
83
  },
57
84
  };
58
- case 'Double':
85
+ case 'TSArrayType':
59
86
  return {
60
87
  name,
61
88
  optional,
62
- typeAnnotation: {
63
- type: 'DoubleTypeAnnotation',
64
- },
89
+ typeAnnotation: extractArrayElementType(typeAnnotation, name, parser),
90
+ };
91
+ default:
92
+ type;
93
+ throw new Error(`Unable to determine event type for "${name}": ${type}`);
94
+ }
95
+ }
96
+ function extractArrayElementType(typeAnnotation, name, parser) {
97
+ const type = extractTypeFromTypeAnnotation(typeAnnotation);
98
+ switch (type) {
99
+ case 'TSParenthesizedType':
100
+ return extractArrayElementType(
101
+ typeAnnotation.typeAnnotation,
102
+ name,
103
+ parser,
104
+ );
105
+ case 'TSBooleanKeyword':
106
+ return {
107
+ type: 'BooleanTypeAnnotation',
108
+ };
109
+ case 'TSStringKeyword':
110
+ return {
111
+ type: 'StringTypeAnnotation',
65
112
  };
66
113
  case 'Float':
67
114
  return {
68
- name,
69
- optional,
70
- typeAnnotation: {
71
- type: 'FloatTypeAnnotation',
72
- },
115
+ type: 'FloatTypeAnnotation',
73
116
  };
74
- case 'TSTypeLiteral':
117
+ case 'Int32':
75
118
  return {
76
- name,
77
- optional,
78
- typeAnnotation: {
79
- type: 'ObjectTypeAnnotation',
80
- properties: typeAnnotation.members.map(buildPropertiesForEvent),
81
- },
119
+ type: 'Int32TypeAnnotation',
120
+ };
121
+ case 'TSNumberKeyword':
122
+ case 'Double':
123
+ return {
124
+ type: 'DoubleTypeAnnotation',
82
125
  };
83
126
  case 'TSUnionType':
84
127
  return {
85
- name,
86
- optional,
87
- typeAnnotation: {
88
- type: 'StringEnumTypeAnnotation',
89
- options: typeAnnotation.types.map(option => option.literal.value),
90
- },
128
+ type: 'StringEnumTypeAnnotation',
129
+ options: typeAnnotation.types.map(option => option.literal.value),
130
+ };
131
+ case 'TSTypeLiteral':
132
+ return {
133
+ type: 'ObjectTypeAnnotation',
134
+ properties: typeAnnotation.members.map(member =>
135
+ buildPropertiesForEvent(member, parser),
136
+ ),
137
+ };
138
+ case 'TSArrayType':
139
+ return {
140
+ type: 'ArrayTypeAnnotation',
141
+ elementType: extractArrayElementType(
142
+ typeAnnotation.elementType,
143
+ name,
144
+ parser,
145
+ ),
91
146
  };
92
147
  default:
93
- type;
94
- throw new Error(`Unable to determine event type for "${name}": ${type}`);
148
+ throw new Error(
149
+ `Unrecognized ${type} for Array ${name} in events.\n${JSON.stringify(
150
+ typeAnnotation,
151
+ null,
152
+ 2,
153
+ )}`,
154
+ );
95
155
  }
96
156
  }
157
+ function extractTypeFromTypeAnnotation(typeAnnotation) {
158
+ return typeAnnotation.type === 'TSTypeReference'
159
+ ? typeAnnotation.typeName.name
160
+ : typeAnnotation.type;
161
+ }
97
162
  function findEventArgumentsAndType(
163
+ parser,
98
164
  typeAnnotation,
99
165
  types,
100
166
  bubblingType,
@@ -102,7 +168,7 @@ function findEventArgumentsAndType(
102
168
  ) {
103
169
  if (typeAnnotation.type === 'TSInterfaceDeclaration') {
104
170
  return {
105
- argumentProps: flattenProperties([typeAnnotation], types),
171
+ argumentProps: flattenProperties([typeAnnotation], types, parser),
106
172
  paperTopLevelNameDeprecated: paperName,
107
173
  bubblingType,
108
174
  };
@@ -114,12 +180,11 @@ function findEventArgumentsAndType(
114
180
  bubblingType,
115
181
  };
116
182
  }
117
- if (!typeAnnotation.typeName) {
118
- throw new Error("typeAnnotation of event doesn't have a name");
119
- }
183
+ throwIfEventHasNoName(typeAnnotation, parser);
120
184
  const name = typeAnnotation.typeName.name;
121
185
  if (name === 'Readonly') {
122
186
  return findEventArgumentsAndType(
187
+ parser,
123
188
  typeAnnotation.typeParameters.params[0],
124
189
  types,
125
190
  bubblingType,
@@ -132,8 +197,8 @@ function findEventArgumentsAndType(
132
197
  ? typeAnnotation.typeParameters.params[1].literal.value
133
198
  : null;
134
199
  switch (typeAnnotation.typeParameters.params[0].type) {
135
- case 'TSNullKeyword':
136
- case 'TSUndefinedKeyword':
200
+ case parser.nullLiteralTypeAnnotation:
201
+ case parser.undefinedLiteralTypeAnnotation:
137
202
  return {
138
203
  argumentProps: [],
139
204
  bubblingType: eventType,
@@ -141,6 +206,7 @@ function findEventArgumentsAndType(
141
206
  };
142
207
  default:
143
208
  return findEventArgumentsAndType(
209
+ parser,
144
210
  typeAnnotation.typeParameters.params[0],
145
211
  types,
146
212
  eventType,
@@ -153,6 +219,7 @@ function findEventArgumentsAndType(
153
219
  elementType = elementType.typeAnnotation;
154
220
  }
155
221
  return findEventArgumentsAndType(
222
+ parser,
156
223
  elementType,
157
224
  types,
158
225
  bubblingType,
@@ -166,28 +233,21 @@ function findEventArgumentsAndType(
166
233
  };
167
234
  }
168
235
  }
169
-
170
- /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
171
- * LTI update could not be added via codemod */
172
- function buildPropertiesForEvent(property) {
236
+ function buildPropertiesForEvent(
237
+ /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
238
+ * LTI update could not be added via codemod */
239
+ property,
240
+ parser,
241
+ ) {
173
242
  const name = property.key.name;
174
- const optional = property.optional || false;
243
+ const optional = parser.isOptionalProperty(property);
175
244
  let typeAnnotation = property.typeAnnotation.typeAnnotation;
176
- return getPropertyType(name, optional, typeAnnotation);
177
- }
178
-
179
- /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
180
- * LTI update could not be added via codemod */
181
- function getEventArgument(argumentProps, name) {
182
- return {
183
- type: 'ObjectTypeAnnotation',
184
- properties: argumentProps.map(buildPropertiesForEvent),
185
- };
245
+ return getPropertyType(name, optional, typeAnnotation, parser);
186
246
  }
187
247
 
188
248
  // $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
189
249
 
190
- function buildEventSchema(types, property) {
250
+ function buildEventSchema(types, property, parser) {
191
251
  // unpack WithDefault, (T) or T|U
192
252
  const topLevelType = parseTopLevelType(
193
253
  property.typeAnnotation.typeAnnotation,
@@ -197,6 +257,7 @@ function buildEventSchema(types, property) {
197
257
  const typeAnnotation = topLevelType.type;
198
258
  const optional = property.optional || topLevelType.optional;
199
259
  const _findEventArgumentsAn = findEventArgumentsAndType(
260
+ parser,
200
261
  typeAnnotation,
201
262
  types,
202
263
  ),
@@ -204,36 +265,45 @@ function buildEventSchema(types, property) {
204
265
  bubblingType = _findEventArgumentsAn.bubblingType,
205
266
  paperTopLevelNameDeprecated =
206
267
  _findEventArgumentsAn.paperTopLevelNameDeprecated;
207
- if (!argumentProps) {
208
- throw new Error(`Unable to determine event arguments for "${name}"`);
209
- } else if (!bubblingType) {
210
- throw new Error(`Unable to determine event bubbling type for "${name}"`);
211
- } else {
212
- if (paperTopLevelNameDeprecated != null) {
213
- return {
214
- name,
215
- optional,
216
- bubblingType,
217
- paperTopLevelNameDeprecated,
218
- typeAnnotation: {
219
- type: 'EventTypeAnnotation',
220
- argument: getEventArgument(argumentProps, name),
221
- },
222
- };
223
- }
268
+ const nonNullableArgumentProps = throwIfArgumentPropsAreNull(
269
+ argumentProps,
270
+ name,
271
+ );
272
+ const nonNullableBubblingType = throwIfBubblingTypeIsNull(bubblingType, name);
273
+ if (paperTopLevelNameDeprecated != null) {
224
274
  return {
225
275
  name,
226
276
  optional,
227
- bubblingType,
277
+ bubblingType: nonNullableBubblingType,
278
+ paperTopLevelNameDeprecated,
228
279
  typeAnnotation: {
229
280
  type: 'EventTypeAnnotation',
230
- argument: getEventArgument(argumentProps, name),
281
+ argument: getEventArgument(
282
+ nonNullableArgumentProps,
283
+ buildPropertiesForEvent,
284
+ parser,
285
+ ),
231
286
  },
232
287
  };
233
288
  }
289
+ return {
290
+ name,
291
+ optional,
292
+ bubblingType: nonNullableBubblingType,
293
+ typeAnnotation: {
294
+ type: 'EventTypeAnnotation',
295
+ argument: getEventArgument(
296
+ nonNullableArgumentProps,
297
+ buildPropertiesForEvent,
298
+ parser,
299
+ ),
300
+ },
301
+ };
234
302
  }
235
- function getEvents(eventTypeAST, types) {
236
- return eventTypeAST.map(property => buildEventSchema(types, property));
303
+ function getEvents(eventTypeAST, types, parser) {
304
+ return eventTypeAST
305
+ .map(property => buildEventSchema(types, property, parser))
306
+ .filter(Boolean);
237
307
  }
238
308
  module.exports = {
239
309
  getEvents,