@react-native/codegen 0.72.4 → 0.73.0

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 (28) hide show
  1. package/lib/generators/components/GenerateEventEmitterCpp.js +33 -44
  2. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +35 -44
  3. package/lib/generators/components/GenerateEventEmitterH.js +0 -2
  4. package/lib/generators/components/GenerateEventEmitterH.js.flow +0 -2
  5. package/lib/generators/modules/GenerateModuleCpp.js +3 -3
  6. package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
  7. package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
  8. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
  9. package/lib/parsers/flow/components/index.js +19 -57
  10. package/lib/parsers/flow/components/index.js.flow +20 -63
  11. package/lib/parsers/flow/modules/index.js +16 -26
  12. package/lib/parsers/flow/modules/index.js.flow +17 -26
  13. package/lib/parsers/flow/parser.js +16 -1
  14. package/lib/parsers/flow/parser.js.flow +13 -1
  15. package/lib/parsers/parser.js.flow +19 -0
  16. package/lib/parsers/parserMock.js +9 -0
  17. package/lib/parsers/parserMock.js.flow +12 -0
  18. package/lib/parsers/parsers-commons.js +36 -0
  19. package/lib/parsers/parsers-commons.js.flow +51 -1
  20. package/lib/parsers/parsers-primitives.js +37 -35
  21. package/lib/parsers/parsers-primitives.js.flow +41 -35
  22. package/lib/parsers/typescript/components/index.js +19 -57
  23. package/lib/parsers/typescript/components/index.js.flow +18 -63
  24. package/lib/parsers/typescript/modules/index.js +16 -26
  25. package/lib/parsers/typescript/modules/index.js.flow +17 -25
  26. package/lib/parsers/typescript/parser.js +28 -1
  27. package/lib/parsers/typescript/parser.js.flow +26 -1
  28. package/package.json +1 -1
@@ -88,15 +88,10 @@ const _require2 = require('../../parsers-commons'),
88
88
  parseObjectProperty = _require2.parseObjectProperty;
89
89
  const _require3 = require('../../parsers-primitives'),
90
90
  emitArrayType = _require3.emitArrayType,
91
- emitBoolean = _require3.emitBoolean,
92
91
  emitFunction = _require3.emitFunction,
93
- emitNumber = _require3.emitNumber,
94
92
  emitGenericObject = _require3.emitGenericObject,
95
93
  emitPromise = _require3.emitPromise,
96
94
  emitRootTag = _require3.emitRootTag,
97
- emitVoid = _require3.emitVoid,
98
- emitString = _require3.emitString,
99
- emitMixed = _require3.emitMixed,
100
95
  emitUnion = _require3.emitUnion,
101
96
  emitCommonTypes = _require3.emitCommonTypes,
102
97
  typeAliasResolution = _require3.typeAliasResolution,
@@ -258,18 +253,6 @@ function translateTypeAnnotation(
258
253
  nullable,
259
254
  );
260
255
  }
261
- case 'BooleanTypeAnnotation': {
262
- return emitBoolean(nullable);
263
- }
264
- case 'NumberTypeAnnotation': {
265
- return emitNumber(nullable);
266
- }
267
- case 'VoidTypeAnnotation': {
268
- return emitVoid(nullable);
269
- }
270
- case 'StringTypeAnnotation': {
271
- return emitString(nullable);
272
- }
273
256
  case 'FunctionTypeAnnotation': {
274
257
  return emitFunction(
275
258
  nullable,
@@ -294,13 +277,6 @@ function translateTypeAnnotation(
294
277
  memberType: 'StringTypeAnnotation',
295
278
  });
296
279
  }
297
- case 'MixedTypeAnnotation': {
298
- if (cxxOnly) {
299
- return emitMixed(nullable);
300
- } else {
301
- return emitGenericObject(nullable);
302
- }
303
- }
304
280
  case 'EnumStringBody':
