@react-native/codegen 0.72.3 → 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 (77) hide show
  1. package/lib/CodegenSchema.d.ts +10 -10
  2. package/lib/CodegenSchema.js.flow +6 -1
  3. package/lib/SchemaValidator.d.ts +1 -1
  4. package/lib/generators/RNCodegen.d.ts +98 -0
  5. package/lib/generators/RNCodegen.js +33 -4
  6. package/lib/generators/RNCodegen.js.flow +35 -4
  7. package/lib/generators/components/ComponentsGeneratorUtils.js +2 -0
  8. package/lib/generators/components/ComponentsGeneratorUtils.js.flow +2 -0
  9. package/lib/generators/components/CppHelpers.js +2 -0
  10. package/lib/generators/components/CppHelpers.js.flow +2 -0
  11. package/lib/generators/components/GenerateEventEmitterCpp.js +33 -44
  12. package/lib/generators/components/GenerateEventEmitterCpp.js.flow +35 -44
  13. package/lib/generators/components/GenerateEventEmitterH.js +0 -2
  14. package/lib/generators/components/GenerateEventEmitterH.js.flow +0 -2
  15. package/lib/generators/components/GeneratePropsH.js +2 -0
  16. package/lib/generators/components/GeneratePropsH.js.flow +2 -0
  17. package/lib/generators/components/GeneratePropsJavaDelegate.js +2 -0
  18. package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +2 -0
  19. package/lib/generators/components/GeneratePropsJavaInterface.js +2 -0
  20. package/lib/generators/components/GeneratePropsJavaInterface.js.flow +2 -0
  21. package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -1
  22. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +5 -0
  23. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +6 -0
  24. package/lib/generators/components/GenerateViewConfigJs.js +1 -0
  25. package/lib/generators/components/GenerateViewConfigJs.js.flow +1 -0
  26. package/lib/generators/components/JavaHelpers.js +7 -0
  27. package/lib/generators/components/JavaHelpers.js.flow +8 -0
  28. package/lib/generators/components/__test_fixtures__/fixtures.js +29 -0
  29. package/lib/generators/components/__test_fixtures__/fixtures.js.flow +30 -0
  30. package/lib/generators/modules/GenerateModuleCpp.js +3 -3
  31. package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
  32. package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
  33. package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
  34. package/lib/parsers/error-utils.js +9 -15
  35. package/lib/parsers/error-utils.js.flow +12 -15
  36. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +5 -1
  37. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +5 -1
  38. package/lib/parsers/flow/components/componentsUtils.js +4 -0
  39. package/lib/parsers/flow/components/componentsUtils.js.flow +4 -0
  40. package/lib/parsers/flow/components/index.js +35 -168
  41. package/lib/parsers/flow/components/index.js.flow +33 -107
  42. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +4 -4
  43. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +4 -4
  44. package/lib/parsers/flow/modules/index.js +51 -327
  45. package/lib/parsers/flow/modules/index.js.flow +34 -258
  46. package/lib/parsers/flow/parser.js +84 -13
  47. package/lib/parsers/flow/parser.js.flow +80 -4
  48. package/lib/parsers/parser.d.ts +2 -2
  49. package/lib/parsers/parser.js.flow +84 -3
  50. package/lib/parsers/parserMock.js +53 -0
  51. package/lib/parsers/parserMock.js.flow +64 -1
  52. package/lib/parsers/parsers-commons.js +397 -4
  53. package/lib/parsers/parsers-commons.js.flow +391 -4
  54. package/lib/parsers/parsers-primitives.js +121 -3
  55. package/lib/parsers/parsers-primitives.js.flow +134 -3
  56. package/lib/parsers/schema/index.d.ts +1 -1
  57. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +5 -1
  58. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +5 -1
  59. package/lib/parsers/typescript/components/componentsUtils.js +8 -0
  60. package/lib/parsers/typescript/components/componentsUtils.js.flow +8 -0
  61. package/lib/parsers/typescript/components/index.js +37 -170
  62. package/lib/parsers/typescript/components/index.js.flow +33 -109
  63. package/lib/parsers/typescript/modules/index.js +113 -441
  64. package/lib/parsers/typescript/modules/index.js.flow +100 -309
  65. package/lib/parsers/typescript/parser.js +97 -13
  66. package/lib/parsers/typescript/parser.js.flow +94 -4
  67. package/lib/parsers/utils.js +4 -0
  68. package/lib/parsers/utils.js.flow +3 -0
  69. package/package.json +1 -1
  70. package/lib/parsers/flow/Visitor.js +0 -37
  71. package/lib/parsers/flow/Visitor.js.flow +0 -41
  72. package/lib/parsers/flow/components/options.js +0 -72
  73. package/lib/parsers/flow/components/options.js.flow +0 -87
  74. package/lib/parsers/typescript/Visitor.js +0 -42
  75. package/lib/parsers/typescript/Visitor.js.flow +0 -47
  76. package/lib/parsers/typescript/components/options.js +0 -72
  77. package/lib/parsers/typescript/components/options.js.flow +0 -87
