@react-native/codegen 0.72.2 → 0.72.4

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/GeneratePropsH.js +2 -0
  12. package/lib/generators/components/GeneratePropsH.js.flow +2 -0
  13. package/lib/generators/components/GeneratePropsJavaDelegate.js +2 -0
  14. package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +2 -0
  15. package/lib/generators/components/GeneratePropsJavaInterface.js +2 -0
  16. package/lib/generators/components/GeneratePropsJavaInterface.js.flow +2 -0
  17. package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -1
  18. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +5 -0
  19. package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +6 -0
  20. package/lib/generators/components/GenerateViewConfigJs.js +1 -0
  21. package/lib/generators/components/GenerateViewConfigJs.js.flow +1 -0
  22. package/lib/generators/components/JavaHelpers.js +7 -0
  23. package/lib/generators/components/JavaHelpers.js.flow +8 -0
  24. package/lib/generators/components/__test_fixtures__/fixtures.js +29 -0
  25. package/lib/generators/components/__test_fixtures__/fixtures.js.flow +30 -0
  26. package/lib/parsers/error-utils.js +50 -15
  27. package/lib/parsers/error-utils.js.flow +57 -15
  28. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +5 -1
  29. package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +5 -1
  30. package/lib/parsers/flow/components/componentsUtils.js +4 -0
  31. package/lib/parsers/flow/components/componentsUtils.js.flow +4 -0
  32. package/lib/parsers/flow/components/index.js +27 -126
  33. package/lib/parsers/flow/components/index.js.flow +22 -52
  34. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +4 -4
  35. package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +4 -4
  36. package/lib/parsers/flow/modules/index.js +41 -317
  37. package/lib/parsers/flow/modules/index.js.flow +19 -247
  38. package/lib/parsers/flow/parser.js +119 -12
  39. package/lib/parsers/flow/parser.js.flow +124 -3
  40. package/lib/parsers/flow/utils.js +0 -38
  41. package/lib/parsers/flow/utils.js.flow +0 -38
  42. package/lib/parsers/parser.d.ts +2 -2
  43. package/lib/parsers/parser.js.flow +84 -0
  44. package/lib/parsers/parserMock.js +58 -0
  45. package/lib/parsers/parserMock.js.flow +72 -0
  46. package/lib/parsers/parsers-commons.js +362 -5
  47. package/lib/parsers/parsers-commons.js.flow +351 -8
  48. package/lib/parsers/parsers-primitives.js +119 -3
  49. package/lib/parsers/parsers-primitives.js.flow +128 -3
  50. package/lib/parsers/schema/index.d.ts +1 -1
  51. package/lib/parsers/{typescript/components/schema.js.flow → schema.js.flow} +1 -1
  52. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +5 -1
  53. package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +5 -1
  54. package/lib/parsers/typescript/components/componentsUtils.js +8 -0
  55. package/lib/parsers/typescript/components/componentsUtils.js.flow +8 -0
  56. package/lib/parsers/typescript/components/index.js +28 -127
  57. package/lib/parsers/typescript/components/index.js.flow +24 -54
  58. package/lib/parsers/typescript/modules/index.js +103 -434
  59. package/lib/parsers/typescript/modules/index.js.flow +85 -299
  60. package/lib/parsers/typescript/parser.js +113 -12
  61. package/lib/parsers/typescript/parser.js.flow +115 -3
  62. package/lib/parsers/typescript/utils.js +0 -31
  63. package/lib/parsers/typescript/utils.js.flow +0 -31
  64. package/lib/parsers/utils.js +4 -0
  65. package/lib/parsers/utils.js.flow +3 -0
  66. package/package.json +1 -1
  67. package/lib/parsers/flow/Visitor.js +0 -37
  68. package/lib/parsers/flow/Visitor.js.flow +0 -41
  69. package/lib/parsers/flow/components/options.js +0 -72
  70. package/lib/parsers/flow/components/options.js.flow +0 -87
  71. package/lib/parsers/flow/components/schema.js +0 -106
  72. package/lib/parsers/flow/components/schema.js.flow +0 -62
  73. package/lib/parsers/typescript/Visitor.js +0 -42
  74. package/lib/parsers/typescript/Visitor.js.flow +0 -47
  75. package/lib/parsers/typescript/components/options.js +0 -72
  76. package/lib/parsers/typescript/components/options.js.flow +0 -87
  77. /package/lib/parsers/{typescript/components/schema.js → schema.js} +0 -0