305
281
  case 'EnumNumberBody': {
306
282
  return typeEnumResolution(
@@ -313,11 +289,25 @@ function translateTypeAnnotation(
313
289
  );
314
290
  }
315
291
  default: {
316
- throw new UnsupportedTypeAnnotationParserError(
292
+ const commonType = emitCommonTypes(
317
293
  hasteModuleName,
294
+ types,
318
295
  typeAnnotation,
319
- parser.language(),
296
+ aliasMap,
297
+ enumMap,
298
+ tryParse,
299
+ cxxOnly,
300
+ nullable,
301
+ parser,
320
302
  );
303
+ if (!commonType) {
304
+ throw new UnsupportedTypeAnnotationParserError(
305
+ hasteModuleName,
306
+ typeAnnotation,
307
+ parser.language(),
308
+ );
309
+ }
310
+ return commonType;
321
311
  }
322
312
  }
323
313
  }
@@ -31,15 +31,10 @@ const {
31
31
  } = require('../../parsers-commons');
32
32
  const {
33
33
  emitArrayType,
34
- emitBoolean,
35
34
  emitFunction,
36
- emitNumber,
37
35
  emitGenericObject,
38
36
  emitPromise,
39
37
  emitRootTag,
40
- emitVoid,
41
- emitString,
42
- emitMixed,
43
38
  emitUnion,
44
39
  emitCommonTypes,
45
40
  typeAliasResolution,
@@ -207,18 +202,6 @@ function translateTypeAnnotation(
207
202
  nullable,
208
203
  );
209
204
  }
210
- case 'BooleanTypeAnnotation': {
211
- return emitBoolean(nullable);
212
- }
213
- case 'NumberTypeAnnotation': {
214
- return emitNumber(nullable);
215
- }
216
- case 'VoidTypeAnnotation': {
217
- return emitVoid(nullable);
218
- }
219
- case 'StringTypeAnnotation': {
220
- return emitString(nullable);
221
- }
222
205
  case 'FunctionTypeAnnotation': {
223
206
  return emitFunction(
224
207
  nullable,
@@ -243,13 +226,6 @@ function translateTypeAnnotation(
243
226
  memberType: 'StringTypeAnnotation',
244
227
  });
245
228
  }
246
- case 'MixedTypeAnnotation': {
247
- if (cxxOnly) {
248
- return emitMixed(nullable);
249
- } else {
250
- return emitGenericObject(nullable);
251
- }
252
- }
253
229
  case 'EnumStringBody':
254
230
  case 'EnumNumberBody': {
255
231
  return typeEnumResolution(
@@ -262,11 +238,26 @@ function translateTypeAnnotation(
262
238
  );
263
239
  }
264
240
  default: {
265
- throw new UnsupportedTypeAnnotationParserError(
241
+ const commonType = emitCommonTypes(
266
242
  hasteModuleName,
243
+ types,
267
244
  typeAnnotation,
268
- parser.language(),
245
+ aliasMap,
246
+ enumMap,
247
+ tryParse,
248
+ cxxOnly,
249
+ nullable,
250
+ parser,
269
251
  );
252
+
253
+ if (!commonType) {
254
+ throw new UnsupportedTypeAnnotationParserError(
255
+ hasteModuleName,
256
+ typeAnnotation,
257
+ parser.language(),
258
+ );
259
+ }
260
+ return commonType;
270
261
  }
271
262
  }
272
263
  }
@@ -85,7 +85,13 @@ class FlowParser {
85
85
  return 'Flow';
86
86
  }
87
87
  nameForGenericTypeAnnotation(typeAnnotation) {
88
- return typeAnnotation.id.name;
88
+ var _typeAnnotation$id;
89
+ return typeAnnotation === null || typeAnnotation === void 0
90
+ ? void 0
91
+ : (_typeAnnotation$id = typeAnnotation.id) === null ||
92
+ _typeAnnotation$id === void 0
93
+ ? void 0
94
+ : _typeAnnotation$id.name;
89
95
  }
