@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.
- package/lib/CodegenSchema.d.ts +10 -10
- package/lib/CodegenSchema.js.flow +6 -1
- package/lib/SchemaValidator.d.ts +1 -1
- package/lib/generators/RNCodegen.d.ts +98 -0
- package/lib/generators/RNCodegen.js +33 -4
- package/lib/generators/RNCodegen.js.flow +35 -4
- package/lib/generators/components/ComponentsGeneratorUtils.js +2 -0
- package/lib/generators/components/ComponentsGeneratorUtils.js.flow +2 -0
- package/lib/generators/components/CppHelpers.js +2 -0
- package/lib/generators/components/CppHelpers.js.flow +2 -0
- package/lib/generators/components/GenerateEventEmitterCpp.js +33 -44
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +35 -44
- package/lib/generators/components/GenerateEventEmitterH.js +0 -2
- package/lib/generators/components/GenerateEventEmitterH.js.flow +0 -2
- package/lib/generators/components/GeneratePropsH.js +2 -0
- package/lib/generators/components/GeneratePropsH.js.flow +2 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js +2 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +2 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js +2 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js.flow +2 -0
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -1
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +5 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +6 -0
- package/lib/generators/components/GenerateViewConfigJs.js +1 -0
- package/lib/generators/components/GenerateViewConfigJs.js.flow +1 -0
- package/lib/generators/components/JavaHelpers.js +7 -0
- package/lib/generators/components/JavaHelpers.js.flow +8 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js +29 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +30 -0
- package/lib/generators/modules/GenerateModuleCpp.js +3 -3
- package/lib/generators/modules/GenerateModuleCpp.js.flow +3 -3
- package/lib/generators/modules/GenerateModuleJavaSpec.js +1 -2
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +1 -2
- package/lib/parsers/error-utils.js +9 -15
- package/lib/parsers/error-utils.js.flow +12 -15
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +5 -1
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +5 -1
- package/lib/parsers/flow/components/componentsUtils.js +4 -0
- package/lib/parsers/flow/components/componentsUtils.js.flow +4 -0
- package/lib/parsers/flow/components/index.js +35 -168
- package/lib/parsers/flow/components/index.js.flow +33 -107
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +4 -4
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +4 -4
- package/lib/parsers/flow/modules/index.js +51 -327
- package/lib/parsers/flow/modules/index.js.flow +34 -258
- package/lib/parsers/flow/parser.js +84 -13
- package/lib/parsers/flow/parser.js.flow +80 -4
- package/lib/parsers/parser.d.ts +2 -2
- package/lib/parsers/parser.js.flow +84 -3
- package/lib/parsers/parserMock.js +53 -0
- package/lib/parsers/parserMock.js.flow +64 -1
- package/lib/parsers/parsers-commons.js +397 -4
- package/lib/parsers/parsers-commons.js.flow +391 -4
- package/lib/parsers/parsers-primitives.js +121 -3
- package/lib/parsers/parsers-primitives.js.flow +134 -3
- package/lib/parsers/schema/index.d.ts +1 -1
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +5 -1
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +5 -1
- package/lib/parsers/typescript/components/componentsUtils.js +8 -0
- package/lib/parsers/typescript/components/componentsUtils.js.flow +8 -0
- package/lib/parsers/typescript/components/index.js +37 -170
- package/lib/parsers/typescript/components/index.js.flow +33 -109
- package/lib/parsers/typescript/modules/index.js +113 -441
- package/lib/parsers/typescript/modules/index.js.flow +100 -309
- package/lib/parsers/typescript/parser.js +97 -13
- package/lib/parsers/typescript/parser.js.flow +94 -4
- package/lib/parsers/utils.js +4 -0
- package/lib/parsers/utils.js.flow +3 -0
- package/package.json +1 -1
- package/lib/parsers/flow/Visitor.js +0 -37
- package/lib/parsers/flow/Visitor.js.flow +0 -41
- package/lib/parsers/flow/components/options.js +0 -72
- package/lib/parsers/flow/components/options.js.flow +0 -87
- package/lib/parsers/typescript/Visitor.js +0 -42
- package/lib/parsers/typescript/Visitor.js.flow +0 -47
- package/lib/parsers/typescript/components/options.js +0 -72
- package/lib/parsers/typescript/components/options.js.flow +0 -87
|
@@ -21,6 +21,8 @@ import type {
|
|
|
21
21
|
NativeModuleParamTypeAnnotation,
|
|
22
22
|
NativeModulePropertyShape,
|
|
23
23
|
SchemaType,
|
|
24
|
+
NativeModuleEnumMap,
|
|
25
|
+
OptionsShape,
|
|
24
26
|
} from '../CodegenSchema.js';
|
|
25
27
|
|
|
26
28
|
import type {Parser} from './parser';
|
|
@@ -32,13 +34,24 @@ const {
|
|
|
32
34
|
getConfigType,
|
|
33
35
|
extractNativeModuleName,
|
|
34
36
|
createParserErrorCapturer,
|
|
37
|
+
visit,
|
|
38
|
+
isModuleRegistryCall,
|
|
39
|
+
verifyPlatforms,
|
|
35
40
|
} = require('./utils');
|
|
36
|
-
|
|
37
41
|
const {
|
|
38
42
|
throwIfPropertyValueTypeIsUnsupported,
|
|
39
43
|
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
|
|
40
44
|
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
|
|
41
45
|
throwIfModuleTypeIsUnsupported,
|
|
46
|
+
throwIfUnusedModuleInterfaceParserError,
|
|
47
|
+
throwIfMoreThanOneModuleRegistryCalls,
|
|
48
|
+
throwIfWrongNumberOfCallExpressionArgs,
|
|
49
|
+
throwIfUntypedModule,
|
|
50
|
+
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
|
|
51
|
+
throwIfIncorrectModuleRegistryCallArgument,
|
|
52
|
+
throwIfModuleInterfaceNotFound,
|
|
53
|
+
throwIfMoreThanOneModuleInterfaceParserError,
|
|
54
|
+
throwIfModuleInterfaceIsMisnamed,
|
|
42
55
|
} = require('./error-utils');
|
|
43
56
|
|
|
44
57
|
const {
|
|
@@ -48,7 +61,13 @@ const {
|
|
|
48
61
|
} = require('./errors');
|
|
49
62
|
|
|
50
63
|
const invariant = require('invariant');
|
|
51
|
-
|
|
64
|
+
|
|
65
|
+
export type CommandOptions = $ReadOnly<{
|
|
66
|
+
supportedCommands: $ReadOnlyArray<string>,
|
|
67
|
+
}>;
|
|
68
|
+
|
|
69
|
+
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
70
|
+
type OptionsAST = Object;
|
|
52
71
|
|
|
53
72
|
function wrapModuleSchema(
|
|
54
73
|
nativeModuleSchema: NativeModuleSchema,
|
|
@@ -317,7 +336,7 @@ function buildPropertySchema(
|
|
|
317
336
|
property.value,
|
|
318
337
|
key.name,
|
|
319
338
|
value.type,
|
|
320
|
-
parser
|
|
339
|
+
parser,
|
|
321
340
|
);
|
|
322
341
|
|
|
323
342
|
return {
|
|
@@ -354,8 +373,12 @@ function buildSchemaFromConfigType(
|
|
|
354
373
|
ast: $FlowFixMe,
|
|
355
374
|
tryParse: ParserErrorCapturer,
|
|
356
375
|
parser: Parser,
|
|
376
|
+
resolveTypeAnnotation: $FlowFixMe,
|
|
377
|
+
translateTypeAnnotation: $FlowFixMe,
|
|
357
378
|
) => NativeModuleSchema,
|
|
358
379
|
parser: Parser,
|
|
380
|
+
resolveTypeAnnotation: $FlowFixMe,
|
|
381
|
+
translateTypeAnnotation: $FlowFixMe,
|
|
359
382
|
): SchemaType {
|
|
360
383
|
switch (configType) {
|
|
361
384
|
case 'component': {
|
|
@@ -370,7 +393,14 @@ function buildSchemaFromConfigType(
|
|
|
370
393
|
const [parsingErrors, tryParse] = createParserErrorCapturer();
|
|
371
394
|
|
|
372
395
|
const schema = tryParse(() =>
|
|
373
|
-
buildModuleSchema(
|
|
396
|
+
buildModuleSchema(
|
|
397
|
+
nativeModuleName,
|
|
398
|
+
ast,
|
|
399
|
+
tryParse,
|
|
400
|
+
parser,
|
|
401
|
+
resolveTypeAnnotation,
|
|
402
|
+
translateTypeAnnotation,
|
|
403
|
+
),
|
|
374
404
|
);
|
|
375
405
|
|
|
376
406
|
if (parsingErrors.length > 0) {
|
|
@@ -410,11 +440,15 @@ function buildSchema(
|
|
|
410
440
|
ast: $FlowFixMe,
|
|
411
441
|
tryParse: ParserErrorCapturer,
|
|
412
442
|
parser: Parser,
|
|
443
|
+
resolveTypeAnnotation: $FlowFixMe,
|
|
444
|
+
translateTypeAnnotation: $FlowFixMe,
|
|
413
445
|
) => NativeModuleSchema,
|
|
414
446
|
Visitor: ({isComponent: boolean, isModule: boolean}) => {
|
|
415
447
|
[type: string]: (node: $FlowFixMe) => void,
|
|
416
448
|
},
|
|
417
449
|
parser: Parser,
|
|
450
|
+
resolveTypeAnnotation: $FlowFixMe,
|
|
451
|
+
translateTypeAnnotation: $FlowFixMe,
|
|
418
452
|
): SchemaType {
|
|
419
453
|
// Early return for non-Spec JavaScript files
|
|
420
454
|
if (
|
|
@@ -435,7 +469,352 @@ function buildSchema(
|
|
|
435
469
|
buildComponentSchema,
|
|
436
470
|
buildModuleSchema,
|
|
437
471
|
parser,
|
|
472
|
+
resolveTypeAnnotation,
|
|
473
|
+
translateTypeAnnotation,
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function createComponentConfig(
|
|
478
|
+
foundConfig: $FlowFixMe,
|
|
479
|
+
commandsTypeNames: $FlowFixMe,
|
|
480
|
+
): $FlowFixMe {
|
|
481
|
+
return {
|
|
482
|
+
...foundConfig,
|
|
483
|
+
commandTypeName:
|
|
484
|
+
commandsTypeNames[0] == null
|
|
485
|
+
? null
|
|
486
|
+
: commandsTypeNames[0].commandTypeName,
|
|
487
|
+
commandOptionsExpression:
|
|
488
|
+
commandsTypeNames[0] == null
|
|
489
|
+
? null
|
|
490
|
+
: commandsTypeNames[0].commandOptionsExpression,
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const parseModuleName = (
|
|
495
|
+
hasteModuleName: string,
|
|
496
|
+
moduleSpec: $FlowFixMe,
|
|
497
|
+
ast: $FlowFixMe,
|
|
498
|
+
parser: Parser,
|
|
499
|
+
): string => {
|
|
500
|
+
const callExpressions = [];
|
|
501
|
+
visit(ast, {
|
|
502
|
+
CallExpression(node) {
|
|
503
|
+
if (isModuleRegistryCall(node)) {
|
|
504
|
+
callExpressions.push(node);
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
throwIfUnusedModuleInterfaceParserError(
|
|
510
|
+
hasteModuleName,
|
|
511
|
+
moduleSpec,
|
|
512
|
+
callExpressions,
|
|
513
|
+
);
|
|
514
|
+
|
|
515
|
+
throwIfMoreThanOneModuleRegistryCalls(
|
|
516
|
+
hasteModuleName,
|
|
517
|
+
callExpressions,
|
|
518
|
+
callExpressions.length,
|
|
519
|
+
);
|
|
520
|
+
|
|
521
|
+
const [callExpression] = callExpressions;
|
|
522
|
+
const typeParameters = parser.callExpressionTypeParameters(callExpression);
|
|
523
|
+
const methodName = callExpression.callee.property.name;
|
|
524
|
+
|
|
525
|
+
throwIfWrongNumberOfCallExpressionArgs(
|
|
526
|
+
hasteModuleName,
|
|
527
|
+
callExpression,
|
|
528
|
+
methodName,
|
|
529
|
+
callExpression.arguments.length,
|
|
438
530
|
);
|
|
531
|
+
|
|
532
|
+
throwIfIncorrectModuleRegistryCallArgument(
|
|
533
|
+
hasteModuleName,
|
|
534
|
+
callExpression.arguments[0],
|
|
535
|
+
methodName,
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
const $moduleName = callExpression.arguments[0].value;
|
|
539
|
+
|
|
540
|
+
throwIfUntypedModule(
|
|
541
|
+
typeParameters,
|
|
542
|
+
hasteModuleName,
|
|
543
|
+
callExpression,
|
|
544
|
+
methodName,
|
|
545
|
+
$moduleName,
|
|
546
|
+
);
|
|
547
|
+
|
|
548
|
+
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
|
|
549
|
+
hasteModuleName,
|
|
550
|
+
typeParameters,
|
|
551
|
+
methodName,
|
|
552
|
+
$moduleName,
|
|
553
|
+
parser,
|
|
554
|
+
);
|
|
555
|
+
|
|
556
|
+
return $moduleName;
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
const buildModuleSchema = (
|
|
560
|
+
hasteModuleName: string,
|
|
561
|
+
/**
|
|
562
|
+
* TODO(T71778680): Flow-type this node.
|
|
563
|
+
*/
|
|
564
|
+
ast: $FlowFixMe,
|
|
565
|
+
tryParse: ParserErrorCapturer,
|
|
566
|
+
parser: Parser,
|
|
567
|
+
resolveTypeAnnotation: $FlowFixMe,
|
|
568
|
+
translateTypeAnnotation: $FlowFixMe,
|
|
569
|
+
): NativeModuleSchema => {
|
|
570
|
+
const language = parser.language();
|
|
571
|
+
const types = parser.getTypes(ast);
|
|
572
|
+
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
|
|
573
|
+
t => parser.isModuleInterface(t),
|
|
574
|
+
);
|
|
575
|
+
|
|
576
|
+
throwIfModuleInterfaceNotFound(
|
|
577
|
+
moduleSpecs.length,
|
|
578
|
+
hasteModuleName,
|
|
579
|
+
ast,
|
|
580
|
+
language,
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
throwIfMoreThanOneModuleInterfaceParserError(
|
|
584
|
+
hasteModuleName,
|
|
585
|
+
moduleSpecs,
|
|
586
|
+
language,
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
const [moduleSpec] = moduleSpecs;
|
|
590
|
+
|
|
591
|
+
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
|
|
592
|
+
|
|
593
|
+
// Parse Module Name
|
|
594
|
+
const moduleName = parseModuleName(hasteModuleName, moduleSpec, ast, parser);
|
|
595
|
+
|
|
596
|
+
// Some module names use platform suffix to indicate platform-exclusive modules.
|
|
597
|
+
// Eventually this should be made explicit in the Flow type itself.
|
|
598
|
+
// Also check the hasteModuleName for platform suffix.
|
|
599
|
+
// Note: this shape is consistent with ComponentSchema.
|
|
600
|
+
const {cxxOnly, excludedPlatforms} = verifyPlatforms(
|
|
601
|
+
hasteModuleName,
|
|
602
|
+
moduleName,
|
|
603
|
+
);
|
|
604
|
+
|
|
605
|
+
const properties: $ReadOnlyArray<$FlowFixMe> =
|
|
606
|
+
language === 'Flow' ? moduleSpec.body.properties : moduleSpec.body.body;
|
|
607
|
+
|
|
608
|
+
// $FlowFixMe[missing-type-arg]
|
|
609
|
+
return properties
|
|
610
|
+
.filter(
|
|
611
|
+
property =>
|
|
612
|
+
property.type === 'ObjectTypeProperty' ||
|
|
613
|
+
property.type === 'TSPropertySignature' ||
|
|
614
|
+
property.type === 'TSMethodSignature',
|
|
615
|
+
)
|
|
616
|
+
.map<?{
|
|
617
|
+
aliasMap: NativeModuleAliasMap,
|
|
618
|
+
enumMap: NativeModuleEnumMap,
|
|
619
|
+
propertyShape: NativeModulePropertyShape,
|
|
620
|
+
}>(property => {
|
|
621
|
+
const aliasMap: {...NativeModuleAliasMap} = {};
|
|
622
|
+
const enumMap: {...NativeModuleEnumMap} = {};
|
|
623
|
+
|
|
624
|
+
return tryParse(() => ({
|
|
625
|
+
aliasMap,
|
|
626
|
+
enumMap,
|
|
627
|
+
propertyShape: buildPropertySchema(
|
|
628
|
+
hasteModuleName,
|
|
629
|
+
property,
|
|
630
|
+
types,
|
|
631
|
+
aliasMap,
|
|
632
|
+
enumMap,
|
|
633
|
+
tryParse,
|
|
634
|
+
cxxOnly,
|
|
635
|
+
resolveTypeAnnotation,
|
|
636
|
+
translateTypeAnnotation,
|
|
637
|
+
parser,
|
|
638
|
+
),
|
|
639
|
+
}));
|
|
640
|
+
})
|
|
641
|
+
.filter(Boolean)
|
|
642
|
+
.reduce(
|
|
643
|
+
(
|
|
644
|
+
moduleSchema: NativeModuleSchema,
|
|
645
|
+
{aliasMap, enumMap, propertyShape},
|
|
646
|
+
) => ({
|
|
647
|
+
type: 'NativeModule',
|
|
648
|
+
aliasMap: {...moduleSchema.aliasMap, ...aliasMap},
|
|
649
|
+
enumMap: {...moduleSchema.enumMap, ...enumMap},
|
|
650
|
+
spec: {
|
|
651
|
+
properties: [...moduleSchema.spec.properties, propertyShape],
|
|
652
|
+
},
|
|
653
|
+
moduleName: moduleSchema.moduleName,
|
|
654
|
+
excludedPlatforms: moduleSchema.excludedPlatforms,
|
|
655
|
+
}),
|
|
656
|
+
{
|
|
657
|
+
type: 'NativeModule',
|
|
658
|
+
aliasMap: {},
|
|
659
|
+
enumMap: {},
|
|
660
|
+
spec: {properties: []},
|
|
661
|
+
moduleName,
|
|
662
|
+
excludedPlatforms:
|
|
663
|
+
excludedPlatforms.length !== 0 ? [...excludedPlatforms] : undefined,
|
|
664
|
+
},
|
|
665
|
+
);
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* This function is used to find the type of a native component
|
|
670
|
+
* provided the default exports statement from generated AST.
|
|
671
|
+
* @param statement The statement to be parsed.
|
|
672
|
+
* @param foundConfigs The 'mutable' array of configs that have been found.
|
|
673
|
+
* @param parser The language parser to be used.
|
|
674
|
+
* @returns void
|
|
675
|
+
*/
|
|
676
|
+
function findNativeComponentType(
|
|
677
|
+
statement: $FlowFixMe,
|
|
678
|
+
foundConfigs: Array<{[string]: string}>,
|
|
679
|
+
parser: Parser,
|
|
680
|
+
): void {
|
|
681
|
+
let declaration = statement.declaration;
|
|
682
|
+
|
|
683
|
+
// codegenNativeComponent can be nested inside a cast
|
|
684
|
+
// expression so we need to go one level deeper
|
|
685
|
+
if (
|
|
686
|
+
declaration.type === 'TSAsExpression' ||
|
|
687
|
+
declaration.type === 'TypeCastExpression'
|
|
688
|
+
) {
|
|
689
|
+
declaration = declaration.expression;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
try {
|
|
693
|
+
if (declaration.callee.name === 'codegenNativeComponent') {
|
|
694
|
+
const typeArgumentParams =
|
|
695
|
+
parser.getTypeArgumentParamsFromDeclaration(declaration);
|
|
696
|
+
const funcArgumentParams = declaration.arguments;
|
|
697
|
+
|
|
698
|
+
const nativeComponentType: {[string]: string} =
|
|
699
|
+
parser.getNativeComponentType(typeArgumentParams, funcArgumentParams);
|
|
700
|
+
if (funcArgumentParams.length > 1) {
|
|
701
|
+
nativeComponentType.optionsExpression = funcArgumentParams[1];
|
|
702
|
+
}
|
|
703
|
+
foundConfigs.push(nativeComponentType);
|
|
704
|
+
}
|
|
705
|
+
} catch (e) {
|
|
706
|
+
// ignore
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function getCommandOptions(
|
|
711
|
+
commandOptionsExpression: OptionsAST,
|
|
712
|
+
): ?CommandOptions {
|
|
713
|
+
if (commandOptionsExpression == null) {
|
|
714
|
+
return null;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
let foundOptions;
|
|
718
|
+
try {
|
|
719
|
+
foundOptions = commandOptionsExpression.properties.reduce(
|
|
720
|
+
(options, prop) => {
|
|
721
|
+
options[prop.key.name] = (
|
|
722
|
+
(prop && prop.value && prop.value.elements) ||
|
|
723
|
+
[]
|
|
724
|
+
).map(element => element && element.value);
|
|
725
|
+
return options;
|
|
726
|
+
},
|
|
727
|
+
{},
|
|
728
|
+
);
|
|
729
|
+
} catch (e) {
|
|
730
|
+
throw new Error(
|
|
731
|
+
'Failed to parse command options, please check that they are defined correctly',
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
return foundOptions;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
|
|
739
|
+
if (!optionsExpression) {
|
|
740
|
+
return null;
|
|
741
|
+
}
|
|
742
|
+
let foundOptions;
|
|
743
|
+
try {
|
|
744
|
+
foundOptions = optionsExpression.properties.reduce((options, prop) => {
|
|
745
|
+
if (prop.value.type === 'ArrayExpression') {
|
|
746
|
+
options[prop.key.name] = prop.value.elements.map(
|
|
747
|
+
element => element.value,
|
|
748
|
+
);
|
|
749
|
+
} else {
|
|
750
|
+
options[prop.key.name] = prop.value.value;
|
|
751
|
+
}
|
|
752
|
+
return options;
|
|
753
|
+
}, {});
|
|
754
|
+
} catch (e) {
|
|
755
|
+
throw new Error(
|
|
756
|
+
'Failed to parse codegen options, please check that they are defined correctly',
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
if (
|
|
761
|
+
foundOptions.paperComponentName &&
|
|
762
|
+
foundOptions.paperComponentNameDeprecated
|
|
763
|
+
) {
|
|
764
|
+
throw new Error(
|
|
765
|
+
'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
return foundOptions;
|
|
769
|
+
}
|
|
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);
|
|
439
818
|
}
|
|
440
819
|
|
|
441
820
|
module.exports = {
|
|
@@ -449,4 +828,12 @@ module.exports = {
|
|
|
449
828
|
buildPropertySchema,
|
|
450
829
|
buildSchemaFromConfigType,
|
|
451
830
|
buildSchema,
|
|
831
|
+
createComponentConfig,
|
|
832
|
+
parseModuleName,
|
|
833
|
+
buildModuleSchema,
|
|
834
|
+
findNativeComponentType,
|
|
835
|
+
propertyNames,
|
|
836
|
+
getCommandOptions,
|
|
837
|
+
getOptions,
|
|
838
|
+
getCommandTypeNameAndOptionsExpression,
|
|
452
839
|
};
|
|
@@ -86,7 +86,10 @@ const _require = require('./errors'),
|
|
|
86
86
|
ParserError = _require.ParserError;
|
|
87
87
|
const _require2 = require('./error-utils'),
|
|
88
88
|
throwIfArrayElementTypeAnnotationIsUnsupported =
|
|
89
|
-
_require2.throwIfArrayElementTypeAnnotationIsUnsupported
|
|
89
|
+
_require2.throwIfArrayElementTypeAnnotationIsUnsupported,
|
|
90
|
+
throwIfPartialNotAnnotatingTypeParameter =
|
|
91
|
+
_require2.throwIfPartialNotAnnotatingTypeParameter,
|
|
92
|
+
throwIfPartialWithMoreParameter = _require2.throwIfPartialWithMoreParameter;
|
|
90
93
|
const _require3 = require('./parsers-utils'),
|
|
91
94
|
nullGuard = _require3.nullGuard;
|
|
92
95
|
const _require4 = require('./parsers-commons'),
|
|
@@ -95,6 +98,8 @@ const _require4 = require('./parsers-commons'),
|
|
|
95
98
|
wrapNullable = _require4.wrapNullable,
|
|
96
99
|
unwrapNullable = _require4.unwrapNullable,
|
|
97
100
|
translateFunctionTypeAnnotation = _require4.translateFunctionTypeAnnotation;
|
|
101
|
+
const _require5 = require('./utils'),
|
|
102
|
+
isModuleRegistryCall = _require5.isModuleRegistryCall;
|
|
98
103
|
function emitBoolean(nullable) {
|
|
99
104
|
return wrapNullable(nullable, {
|
|
100
105
|
type: 'BooleanTypeAnnotation',
|
|
@@ -220,7 +225,6 @@ function typeEnumResolution(
|
|
|
220
225
|
typeResolution,
|
|
221
226
|
nullable,
|
|
222
227
|
hasteModuleName,
|
|
223
|
-
language,
|
|
224
228
|
enumMap,
|
|
225
229
|
parser,
|
|
226
230
|
) {
|
|
@@ -228,7 +232,7 @@ function typeEnumResolution(
|
|
|
228
232
|
throw new UnsupportedTypeAnnotationParserError(
|
|
229
233
|
hasteModuleName,
|
|
230
234
|
typeAnnotation,
|
|
231
|
-
language,
|
|
235
|
+
parser.language(),
|
|
232
236
|
);
|
|
233
237
|
}
|
|
234
238
|
const enumName = typeResolution.name;
|
|
@@ -427,6 +431,117 @@ function emitArrayType(
|
|
|
427
431
|
parser,
|
|
428
432
|
);
|
|
429
433
|
}
|
|
434
|
+
function Visitor(infoMap) {
|
|
435
|
+
return {
|
|
436
|
+
CallExpression(node) {
|
|
437
|
+
if (
|
|
438
|
+
node.callee.type === 'Identifier' &&
|
|
439
|
+
node.callee.name === 'codegenNativeComponent'
|
|
440
|
+
) {
|
|
441
|
+
infoMap.isComponent = true;
|
|
442
|
+
}
|
|
443
|
+
if (isModuleRegistryCall(node)) {
|
|
444
|
+
infoMap.isModule = true;
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
InterfaceExtends(node) {
|
|
448
|
+
if (node.id.name === 'TurboModule') {
|
|
449
|
+
infoMap.isModule = true;
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
TSInterfaceDeclaration(node) {
|
|
453
|
+
if (
|
|
454
|
+
Array.isArray(node.extends) &&
|
|
455
|
+
node.extends.some(
|
|
456
|
+
extension => extension.expression.name === 'TurboModule',
|
|
457
|
+
)
|
|
458
|
+
) {
|
|
459
|
+
infoMap.isModule = true;
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function emitPartial(
|
|
465
|
+
nullable,
|
|
466
|
+
hasteModuleName,
|
|
467
|
+
typeAnnotation,
|
|
468
|
+
types,
|
|
469
|
+
aliasMap,
|
|
470
|
+
enumMap,
|
|
471
|
+
tryParse,
|
|
472
|
+
cxxOnly,
|
|
473
|
+
parser,
|
|
474
|
+
) {
|
|
475
|
+
throwIfPartialWithMoreParameter(typeAnnotation);
|
|
476
|
+
throwIfPartialNotAnnotatingTypeParameter(typeAnnotation, types, parser);
|
|
477
|
+
const annotatedElement = parser.extractAnnotatedElement(
|
|
478
|
+
typeAnnotation,
|
|
479
|
+
types,
|
|
480
|
+
);
|
|
481
|
+
const annotatedElementProperties =
|
|
482
|
+
parser.getAnnotatedElementProperties(annotatedElement);
|
|
483
|
+
const partialProperties = parser.computePartialProperties(
|
|
484
|
+
annotatedElementProperties,
|
|
485
|
+
hasteModuleName,
|
|
486
|
+
types,
|
|
487
|
+
aliasMap,
|
|
488
|
+
enumMap,
|
|
489
|
+
tryParse,
|
|
490
|
+
cxxOnly,
|
|
491
|
+
);
|
|
492
|
+
return emitObject(nullable, partialProperties);
|
|
493
|
+
}
|
|
494
|
+
function emitCommonTypes(
|
|
495
|
+
hasteModuleName,
|
|
496
|
+
types,
|
|
497
|
+
typeAnnotation,
|
|
498
|
+
aliasMap,
|
|
499
|
+
enumMap,
|
|
500
|
+
tryParse,
|
|
501
|
+
cxxOnly,
|
|
502
|
+
nullable,
|
|
503
|
+
parser,
|
|
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
|
+
}
|
|
527
|
+
const genericTypeAnnotationName =
|
|
528
|
+
parser.nameForGenericTypeAnnotation(typeAnnotation);
|
|
529
|
+
const emitter = typeMap[genericTypeAnnotationName];
|
|
530
|
+
if (!emitter) {
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
return emitter(
|
|
534
|
+
nullable,
|
|
535
|
+
hasteModuleName,
|
|
536
|
+
typeAnnotation,
|
|
537
|
+
types,
|
|
538
|
+
aliasMap,
|
|
539
|
+
enumMap,
|
|
540
|
+
tryParse,
|
|
541
|
+
cxxOnly,
|
|
542
|
+
parser,
|
|
543
|
+
);
|
|
544
|
+
}
|
|
430
545
|
module.exports = {
|
|
431
546
|
emitArrayType,
|
|
432
547
|
emitBoolean,
|
|
@@ -444,7 +559,10 @@ module.exports = {
|
|
|
444
559
|
emitStringish,
|
|
445
560
|
emitMixed,
|
|
446
561
|
emitUnion,
|
|
562
|
+
emitPartial,
|
|
563
|
+
emitCommonTypes,
|
|
447
564
|
typeAliasResolution,
|
|
448
565
|
typeEnumResolution,
|
|
449
566
|
translateArrayTypeAnnotation,
|
|
567
|
+
Visitor,
|
|
450
568
|
};
|