@@ -18,9 +18,11 @@ import type {
18
18
  NativeModuleParamTypeAnnotation,
19
19
  NativeModuleEnumMemberType,
20
20
  NativeModuleEnumMembers,
21
+ NativeModuleAliasMap,
22
+ NativeModuleEnumMap,
21
23
  } from '../CodegenSchema';
22
24
  import type {ParserType} from './errors';
23
- import type {TypeDeclarationMap} from './utils';
25
+ import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
24
26
 
25
27
  /**
26
28
  * This is the main interface for Parsers of various languages.
@@ -159,10 +161,15 @@ export interface Parser {
159
161
  */
160
162
  isModuleInterface(node: $FlowFixMe): boolean;
161
163
 
164
+ /**
165
+ * Given a type name, it returns true if it is a generic type annotation
166
+ */
167
+ isGenericTypeAnnotation(type: $FlowFixMe): boolean;
168
+
162
169
  /**
163
170
  * Given a typeAnnotation, it returns the annotated element.
164
- * @paramater typeAnnotation: the annotation for a type.
165
- * @paramater types: a map of type declarations.
171
+ * @parameter typeAnnotation: the annotation for a type.
172
+ * @parameter types: a map of type declarations.
166
173
  * @returns: the annotated element.
167
174
  */
168
175
  extractAnnotatedElement(
@@ -174,4 +181,78 @@ export interface Parser {
174
181
  * Given the AST, returns the TypeDeclarationMap
175
182
  */
176
183
  getTypes(ast: $FlowFixMe): TypeDeclarationMap;
184
+
185
+ /**
186
+ * Given a callExpression, it returns the typeParameters of the callExpression.
187
+ * @parameter callExpression: the callExpression.
188
+ * @returns: the typeParameters of the callExpression or null if it does not exist.
189
+ */
190
+ callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null;
191
+
192
+ /**
193
+ * Given an array of properties from a Partial type, it returns an array of remaped properties.
194
+ * @parameter properties: properties from a Partial types.
195
+ * @parameter hasteModuleName: a string with the native module name.
196
+ * @parameter types: a map of type declarations.
197
+ * @parameter aliasMap: a map of type aliases.
198
+ * @parameter enumMap: a map of type enums.
199
+ * @parameter tryParse: a parser error capturer.
200
+ * @parameter cxxOnly: a boolean specifying if the module is Cxx only.
201
+ * @returns: an array of remaped properties
202
+ */
203
+ computePartialProperties(
204
+ properties: Array<$FlowFixMe>,
205
+ hasteModuleName: string,
206
+ types: TypeDeclarationMap,
207
+ aliasMap: {...NativeModuleAliasMap},
208
+ enumMap: {...NativeModuleEnumMap},
209
+ tryParse: ParserErrorCapturer,
210
+ cxxOnly: boolean,
211
+ ): Array<$FlowFixMe>;
212
+
213
+ /**
214
+ * Given a propertyValueType, it returns a boolean specifying if the property is a function type annotation.
215
+ * @parameter propertyValueType: the propertyValueType.
216
+ * @returns: a boolean specifying if the property is a function type annotation.
217
+ */
218
+ functionTypeAnnotation(propertyValueType: string): boolean;
219
+
220
+ /**
221
+ * Given a declaration, it returns the typeArgumentParams of the declaration.
222
+ * @parameter declaration: the declaration.
223
+ * @returns: the typeArgumentParams of the declaration.
224
+ */
225
+ getTypeArgumentParamsFromDeclaration(declaration: $FlowFixMe): $FlowFixMe;
226
+
227
+ /**
228
+ * Given a typeArgumentParams and funcArgumentParams it returns a native component type.
229
+ * @parameter typeArgumentParams: the typeArgumentParams.
230
+ * @parameter funcArgumentParams: the funcArgumentParams.
231
+ * @returns: a native component type.
232
+ */
233
+ getNativeComponentType(
234
+ typeArgumentParams: $FlowFixMe,
235
+ funcArgumentParams: $FlowFixMe,
236
+ ): {[string]: string};
237
+
238
+ /**
239
+ * Given a annotatedElement, it returns the properties of annotated element.
240
+ * @parameter annotatedElement: the annotated element.
241
+ * @returns: the properties of annotated element.
242
+ */
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;
177
258
  }
@@ -158,10 +158,63 @@ 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
  }