90
96
  checkIfInvalidModule(typeArguments) {
91
97
  return (
@@ -208,6 +214,9 @@ class FlowParser {
208
214
  node.extends[0].id.name === 'TurboModule'
209
215
  );
210
216
  }
217
+ isGenericTypeAnnotation(type) {
218
+ return type === 'GenericTypeAnnotation';
219
+ }
211
220
  extractAnnotatedElement(typeAnnotation, types) {
212
221
  return types[typeAnnotation.typeParameters.params[0].id.name];
213
222
  }
@@ -300,6 +309,12 @@ class FlowParser {
300
309
  getAnnotatedElementProperties(annotatedElement) {
301
310
  return annotatedElement.right.properties;
302
311
  }
312
+ bodyProperties(typeAlias) {
313
+ return typeAlias.body.properties;
314
+ }
315
+ convertKeywordToTypeAnnotation(keyword) {
316
+ return keyword;
317
+ }
303
318
  }
304
319
  module.exports = {
305
320
  FlowParser,
@@ -67,7 +67,7 @@ class FlowParser implements Parser {
67
67
  }
68
68
 
69
69
  nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
70
- return typeAnnotation.id.name;
70
+ return typeAnnotation?.id?.name;
71
71
  }
72
72
 
73
73
  checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
@@ -214,6 +214,10 @@ class FlowParser implements Parser {
214
214
  );
215
215
  }
216
216
 
217
+ isGenericTypeAnnotation(type: $FlowFixMe): boolean {
218
+ return type === 'GenericTypeAnnotation';
219
+ }
220
+
217
221
  extractAnnotatedElement(
218
222
  typeAnnotation: $FlowFixMe,
219
223
  types: TypeDeclarationMap,
@@ -317,6 +321,14 @@ class FlowParser implements Parser {
317
321
  getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
318
322
  return annotatedElement.right.properties;
319
323
  }
324
+
325
+ bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
326
+ return typeAlias.body.properties;
327
+ }
328
+
329
+ convertKeywordToTypeAnnotation(keyword: string): string {
330
+ return keyword;
331
+ }
320
332
  }
321
333
 
322
334
  module.exports = {
@@ -161,6 +161,11 @@ export interface Parser {
161
161
  */
162
162
  isModuleInterface(node: $FlowFixMe): boolean;
163
163
 
164
+ /**
165
+ * Given a type name, it returns true if it is a generic type annotation
166
+ */
167
+ isGenericTypeAnnotation(type: $FlowFixMe): boolean;
168
+
164
169
  /**
165
170
  * Given a typeAnnotation, it returns the annotated element.
166
171
  * @parameter typeAnnotation: the annotation for a type.
@@ -236,4 +241,18 @@ export interface Parser {
236
241
  * @returns: the properties of annotated element.
237
242
  */
238
243
  getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe;
244
+
245
+ /**
246
+ * Given a typeAlias, it returns an array of properties.
247
+ * @parameter typeAlias: the type alias.
248
+ * @returns: an array of properties.
249
+ */
250
+ bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe>;
251
+
252
+ /**
253
+ * Given a keyword convert it to TypeAnnotation.
254
+ * @parameter keyword
255
+ * @returns: converted TypeAnnotation to Keywords
256
+ */
257
+ convertKeywordToTypeAnnotation(keyword: string): string;
239
258
  }
@@ -158,6 +158,9 @@ export class MockedParser {
158
158
  node.extends[0].id.name === 'TurboModule'
159
159
  );
160
160
  }
161
+ isGenericTypeAnnotation(type) {
162
+ return true;
163
+ }
161
164
  extractAnnotatedElement(typeAnnotation, types) {
162
165
  return types[typeAnnotation.typeParameters.params[0].id.name];
163
166
  }