@@ -34,7 +34,6 @@ import type {
34
34
  NativeModuleObjectTypeAnnotation,
35
35
  NativeModuleEnumDeclaration,
36
36
  } from '../CodegenSchema';
37
- import type {ParserType} from './errors';
38
37
  import type {Parser} from './parser';
39
38
  import type {
40
39
  ParserErrorCapturer,
@@ -50,6 +49,8 @@ const {
50
49
 
51
50
  const {
52
51
  throwIfArrayElementTypeAnnotationIsUnsupported,
52
+ throwIfPartialNotAnnotatingTypeParameter,
53
+ throwIfPartialWithMoreParameter,
53
54
  } = require('./error-utils');
54
55
  const {nullGuard} = require('./parsers-utils');
55
56
  const {
@@ -59,6 +60,8 @@ const {
59
60
  translateFunctionTypeAnnotation,
60
61
  } = require('./parsers-commons');
61
62
 
63
+ const {isModuleRegistryCall} = require('./utils');
64
+
62
65
  function emitBoolean(nullable: boolean): Nullable<BooleanTypeAnnotation> {
63
66
  return wrapNullable(nullable, {
64
67
  type: 'BooleanTypeAnnotation',
@@ -204,7 +207,6 @@ function typeEnumResolution(
204
207
  typeResolution: TypeResolutionStatus,
205
208
  nullable: boolean,
206
209
  hasteModuleName: string,
207
- language: ParserType,
208
210
  enumMap: {...NativeModuleEnumMap},
209
211
  parser: Parser,
210
212
  ): Nullable<NativeModuleEnumDeclaration> {
@@ -212,7 +214,7 @@ function typeEnumResolution(
212
214
  throw new UnsupportedTypeAnnotationParserError(
213
215
  hasteModuleName,
214
216
  typeAnnotation,
215
- language,
217
+ parser.language(),
216
218
  );
217
219
  }
218
220
 
@@ -439,6 +441,126 @@ function emitArrayType(
439
441
  );
440
442
  }
441
443
 
444
+ function Visitor(infoMap: {isComponent: boolean, isModule: boolean}): {
445
+ [type: string]: (node: $FlowFixMe) => void,
446
+ } {
447
+ return {
448
+ CallExpression(node: $FlowFixMe) {
449
+ if (
450
+ node.callee.type === 'Identifier' &&
451
+ node.callee.name === 'codegenNativeComponent'
452
+ ) {
453
+ infoMap.isComponent = true;
454
+ }
455
+
456
+ if (isModuleRegistryCall(node)) {
457
+ infoMap.isModule = true;
458
+ }
459
+ },
460
+ InterfaceExtends(node: $FlowFixMe) {
461
+ if (node.id.name === 'TurboModule') {
462
+ infoMap.isModule = true;
463
+ }
464
+ },
465
+ TSInterfaceDeclaration(node: $FlowFixMe) {
466
+ if (
467
+ Array.isArray(node.extends) &&
468
+ node.extends.some(
469
+ extension => extension.expression.name === 'TurboModule',
470
+ )
471
+ ) {
472
+ infoMap.isModule = true;
473
+ }
474
+ },
475
+ };
476
+ }
477
+
478
+ function emitPartial(
479
+ hasteModuleName: string,
480
+ typeAnnotation: $FlowFixMe,
481
+ types: TypeDeclarationMap,
482
+ aliasMap: {...NativeModuleAliasMap},
483
+ enumMap: {...NativeModuleEnumMap},
484
+ tryParse: ParserErrorCapturer,
485
+ cxxOnly: boolean,
486
+ nullable: boolean,
487
+ parser: Parser,
488
+ ): Nullable<NativeModuleTypeAnnotation> {
489
+ throwIfPartialWithMoreParameter(typeAnnotation);
490
+
491
+ throwIfPartialNotAnnotatingTypeParameter(typeAnnotation, types, parser);
492
+
493
+ const annotatedElement = parser.extractAnnotatedElement(
494
+ typeAnnotation,
495
+ types,
496
+ );
497
+ const annotatedElementProperties =
498
+ parser.getAnnotatedElementProperties(annotatedElement);
499
+
500
+ const partialProperties = parser.computePartialProperties(
501
+ annotatedElementProperties,
502
+ hasteModuleName,
503
+ types,
504
+ aliasMap,
505
+ enumMap,
506
+ tryParse,
507
+ cxxOnly,
508
+ );
509
+
510
+ return emitObject(nullable, partialProperties);
511
+ }
512
+
513
+ function emitCommonTypes(
514
+ hasteModuleName: string,
515
+ types: TypeDeclarationMap,
516
+ typeAnnotation: $FlowFixMe,
517
+ aliasMap: {...NativeModuleAliasMap},
518
+ enumMap: {...NativeModuleEnumMap},
519
+ tryParse: ParserErrorCapturer,
520
+ cxxOnly: boolean,
521
+ nullable: boolean,
522
+ parser: Parser,
523
+ ): $FlowFixMe {
524
+ const genericTypeAnnotationName =
525
+ parser.nameForGenericTypeAnnotation(typeAnnotation);
526
+
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
+ }
561
+ }
562
+ }
563
+
442
564
  module.exports = {
443
565
  emitArrayType,
444
566
  emitBoolean,
@@ -456,7 +578,10 @@ module.exports = {
456
578
  emitStringish,
457
579
  emitMixed,
458
580
  emitUnion,
581
+ emitPartial,
582
+ emitCommonTypes,
459
583
  typeAliasResolution,
460
584
  typeEnumResolution,
461
585
  translateArrayTypeAnnotation,
586
+ Visitor,
462
587
  };
@@ -5,6 +5,6 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import type { SchemaType } from "../../CodegenSchema";
8
+ import type { SchemaType } from '../../CodegenSchema';
9
9
 
10
10
  export declare function parse(filename: string): SchemaType | undefined;
@@ -18,7 +18,7 @@ import type {
18
18
  ExtendsPropsShape,
19
19
  SchemaType,
20
20
  OptionsShape,
21
- } from '../../../CodegenSchema.js';
21
+ } from '../CodegenSchema.js';
22
22
 
23
23
  export type ComponentSchemaBuilderConfig = $ReadOnly<{
24
24
  filename: string,
@@ -193,7 +193,7 @@ const ALL_PROP_TYPES_NO_EVENTS = `
193
193
 
194
194
  const codegenNativeComponent = require('codegenNativeComponent');
195
195
 
196
- import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
196
+ import type {Int32, Double, Float, WithDefault, UnsafeMixed} from 'CodegenTypes';
197
197
  import type {ImageSource} from 'ImageSource';
198
198
  import type {
199
199
  ColorValue,
@@ -306,6 +306,10 @@ export interface ModuleProps extends ViewProps {
306
306
  dimension_optional_key?: DimensionValue;
307
307
  dimension_optional_value: DimensionValue | null | undefined;
308
308
  dimension_optional_both?: DimensionValue | null | undefined;
309
+
310
+ // Mixed props
311
+ mixed_required: UnsafeMixed,
312
+ mixed_optional_key?: UnsafeMixed,
309
313
  }
310
314
 
311
315
  export default codegenNativeComponent<ModuleProps>(
@@ -197,7 +197,7 @@ const ALL_PROP_TYPES_NO_EVENTS = `
197
197
 
198
198
  const codegenNativeComponent = require('codegenNativeComponent');
199
199
 
200
- import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
200
+ import type {Int32, Double, Float, WithDefault, UnsafeMixed} from 'CodegenTypes';
201
201
  import type {ImageSource} from 'ImageSource';
202
202
  import type {
203
203
  ColorValue,
@@ -310,6 +310,10 @@ export interface ModuleProps extends ViewProps {
310
310
  dimension_optional_key?: DimensionValue;
311
311
  dimension_optional_value: DimensionValue | null | undefined;
312
312
  dimension_optional_both?: DimensionValue | null | undefined;
313
+
314
+ // Mixed props
315
+ mixed_required: UnsafeMixed,
316
+ mixed_optional_key?: UnsafeMixed,
313
317
  }
314
318
 
315
319
  export default codegenNativeComponent<ModuleProps>(
@@ -252,6 +252,10 @@ function getCommonTypeAnnotation(
252
252
  return {
253
253
  type: 'StringTypeAnnotation',
254
254
  };
255
+ case 'UnsafeMixed':
256
+ return {
257
+ type: 'MixedTypeAnnotation',
258
+ };
255
259
  default:
256
260
  return undefined;
257
261
  }
@@ -389,6 +393,10 @@ function getTypeAnnotation(name, annotation, defaultValue, types, buildSchema) {
389
393
  throw new Error(
390
394
  `Cannot use "${type}" type annotation for "${name}": must use a specific numeric type like Int32, Double, or Float`,
391
395
  );
396
+ case 'TSFunctionType':
397
+ throw new Error(
398
+ `Cannot use "${type}" type annotation for "${name}": must use a specific function type like BubblingEventHandler, or DirectEventHandler`,
399
+ );
392
400
  default:
393
401
  type;
394
402
  throw new Error(`Unknown prop type for "${name}": "${type}"`);
@@ -262,6 +262,10 @@ function getCommonTypeAnnotation<T>(
262
262
  return {
263
263
  type: 'StringTypeAnnotation',
264
264
  };
265
+ case 'UnsafeMixed':
266
+ return {
267
+ type: 'MixedTypeAnnotation',
268
+ };
265
269
  default:
266
270
  return undefined;
267
271
  }
@@ -416,6 +420,10 @@ function getTypeAnnotation<T>(
416
420
  throw new Error(
417
421
  `Cannot use "${type}" type annotation for "${name}": must use a specific numeric type like Int32, Double, or Float`,
418
422
  );
423
+ case 'TSFunctionType':
424
+ throw new Error(
425
+ `Cannot use "${type}" type annotation for "${name}": must use a specific function type like BubblingEventHandler, or DirectEventHandler`,
426
+ );
419
427
  default:
420
428
  (type: empty);
421
429
  throw new Error(`Unknown prop type for "${name}": "${type}"`);
@@ -10,116 +10,34 @@
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
- }
75
- const _require = require('../utils'),
76
- getTypes = _require.getTypes;
77
- const _require2 = require('./commands'),
78
- getCommands = _require2.getCommands;
79
- const _require3 = require('./events'),
80
- getEvents = _require3.getEvents;
81
- const _require4 = require('./extends'),
82
- categorizeProps = _require4.categorizeProps;
83
- const _require5 = require('./options'),
84
- getCommandOptions = _require5.getCommandOptions,
85
- getOptions = _require5.getOptions;
86
- const _require6 = require('./props'),
87
- getProps = _require6.getProps;
88
- const _require7 = require('./componentsUtils.js'),
89
- getProperties = _require7.getProperties;
13
+ const _require = require('./commands'),
14
+ getCommands = _require.getCommands;
15
+ const _require2 = require('./events'),
16
+ getEvents = _require2.getEvents;
17
+ const _require3 = require('./extends'),
18
+ categorizeProps = _require3.categorizeProps;
19
+ const _require4 = require('./props'),
20
+ getProps = _require4.getProps;
21
+ const _require5 = require('./componentsUtils.js'),
22
+ getProperties = _require5.getProperties;
23
+ const _require6 = require('../../error-utils'),
24
+ throwIfMoreThanOneCodegenNativecommands =
25
+ _require6.throwIfMoreThanOneCodegenNativecommands;
26
+ const _require7 = require('../../parsers-commons'),
27
+ createComponentConfig = _require7.createComponentConfig,
28
+ findNativeComponentType = _require7.findNativeComponentType,
29
+ getCommandOptions = _require7.getCommandOptions,
30
+ getOptions = _require7.getOptions;
90
31
 
91
- /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
92
- * LTI update could not be added via codemod */
93
- function findComponentConfig(ast) {
32
+ // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
33
+ function findComponentConfig(ast, parser) {
94
34
  const foundConfigs = [];
95
35
  const defaultExports = ast.body.filter(
96
36
  node => node.type === 'ExportDefaultDeclaration',
97
37
  );
98
- defaultExports.forEach(statement => {
99
- let declaration = statement.declaration;
100
-
101
- // codegenNativeComponent can be nested inside a cast
102
- // expression so we need to go one level deeper
103
- if (declaration.type === 'TSAsExpression') {
104
- declaration = declaration.expression;
105
- }
106
- try {
107
- if (declaration.callee.name === 'codegenNativeComponent') {
108
- const typeArgumentParams = declaration.typeParameters.params;
109
- const funcArgumentParams = declaration.arguments;
110
- const nativeComponentType = {
111
- propsTypeName: typeArgumentParams[0].typeName.name,
112
- componentName: funcArgumentParams[0].value,
113
- };
114
- if (funcArgumentParams.length > 1) {
115
- nativeComponentType.optionsExpression = funcArgumentParams[1];
116
- }
117
- foundConfigs.push(nativeComponentType);
118
- }
119
- } catch (e) {
120
- // ignore
121
- }
122
- });
38
+ defaultExports.forEach(statement =>
39
+ findNativeComponentType(statement, foundConfigs, parser),
40
+ );
123
41
  if (foundConfigs.length === 0) {
124
42
  throw new Error('Could not find component config for native component');
125
43
  }
@@ -162,23 +80,8 @@ function findComponentConfig(ast) {
162
80
  };
163
81
  })
164
82
  .filter(Boolean);
165
- if (commandsTypeNames.length > 1) {
166
- throw new Error('codegenNativeCommands may only be called once in a file');
167
- }
168
- return _objectSpread(
169
- _objectSpread({}, foundConfig),
170
- {},
171
- {
172
- commandTypeName:
173
- commandsTypeNames[0] == null
174
- ? null
175
- : commandsTypeNames[0].commandTypeName,
176
- commandOptionsExpression:
177
- commandsTypeNames[0] == null
178
- ? null
179
- : commandsTypeNames[0].commandOptionsExpression,
180
- },
181
- );
83
+ throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
84
+ return createComponentConfig(foundConfig, commandsTypeNames);
182
85
  }
183
86
  function getCommandProperties(
184
87
  /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
@@ -231,16 +134,14 @@ function getCommandProperties(
231
134
  // $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
232
135
 
233
136
  // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
234
- /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
235
- * LTI update could not be added via codemod */
236
- function buildComponentSchema(ast) {
237
- const _findComponentConfig = findComponentConfig(ast),
137
+ function buildComponentSchema(ast, parser) {
138
+ const _findComponentConfig = findComponentConfig(ast, parser),
238
139
  componentName = _findComponentConfig.componentName,
239
140
  propsTypeName = _findComponentConfig.propsTypeName,
240
141
  commandTypeName = _findComponentConfig.commandTypeName,
241
142
  commandOptionsExpression = _findComponentConfig.commandOptionsExpression,
242
143
  optionsExpression = _findComponentConfig.optionsExpression;
243
- const types = getTypes(ast);
144
+ const types = parser.getTypes(ast);
244
145
  const propProperties = getProperties(propsTypeName, types);
245
146
  const commandOptions = getCommandOptions(commandOptionsExpression);
246
147
  const commandProperties = getCommandProperties(
@@ -10,54 +10,35 @@
10
10
 
11
11
  'use strict';
12
12
  import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
13
+ import type {Parser} from '../../parser';
13
14
  import type {TypeDeclarationMap} from '../../utils';
14
- import type {CommandOptions} from './options';
15
- import type {ComponentSchemaBuilderConfig} from './schema.js';
15
+ import type {CommandOptions} from '../../parsers-commons';
16
+ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
16
17
 
17
- const {getTypes} = require('../utils');
18
18
  const {getCommands} = require('./commands');
19
19
  const {getEvents} = require('./events');
20
20
  const {categorizeProps} = require('./extends');
21
- const {getCommandOptions, getOptions} = require('./options');
22
21
  const {getProps} = require('./props');
23
22
  const {getProperties} = require('./componentsUtils.js');
23
+ const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
24
+ const {
25
+ createComponentConfig,
26
+ findNativeComponentType,
27
+ getCommandOptions,
28
+ getOptions,
29
+ } = require('../../parsers-commons');
24
30
 
25
- /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
26
- * LTI update could not be added via codemod */
27
- function findComponentConfig(ast) {
28
- const foundConfigs = [];
31
+ // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
32
+ function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
33
+ const foundConfigs: Array<{[string]: string}> = [];
29
34
 
30
35
  const defaultExports = ast.body.filter(
31
36
  node => node.type === 'ExportDefaultDeclaration',
32
37
  );
33
38
 
34
- defaultExports.forEach(statement => {
35
- let declaration = statement.declaration;
36
-
37
- // codegenNativeComponent can be nested inside a cast
38
- // expression so we need to go one level deeper
39
- if (declaration.type === 'TSAsExpression') {
40
- declaration = declaration.expression;
41
- }
42
-
43
- try {
44
- if (declaration.callee.name === 'codegenNativeComponent') {
45
- const typeArgumentParams = declaration.typeParameters.params;
46
- const funcArgumentParams = declaration.arguments;
47
-
48
- const nativeComponentType: {[string]: string} = {
49
- propsTypeName: typeArgumentParams[0].typeName.name,
50
- componentName: funcArgumentParams[0].value,
51
- };
52
- if (funcArgumentParams.length > 1) {
53
- nativeComponentType.optionsExpression = funcArgumentParams[1];
54
- }
55
- foundConfigs.push(nativeComponentType);
56
- }
57
- } catch (e) {
58
- // ignore
59
- }
60
- });
39
+ defaultExports.forEach(statement =>
40
+ findNativeComponentType(statement, foundConfigs, parser),
41
+ );
61
42
 
62
43
  if (foundConfigs.length === 0) {
63
44
  throw new Error('Could not find component config for native component');
@@ -109,21 +90,9 @@ function findComponentConfig(ast) {
109
90
  })
110
91
  .filter(Boolean);
111
92
 
112
- if (commandsTypeNames.length > 1) {
113
- throw new Error('codegenNativeCommands may only be called once in a file');
114
- }
93
+ throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
115
94
 
116
- return {
117
- ...foundConfig,
118
- commandTypeName:
119
- commandsTypeNames[0] == null
120
- ? null
121
- : commandsTypeNames[0].commandTypeName,
122
- commandOptionsExpression:
123
- commandsTypeNames[0] == null
124
- ? null
125
- : commandsTypeNames[0].commandOptionsExpression,
126
- };
95
+ return createComponentConfig(foundConfig, commandsTypeNames);
127
96
  }
128
97
 
129
98
  function getCommandProperties(
@@ -185,18 +154,19 @@ function getCommandProperties(
185
154
  type PropsAST = Object;
186
155
 
187
156
  // $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser
188
- /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
189
- * LTI update could not be added via codemod */
190
- function buildComponentSchema(ast): ComponentSchemaBuilderConfig {
157
+ function buildComponentSchema(
158
+ ast: $FlowFixMe,
159
+ parser: Parser,
160
+ ): ComponentSchemaBuilderConfig {
191
161
  const {
192
162
  componentName,
193
163
  propsTypeName,
194
164
  commandTypeName,
195
165
  commandOptionsExpression,
196
166
  optionsExpression,
197
- } = findComponentConfig(ast);
167
+ } = findComponentConfig(ast, parser);
198
168
 
199
- const types = getTypes(ast);
169
+ const types = parser.getTypes(ast);
200
170
 
201
171
  const propProperties = getProperties(propsTypeName, types);
202
172
  const commandOptions = getCommandOptions(commandOptionsExpression);