164
167
  getTypes(ast) {
165
168
  return {};
166
169
  }
170
+ callExpressionTypeParameters(callExpression) {
171
+ return callExpression.typeArguments || null;
172
+ }
173
+ computePartialProperties(
174
+ properties,
175
+ hasteModuleName,
176
+ types,
177
+ aliasMap,
178
+ enumMap,
179
+ tryParse,
180
+ cxxOnly,
181
+ ) {
182
+ return [
183
+ {
184
+ name: 'a',
185
+ optional: true,
186
+ typeAnnotation: {
187
+ type: 'StringTypeAnnotation',
188
+ },
189
+ },
190
+ {
191
+ name: 'b',
192
+ optional: true,
193
+ typeAnnotation: {
194
+ type: 'BooleanTypeAnnotation',
195
+ },
196
+ },
197
+ ];
198
+ }
199
+ functionTypeAnnotation(propertyValueType) {
200
+ return propertyValueType === 'FunctionTypeAnnotation';
201
+ }
202
+ getTypeArgumentParamsFromDeclaration(declaration) {
203
+ return declaration.typeArguments.params;
204
+ }
205
+ getNativeComponentType(typeArgumentParams, funcArgumentParams) {
206
+ return {
207
+ propsTypeName: typeArgumentParams[0].id.name,
208
+ componentName: funcArgumentParams[0].value,
209
+ };
210
+ }
211
+ getAnnotatedElementProperties(annotatedElement) {
212
+ return annotatedElement.right.properties;
213
+ }
214
+ bodyProperties(typeAlias) {
215
+ return typeAlias.body.properties;
216
+ }
217
+ convertKeywordToTypeAnnotation(keyword) {
218
+ return keyword;
219
+ }
167
220
  }
@@ -20,8 +20,10 @@ import type {
20
20
  NativeModuleParamTypeAnnotation,
21
21
  NativeModuleEnumMemberType,
22
22
  NativeModuleEnumMembers,
23
+ NativeModuleAliasMap,
24
+ NativeModuleEnumMap,
23
25
  } from '../CodegenSchema';
24
- import type {TypeDeclarationMap} from './utils';
26
+ import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
25
27
 
26
28
  // $FlowFixMe[untyped-import] there's no flowtype flow-parser