@@ -208,4 +211,10 @@ export class MockedParser {
208
211
  getAnnotatedElementProperties(annotatedElement) {
209
212
  return annotatedElement.right.properties;
210
213
  }
214
+ bodyProperties(typeAlias) {
215
+ return typeAlias.body.properties;
216
+ }
217
+ convertKeywordToTypeAnnotation(keyword) {
218
+ return keyword;
219
+ }
211
220
  }
@@ -172,6 +172,10 @@ export class MockedParser implements Parser {
172
172
  );
173
173
  }
174
174
 
175
+ isGenericTypeAnnotation(type: $FlowFixMe): boolean {
176
+ return true;
177
+ }
178
+
175
179
  extractAnnotatedElement(
176
180
  typeAnnotation: $FlowFixMe,
177
181
  types: TypeDeclarationMap,
@@ -231,4 +235,12 @@ export class MockedParser implements Parser {
231
235
  getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
232
236
  return annotatedElement.right.properties;
233
237
  }
238
+
239
+ bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
240
+ return typeAlias.body.properties;
241
+ }
242
+
243
+ convertKeywordToTypeAnnotation(keyword: string): string {
244
+ return keyword;
245
+ }
234
246
  }
@@ -787,6 +787,40 @@ function getOptions(optionsExpression) {
787
787
  }
788
788
  return foundOptions;
789
789
  }
790
+ function getCommandTypeNameAndOptionsExpression(namedExport, parser) {
791
+ let callExpression;
792
+ let calleeName;
793
+ try {
794
+ callExpression = namedExport.declaration.declarations[0].init;
795
+ calleeName = callExpression.callee.name;
796
+ } catch (e) {
797
+ return;
798
+ }
799
+ if (calleeName !== 'codegenNativeCommands') {
800
+ return;
801
+ }
802
+ if (callExpression.arguments.length !== 1) {
803
+ throw new Error(
804
+ 'codegenNativeCommands must be passed options including the supported commands',
805
+ );
806
+ }
807
+ const typeArgumentParam =
808
+ parser.getTypeArgumentParamsFromDeclaration(callExpression)[0];
809
+ if (!parser.isGenericTypeAnnotation(typeArgumentParam.type)) {
810
+ throw new Error(
811
+ "codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
812
+ );
813
+ }
814
+ return {
815
+ commandTypeName: parser.nameForGenericTypeAnnotation(typeArgumentParam),
816
+ commandOptionsExpression: callExpression.arguments[0],
817
+ };
818
+ }
819
+ function propertyNames(properties) {
820
+ return properties
821
+ .map(property => property && property.key && property.key.name)
822
+ .filter(Boolean);
823
+ }
790
824
  module.exports = {
791
825
  wrapModuleSchema,
792
826
  unwrapNullable,
@@ -802,6 +836,8 @@ module.exports = {
802
836
  parseModuleName,
803
837
  buildModuleSchema,
804
838
  findNativeComponentType,
839
+ propertyNames,
805
840
  getCommandOptions,
806
841
  getOptions,
842
+ getCommandTypeNameAndOptionsExpression,
807
843
  };
@@ -765,10 +765,58 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
765
765
  'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
766
766
  );
767
767
  }
768
-
769
768
  return foundOptions;
770
769
  }
771
770
 
771
+ function getCommandTypeNameAndOptionsExpression(
772
+ namedExport: $FlowFixMe,
773
+ parser: Parser,
774
+ ): {
775
+ commandOptionsExpression: OptionsAST,
776
+ commandTypeName: string,
777
+ } | void {
778
+ let callExpression;
779
+ let calleeName;
780
+ try {
781
+ callExpression = namedExport.declaration.declarations[0].init;
782
+ calleeName = callExpression.callee.name;
783
+ } catch (e) {
784
+ return;
785
+ }
786
+
787
+ if (calleeName !== 'codegenNativeCommands') {
788
+ return;
789
+ }
790
+
791
+ if (callExpression.arguments.length !== 1) {
792
+ throw new Error(
793
+ 'codegenNativeCommands must be passed options including the supported commands',
794
+ );
795
+ }
796
+
797
+ const typeArgumentParam =
798
+ parser.getTypeArgumentParamsFromDeclaration(callExpression)[0];
799
+
800
+ if (!parser.isGenericTypeAnnotation(typeArgumentParam.type)) {
801
+ throw new Error(
802
+ "codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
803
+ );
804
+ }
805
+
806
+ return {
807
+ commandTypeName: parser.nameForGenericTypeAnnotation(typeArgumentParam),
808
+ commandOptionsExpression: callExpression.arguments[0],
809
+ };
810
+ }
811
+
812
+ function propertyNames(
813
+ properties: $ReadOnlyArray<$FlowFixMe>,
814
+ ): $ReadOnlyArray<$FlowFixMe> {
815
+ return properties
816
+ .map(property => property && property.key && property.key.name)
817
+ .filter(Boolean);
818
+ }
819
+
772
820
  module.exports = {
773
821
  wrapModuleSchema,
774
822
  unwrapNullable,
@@ -784,6 +832,8 @@ module.exports = {
784
832
  parseModuleName,
785
833
  buildModuleSchema,
786
834
  findNativeComponentType,
835
+ propertyNames,
787
836
  getCommandOptions,
788
837
  getOptions,
838
+ getCommandTypeNameAndOptionsExpression,
789
839
  };
@@ -462,6 +462,7 @@ function Visitor(infoMap) {
462
462
  };
463
463
  }