27
29
  const flowParser = require('flow-parser');
@@ -170,6 +172,10 @@ export class MockedParser implements Parser {
170
172
  );
171
173
  }
172
174
 
175
+ isGenericTypeAnnotation(type: $FlowFixMe): boolean {
176
+ return true;
177
+ }
178
+
173
179
  extractAnnotatedElement(
174
180
  typeAnnotation: $FlowFixMe,
175
181
  types: TypeDeclarationMap,
@@ -180,4 +186,61 @@ export class MockedParser implements Parser {
180
186
  getTypes(ast: $FlowFixMe): TypeDeclarationMap {
181
187
  return {};
182
188
  }
189
+
190
+ callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
191
+ return callExpression.typeArguments || null;
192
+ }
193
+
194
+ computePartialProperties(
195
+ properties: Array<$FlowFixMe>,
196
+ hasteModuleName: string,
197
+ types: TypeDeclarationMap,
198
+ aliasMap: {...NativeModuleAliasMap},
199
+ enumMap: {...NativeModuleEnumMap},
200
+ tryParse: ParserErrorCapturer,
201
+ cxxOnly: boolean,
202
+ ): Array<$FlowFixMe> {
203
+ return [
204
+ {
205
+ name: 'a',
206
+ optional: true,
207
+ typeAnnotation: {type: 'StringTypeAnnotation'},
208
+ },
209
+ {
210
+ name: 'b',
211
+ optional: true,
212
+ typeAnnotation: {type: 'BooleanTypeAnnotation'},
213
+ },
214
+ ];
215
+ }
216
+
217
+ functionTypeAnnotation(propertyValueType: string): boolean {
218
+ return propertyValueType === 'FunctionTypeAnnotation';
219
+ }
220
+
221
+ getTypeArgumentParamsFromDeclaration(declaration: $FlowFixMe): $FlowFixMe {
222
+ return declaration.typeArguments.params;
223
+ }
224
+
225
+ getNativeComponentType(
226
+ typeArgumentParams: $FlowFixMe,
227
+ funcArgumentParams: $FlowFixMe,
228
+ ): {[string]: string} {
229
+ return {
230
+ propsTypeName: typeArgumentParams[0].id.name,
231
+ componentName: funcArgumentParams[0].value,
232
+ };
233
+ }
234
+
235
+ getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
236
+ return annotatedElement.right.properties;
237
+ }
238
+
239
+ bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
240
+ return typeAlias.body.properties;
241
+ }
242
+
243
+ convertKeywordToTypeAnnotation(keyword: string): string {
244
+ return keyword;
245
+ }
183
246
  }
@@ -10,6 +10,68 @@
10
10
 
11
11
  'use strict';
12
12
 
13
+ function ownKeys(object, enumerableOnly) {
14
+ var keys = Object.keys(object);
15
+ if (Object.getOwnPropertySymbols) {
16
+ var symbols = Object.getOwnPropertySymbols(object);
17
+ enumerableOnly &&
18
+ (symbols = symbols.filter(function (sym) {
19
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
20
+ })),
21
+ keys.push.apply(keys, symbols);
22
+ }
23
+ return keys;
24
+ }
25
+ function _objectSpread(target) {
26
+ for (var i = 1; i < arguments.length; i++) {
27
+ var source = null != arguments[i] ? arguments[i] : {};
28
+ i % 2
29
+ ? ownKeys(Object(source), !0).forEach(function (key) {
30
+ _defineProperty(target, key, source[key]);
31
+ })
32
+ : Object.getOwnPropertyDescriptors
33
+ ? Object.defineProperties(
34
+ target,
35
+ Object.getOwnPropertyDescriptors(source),
36
+ )
37
+ : ownKeys(Object(source)).forEach(function (key) {
38
+ Object.defineProperty(
39
+ target,
40
+ key,
41
+ Object.getOwnPropertyDescriptor(source, key),
42
+ );
43
+ });
44
+ }
45
+ return target;
46
+ }
47
+ function _defineProperty(obj, key, value) {
48
+ key = _toPropertyKey(key);
49
+ if (key in obj) {
50
+ Object.defineProperty(obj, key, {
51
+ value: value,
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ });
56
+ } else {
57
+ obj[key] = value;
58
+ }
59
+ return obj;
60
+ }
61
+ function _toPropertyKey(arg) {
62
+ var key = _toPrimitive(arg, 'string');
63
+ return typeof key === 'symbol' ? key : String(key);
64
+ }
65
+ function _toPrimitive(input, hint) {
66
+ if (typeof input !== 'object' || input === null) return input;
67
+ var prim = input[Symbol.toPrimitive];
68
+ if (prim !== undefined) {
69
+ var res = prim.call(input, hint || 'default');
70
+ if (typeof res !== 'object') return res;
71
+ throw new TypeError('@@toPrimitive must return a primitive value.');
72
+ }
73
+ return (hint === 'string' ? String : Number)(input);
74
+ }
13
75
  function _slicedToArray(arr, i) {
14
76
  return (
15
77
  _arrayWithHoles(arr) ||
@@ -81,7 +143,10 @@ function _arrayWithHoles(arr) {
81
143
  const _require = require('./utils'),
82
144
  getConfigType = _require.getConfigType,
83
145
  extractNativeModuleName = _require.extractNativeModuleName,
84
- createParserErrorCapturer = _require.createParserErrorCapturer;
146
+ createParserErrorCapturer = _require.createParserErrorCapturer,
147
+ visit = _require.visit,
148
+ isModuleRegistryCall = _require.isModuleRegistryCall,
149
+ verifyPlatforms = _require.verifyPlatforms;
85
150
  const _require2 = require('./error-utils'),
86
151
  throwIfPropertyValueTypeIsUnsupported =
87
152
  _require2.throwIfPropertyValueTypeIsUnsupported,
@@ -89,7 +154,22 @@ const _require2 = require('./error-utils'),
89
154
  _require2.throwIfUnsupportedFunctionParamTypeAnnotationParserError,
90
155
  throwIfUnsupportedFunctionReturnTypeAnnotationParserError =
91
156
  _require2.throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
92
- throwIfModuleTypeIsUnsupported = _require2.throwIfModuleTypeIsUnsupported;
157
+ throwIfModuleTypeIsUnsupported = _require2.throwIfModuleTypeIsUnsupported,
158
+ throwIfUnusedModuleInterfaceParserError =
159
+ _require2.throwIfUnusedModuleInterfaceParserError,
160
+ throwIfMoreThanOneModuleRegistryCalls =
161
+ _require2.throwIfMoreThanOneModuleRegistryCalls,
162
+ throwIfWrongNumberOfCallExpressionArgs =
163
+ _require2.throwIfWrongNumberOfCallExpressionArgs,
164
+ throwIfUntypedModule = _require2.throwIfUntypedModule,
165
+ throwIfIncorrectModuleRegistryCallTypeParameterParserError =
166
+ _require2.throwIfIncorrectModuleRegistryCallTypeParameterParserError,
167
+ throwIfIncorrectModuleRegistryCallArgument =
168
+ _require2.throwIfIncorrectModuleRegistryCallArgument,
169
+ throwIfModuleInterfaceNotFound = _require2.throwIfModuleInterfaceNotFound,
170
+ throwIfMoreThanOneModuleInterfaceParserError =
171
+ _require2.throwIfMoreThanOneModuleInterfaceParserError,
172
+ throwIfModuleInterfaceIsMisnamed = _require2.throwIfModuleInterfaceIsMisnamed;
93
173
  const _require3 = require('./errors'),
94
174
  MissingTypeParameterGenericParserError =
95
175
  _require3.MissingTypeParameterGenericParserError,
@@ -337,7 +417,7 @@ function buildPropertySchema(
337
417
  property.value,
338
418
  key.name,
339
419
  value.type,
340
- parser.language(),
420
+ parser,
341
421
  );
342
422
  return {
343
423
  name: methodName,
@@ -366,6 +446,8 @@ function buildSchemaFromConfigType(
366
446
  buildComponentSchema,
367
447
  buildModuleSchema,
368
448
  parser,
449
+ resolveTypeAnnotation,
450
+ translateTypeAnnotation,
369
451
  ) {
370
452
  switch (configType) {
371
453
  case 'component': {
@@ -381,7 +463,14 @@ function buildSchemaFromConfigType(
381
463
  parsingErrors = _createParserErrorCap2[0],
382
464
  tryParse = _createParserErrorCap2[1];
383
465
  const schema = tryParse(() =>
384
- buildModuleSchema(nativeModuleName, ast, tryParse, parser),
466
+ buildModuleSchema(
467
+ nativeModuleName,
468
+ ast,
469
+ tryParse,
470
+ parser,
471
+ resolveTypeAnnotation,
472
+ translateTypeAnnotation,
473
+ ),
385
474
  );
386
475
  if (parsingErrors.length > 0) {
387
476
  /**
@@ -414,6 +503,8 @@ function buildSchema(
414
503
  buildModuleSchema,
415
504
  Visitor,
416
505
  parser,
506
+ resolveTypeAnnotation,
507
+ translateTypeAnnotation,
417
508
  ) {
418
509
  // Early return for non-Spec JavaScript files
419
510
  if (
@@ -434,8 +525,302 @@ function buildSchema(
434
525
  buildComponentSchema,
435
526
  buildModuleSchema,
436
527
  parser,
528
+ resolveTypeAnnotation,
529
+ translateTypeAnnotation,
530
+ );
531
+ }
532
+ function createComponentConfig(foundConfig, commandsTypeNames) {
533
+ return _objectSpread(
534
+ _objectSpread({}, foundConfig),
535
+ {},
536
+ {
537
+ commandTypeName:
538
+ commandsTypeNames[0] == null
539
+ ? null
540
+ : commandsTypeNames[0].commandTypeName,
541
+ commandOptionsExpression:
542
+ commandsTypeNames[0] == null
543
+ ? null
544
+ : commandsTypeNames[0].commandOptionsExpression,
545
+ },
437
546
  );
438
547
  }
548
+ const parseModuleName = (hasteModuleName, moduleSpec, ast, parser) => {
549
+ const callExpressions = [];
550
+ visit(ast, {
551
+ CallExpression(node) {
552
+ if (isModuleRegistryCall(node)) {
553
+ callExpressions.push(node);
554
+ }
555
+ },
556
+ });
557
+ throwIfUnusedModuleInterfaceParserError(
558
+ hasteModuleName,
559
+ moduleSpec,
560
+ callExpressions,
561
+ );
562
+ throwIfMoreThanOneModuleRegistryCalls(
563
+ hasteModuleName,
564
+ callExpressions,
565
+ callExpressions.length,
566
+ );
567
+ const callExpression = callExpressions[0];
568
+ const typeParameters = parser.callExpressionTypeParameters(callExpression);
569
+ const methodName = callExpression.callee.property.name;
570
+ throwIfWrongNumberOfCallExpressionArgs(
571
+ hasteModuleName,
572
+ callExpression,
573
+ methodName,
574
+ callExpression.arguments.length,
575
+ );
576
+ throwIfIncorrectModuleRegistryCallArgument(
577
+ hasteModuleName,
578
+ callExpression.arguments[0],
579
+ methodName,
580
+ );
581
+ const $moduleName = callExpression.arguments[0].value;
582
+ throwIfUntypedModule(
583
+ typeParameters,
584
+ hasteModuleName,
585
+ callExpression,
586
+ methodName,
587
+ $moduleName,
588
+ );
589
+ throwIfIncorrectModuleRegistryCallTypeParameterParserError(
590
+ hasteModuleName,
591
+ typeParameters,
592
+ methodName,
593
+ $moduleName,
594
+ parser,
595
+ );
596
+ return $moduleName;
597
+ };
598
+ const buildModuleSchema = (
599
+ hasteModuleName,
600
+ ast,
601
+ tryParse,
602
+ parser,
603
+ resolveTypeAnnotation,
604
+ translateTypeAnnotation,
605
+ ) => {
606
+ const language = parser.language();
607
+ const types = parser.getTypes(ast);
608
+ const moduleSpecs = Object.values(types).filter(t =>
609
+ parser.isModuleInterface(t),
610
+ );
611
+ throwIfModuleInterfaceNotFound(
612
+ moduleSpecs.length,
613
+ hasteModuleName,
614
+ ast,
615
+ language,
616
+ );
617
+ throwIfMoreThanOneModuleInterfaceParserError(
618
+ hasteModuleName,
619
+ moduleSpecs,
620
+ language,
621
+ );
622
+ const _moduleSpecs = _slicedToArray(moduleSpecs, 1),
623
+ moduleSpec = _moduleSpecs[0];
624
+ throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
625
+
626
+ // Parse Module Name
627
+ const moduleName = parseModuleName(hasteModuleName, moduleSpec, ast, parser);
628
+
629
+ // Some module names use platform suffix to indicate platform-exclusive modules.
630
+ // Eventually this should be made explicit in the Flow type itself.
631
+ // Also check the hasteModuleName for platform suffix.
632
+ // Note: this shape is consistent with ComponentSchema.
633
+ const _verifyPlatforms = verifyPlatforms(hasteModuleName, moduleName),
634
+ cxxOnly = _verifyPlatforms.cxxOnly,
635
+ excludedPlatforms = _verifyPlatforms.excludedPlatforms;
636
+ const properties =
637
+ language === 'Flow' ? moduleSpec.body.properties : moduleSpec.body.body;
638
+
639
+ // $FlowFixMe[missing-type-arg]
640
+ return properties
641
+ .filter(
642
+ property =>
643
+ property.type === 'ObjectTypeProperty' ||
644
+ property.type === 'TSPropertySignature' ||
645
+ property.type === 'TSMethodSignature',
646
+ )
647
+ .map(property => {
648
+ const aliasMap = {};
649
+ const enumMap = {};
650
+ return tryParse(() => ({
651
+ aliasMap,
652
+ enumMap,
653
+ propertyShape: buildPropertySchema(
654
+ hasteModuleName,
655
+ property,
656
+ types,
657
+ aliasMap,
658
+ enumMap,
659
+ tryParse,
660
+ cxxOnly,
661
+ resolveTypeAnnotation,
662
+ translateTypeAnnotation,
663
+ parser,
664
+ ),
665
+ }));
666
+ })
667
+ .filter(Boolean)
668
+ .reduce(
669
+ (moduleSchema, {aliasMap, enumMap, propertyShape}) => ({
670
+ type: 'NativeModule',
671
+ aliasMap: _objectSpread(
672
+ _objectSpread({}, moduleSchema.aliasMap),
673
+ aliasMap,
674
+ ),
675
+ enumMap: _objectSpread(
676
+ _objectSpread({}, moduleSchema.enumMap),
677
+ enumMap,
678
+ ),
679
+ spec: {
680
+ properties: [...moduleSchema.spec.properties, propertyShape],
681
+ },
682
+ moduleName: moduleSchema.moduleName,
683
+ excludedPlatforms: moduleSchema.excludedPlatforms,
684
+ }),
685
+ {
686
+ type: 'NativeModule',
687
+ aliasMap: {},
688
+ enumMap: {},
689
+ spec: {
690
+ properties: [],
691
+ },
692
+ moduleName,
693
+ excludedPlatforms:
694
+ excludedPlatforms.length !== 0 ? [...excludedPlatforms] : undefined,
695
+ },
696
+ );
697
+ };
698
+
699
+ /**
700
+ * This function is used to find the type of a native component
701
+ * provided the default exports statement from generated AST.
702
+ * @param statement The statement to be parsed.
703
+ * @param foundConfigs The 'mutable' array of configs that have been found.
704
+ * @param parser The language parser to be used.
705
+ * @returns void
706
+ */
707
+ function findNativeComponentType(statement, foundConfigs, parser) {
708
+ let declaration = statement.declaration;
709
+
710
+ // codegenNativeComponent can be nested inside a cast
711
+ // expression so we need to go one level deeper
712
+ if (
713
+ declaration.type === 'TSAsExpression' ||
714
+ declaration.type === 'TypeCastExpression'
715
+ ) {
716
+ declaration = declaration.expression;
717
+ }
718
+ try {
719
+ if (declaration.callee.name === 'codegenNativeComponent') {
720
+ const typeArgumentParams =
721
+ parser.getTypeArgumentParamsFromDeclaration(declaration);
722
+ const funcArgumentParams = declaration.arguments;
723
+ const nativeComponentType = parser.getNativeComponentType(
724
+ typeArgumentParams,
725
+ funcArgumentParams,
726
+ );
727
+ if (funcArgumentParams.length > 1) {
728
+ nativeComponentType.optionsExpression = funcArgumentParams[1];
729
+ }
730
+ foundConfigs.push(nativeComponentType);
731
+ }
732
+ } catch (e) {
733
+ // ignore
734
+ }
735
+ }
736
+ function getCommandOptions(commandOptionsExpression) {
737
+ if (commandOptionsExpression == null) {
738
+ return null;
739
+ }
740
+ let foundOptions;
741
+ try {
742
+ foundOptions = commandOptionsExpression.properties.reduce(
743
+ (options, prop) => {
744
+ options[prop.key.name] = (
745
+ (prop && prop.value && prop.value.elements) ||
746
+ []
747
+ ).map(element => element && element.value);
748
+ return options;
749
+ },
750
+ {},
751
+ );
752
+ } catch (e) {
753
+ throw new Error(
754
+ 'Failed to parse command options, please check that they are defined correctly',
755
+ );
756
+ }
757
+ return foundOptions;
758
+ }
759
+ function getOptions(optionsExpression) {
760
+ if (!optionsExpression) {
761
+ return null;
762
+ }
763
+ let foundOptions;
764
+ try {
765
+ foundOptions = optionsExpression.properties.reduce((options, prop) => {
766
+ if (prop.value.type === 'ArrayExpression') {
767
+ options[prop.key.name] = prop.value.elements.map(
768
+ element => element.value,
769
+ );
770
+ } else {
771
+ options[prop.key.name] = prop.value.value;
772
+ }
773
+ return options;
774
+ }, {});
775
+ } catch (e) {
776
+ throw new Error(
777
+ 'Failed to parse codegen options, please check that they are defined correctly',
778
+ );
779
+ }
780
+ if (
781
+ foundOptions.paperComponentName &&
782
+ foundOptions.paperComponentNameDeprecated
783
+ ) {
784
+ throw new Error(
785
+ 'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
786
+ );
787
+ }
788
+ return foundOptions;
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
+ }
439
824
  module.exports = {
440
825
  wrapModuleSchema,
441
826
  unwrapNullable,
@@ -447,4 +832,12 @@ module.exports = {
447
832
  buildPropertySchema,
448
833
  buildSchemaFromConfigType,
449
834
  buildSchema,
835
+ createComponentConfig,
836
+ parseModuleName,
837
+ buildModuleSchema,
838
+ findNativeComponentType,
839
+ propertyNames,
840
+ getCommandOptions,
841
+ getOptions,
842
+ getCommandTypeNameAndOptionsExpression,
450
843
  };