464
464
  function emitPartial(
465
+ nullable,
465
466
  hasteModuleName,
466
467
  typeAnnotation,
467
468
  types,
@@ -469,7 +470,6 @@ function emitPartial(
469
470
  enumMap,
470
471
  tryParse,
471
472
  cxxOnly,
472
- nullable,
473
473
  parser,
474
474
  ) {
475
475
  throwIfPartialWithMoreParameter(typeAnnotation);
@@ -502,43 +502,45 @@ function emitCommonTypes(
502
502
  nullable,
503
503
  parser,
504
504
  ) {
505
+ const typeMap = {
506
+ Stringish: emitStringish,
507
+ Int32: emitInt32,
508
+ Double: emitDouble,
509
+ Float: emitFloat,
510
+ UnsafeObject: emitGenericObject,
511
+ Object: emitGenericObject,
512
+ $Partial: emitPartial,
513
+ Partial: emitPartial,
514
+ BooleanTypeAnnotation: emitBoolean,
515
+ NumberTypeAnnotation: emitNumber,
516
+ VoidTypeAnnotation: emitVoid,
517
+ StringTypeAnnotation: emitString,
518
+ MixedTypeAnnotation: cxxOnly ? emitMixed : emitGenericObject,
519
+ };
520
+ const typeAnnotationName = parser.convertKeywordToTypeAnnotation(
521
+ typeAnnotation.type,
522
+ );
523
+ const simpleEmitter = typeMap[typeAnnotationName];
524
+ if (simpleEmitter) {
525
+ return simpleEmitter(nullable);
526
+ }
505
527
  const genericTypeAnnotationName =
506
528
  parser.nameForGenericTypeAnnotation(typeAnnotation);
507
- switch (genericTypeAnnotationName) {
508
- case 'Stringish': {
509
- return emitStringish(nullable);
510
- }
511
- case 'Int32': {
512
- return emitInt32(nullable);
513
- }
514
- case 'Double': {
515
- return emitDouble(nullable);
516
- }
517
- case 'Float': {
518
- return emitFloat(nullable);
519
- }
520
- case 'UnsafeObject':
521
- case 'Object': {
522
- return emitGenericObject(nullable);
523
- }
524
- case '$Partial':
525
- case 'Partial': {
526
- return emitPartial(
527
- hasteModuleName,
528
- typeAnnotation,
529
- types,
530
- aliasMap,
531
- enumMap,
532
- tryParse,
533
- cxxOnly,
534
- nullable,
535
- parser,
536
- );
537
- }
538
- default: {
539
- return null;
540
- }
529
+ const emitter = typeMap[genericTypeAnnotationName];
530
+ if (!emitter) {
531
+ return null;
541
532
  }
533
+ return emitter(
534
+ nullable,
535
+ hasteModuleName,
536
+ typeAnnotation,
537
+ types,
538
+ aliasMap,
539
+ enumMap,
540
+ tryParse,
541
+ cxxOnly,
542
+ parser,
543
+ );
542
544
  }
543
545
  module.exports = {
544
546
  emitArrayType,
@@ -476,6 +476,7 @@ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): {
476
476
  }
477
477
 
478
478
  function emitPartial(
479
+ nullable: boolean,
479
480
  hasteModuleName: string,
480
481
  typeAnnotation: $FlowFixMe,
481
482
  types: TypeDeclarationMap,
@@ -483,7 +484,6 @@ function emitPartial(
483
484
  enumMap: {...NativeModuleEnumMap},
484
485
  tryParse: ParserErrorCapturer,
485
486
  cxxOnly: boolean,
486
- nullable: boolean,
487
487
  parser: Parser,
488
488
  ): Nullable<NativeModuleTypeAnnotation> {
489
489
  throwIfPartialWithMoreParameter(typeAnnotation);
@@ -521,44 +521,50 @@ function emitCommonTypes(
521
521
  nullable: boolean,
522
522
  parser: Parser,
523
523
  ): $FlowFixMe {
524
+ const typeMap = {
525
+ Stringish: emitStringish,
526
+ Int32: emitInt32,
527
+ Double: emitDouble,
528
+ Float: emitFloat,
529
+ UnsafeObject: emitGenericObject,
530
+ Object: emitGenericObject,
531
+ $Partial: emitPartial,
532
+ Partial: emitPartial,
533
+ BooleanTypeAnnotation: emitBoolean,
534
+ NumberTypeAnnotation: emitNumber,
535
+ VoidTypeAnnotation: emitVoid,
536
+ StringTypeAnnotation: emitString,
537
+ MixedTypeAnnotation: cxxOnly ? emitMixed : emitGenericObject,
538
+ };
539
+
540
+ const typeAnnotationName = parser.convertKeywordToTypeAnnotation(
541
+ typeAnnotation.type,
542
+ );
543
+
544
+ const simpleEmitter = typeMap[typeAnnotationName];
545
+ if (simpleEmitter) {
546
+ return simpleEmitter(nullable);
547
+ }
548
+
524
549
  const genericTypeAnnotationName =
525
550
  parser.nameForGenericTypeAnnotation(typeAnnotation);
526
551
 
527
- switch (genericTypeAnnotationName) {
528
- case 'Stringish': {
529
- return emitStringish(nullable);
530
- }
531
- case 'Int32': {
532
- return emitInt32(nullable);
533
- }
534
- case 'Double': {
535
- return emitDouble(nullable);
536
- }
537
- case 'Float': {
538
- return emitFloat(nullable);
539
- }
540
- case 'UnsafeObject':
541
- case 'Object': {
542
- return emitGenericObject(nullable);
543
- }
544
- case '$Partial':
545
- case 'Partial': {
546
- return emitPartial(
547
- hasteModuleName,
548
- typeAnnotation,
549
- types,
550
- aliasMap,
551
- enumMap,
552
- tryParse,
553
- cxxOnly,
554
- nullable,
555
- parser,
556
- );
557
- }
558
- default: {
559
- return null;
560
- }
552
+ const emitter = typeMap[genericTypeAnnotationName];
553
+ if (!emitter) {
554
+ return null;
561
555
  }
556
+
557
+ return emitter(
558
+ nullable,
559
+ hasteModuleName,
560
+ typeAnnotation,
561
+ types,
562
+ aliasMap,
563
+ enumMap,
564
+ tryParse,
565
+ cxxOnly,
566
+ parser,
567
+ );
562
568
  }
563
569
 
564
570
  module.exports = {