@react-native/codegen 0.72.5 → 0.73.0-nightly-20230603-fd9e295be
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/README.md +2 -2
- package/lib/CodegenSchema.d.ts +15 -4
- package/lib/CodegenSchema.js.flow +33 -21
- package/lib/generators/components/CppHelpers.js +39 -2
- package/lib/generators/components/CppHelpers.js.flow +54 -5
- package/lib/generators/components/GenerateEventEmitterCpp.js +238 -55
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +233 -55
- package/lib/generators/components/GenerateEventEmitterH.js +77 -28
- package/lib/generators/components/GenerateEventEmitterH.js.flow +87 -32
- package/lib/generators/components/GeneratePropsH.js +1 -2
- package/lib/generators/components/GeneratePropsH.js.flow +1 -2
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +0 -1
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +3 -5
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +4 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +4 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +2 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +2 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js +114 -1
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +114 -1
- 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 +44 -0
- package/lib/parsers/error-utils.js.flow +59 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +51 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +51 -0
- package/lib/parsers/flow/components/componentsUtils.js +6 -6
- package/lib/parsers/flow/components/componentsUtils.js.flow +10 -10
- package/lib/parsers/flow/components/events.js +157 -78
- package/lib/parsers/flow/components/events.js.flow +158 -77
- package/lib/parsers/flow/components/index.js +28 -105
- package/lib/parsers/flow/components/index.js.flow +21 -110
- package/lib/parsers/flow/modules/index.js +39 -49
- package/lib/parsers/flow/modules/index.js.flow +22 -31
- package/lib/parsers/flow/parser.d.ts +9 -2
- package/lib/parsers/flow/parser.js +167 -16
- package/lib/parsers/flow/parser.js.flow +209 -6
- package/lib/parsers/flow/utils.js +0 -63
- package/lib/parsers/flow/utils.js.flow +1 -77
- package/lib/parsers/parser.js.flow +141 -1
- package/lib/parsers/parserMock.js +165 -2
- package/lib/parsers/parserMock.js.flow +216 -2
- package/lib/parsers/parsers-commons.js +128 -9
- package/lib/parsers/parsers-commons.js.flow +182 -12
- package/lib/parsers/parsers-primitives.js +84 -35
- package/lib/parsers/parsers-primitives.js.flow +110 -35
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +51 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +51 -0
- package/lib/parsers/typescript/components/componentsUtils.js +31 -7
- package/lib/parsers/typescript/components/componentsUtils.js.flow +31 -14
- package/lib/parsers/typescript/components/events.js +142 -67
- package/lib/parsers/typescript/components/events.js.flow +149 -68
- package/lib/parsers/typescript/components/extends.js +2 -49
- package/lib/parsers/typescript/components/extends.js.flow +1 -55
- package/lib/parsers/typescript/components/index.js +29 -109
- package/lib/parsers/typescript/components/index.js.flow +21 -116
- package/lib/parsers/typescript/modules/index.js +41 -50
- package/lib/parsers/typescript/modules/index.js.flow +23 -31
- package/lib/parsers/typescript/parser.d.ts +9 -2
- package/lib/parsers/typescript/parser.js +193 -7
- package/lib/parsers/typescript/parser.js.flow +235 -6
- package/lib/parsers/utils.js.flow +6 -0
- package/package.json +16 -5
- package/lib/parsers/flow/components/extends.js +0 -52
- package/lib/parsers/flow/components/extends.js.flow +0 -66
- package/lib/parsers/flow/components/props.js +0 -47
- package/lib/parsers/flow/components/props.js.flow +0 -60
- package/lib/parsers/typescript/components/props.js +0 -39
- package/lib/parsers/typescript/components/props.js.flow +0 -48
- package/lib/parsers/typescript/utils.js +0 -90
- package/lib/parsers/typescript/utils.js.flow +0 -104
|
@@ -169,7 +169,11 @@ const _require2 = require('./error-utils'),
|
|
|
169
169
|
throwIfModuleInterfaceNotFound = _require2.throwIfModuleInterfaceNotFound,
|
|
170
170
|
throwIfMoreThanOneModuleInterfaceParserError =
|
|
171
171
|
_require2.throwIfMoreThanOneModuleInterfaceParserError,
|
|
172
|
-
throwIfModuleInterfaceIsMisnamed = _require2.throwIfModuleInterfaceIsMisnamed
|
|
172
|
+
throwIfModuleInterfaceIsMisnamed = _require2.throwIfModuleInterfaceIsMisnamed,
|
|
173
|
+
throwIfMoreThanOneCodegenNativecommands =
|
|
174
|
+
_require2.throwIfMoreThanOneCodegenNativecommands,
|
|
175
|
+
throwIfConfigNotfound = _require2.throwIfConfigNotfound,
|
|
176
|
+
throwIfMoreThanOneConfig = _require2.throwIfMoreThanOneConfig;
|
|
173
177
|
const _require3 = require('./errors'),
|
|
174
178
|
MissingTypeParameterGenericParserError =
|
|
175
179
|
_require3.MissingTypeParameterGenericParserError,
|
|
@@ -395,7 +399,6 @@ function buildPropertySchema(
|
|
|
395
399
|
enumMap,
|
|
396
400
|
tryParse,
|
|
397
401
|
cxxOnly,
|
|
398
|
-
resolveTypeAnnotation,
|
|
399
402
|
translateTypeAnnotation,
|
|
400
403
|
parser,
|
|
401
404
|
) {
|
|
@@ -409,7 +412,8 @@ function buildPropertySchema(
|
|
|
409
412
|
? property
|
|
410
413
|
: property.typeAnnotation;
|
|
411
414
|
}
|
|
412
|
-
|
|
415
|
+
const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN();
|
|
416
|
+
var _resolveTypeAnnotatio = resolveTypeAnnotationFN(value, types, parser);
|
|
413
417
|
nullable = _resolveTypeAnnotatio.nullable;
|
|
414
418
|
value = _resolveTypeAnnotatio.typeAnnotation;
|
|
415
419
|
throwIfModuleTypeIsUnsupported(
|
|
@@ -446,7 +450,6 @@ function buildSchemaFromConfigType(
|
|
|
446
450
|
buildComponentSchema,
|
|
447
451
|
buildModuleSchema,
|
|
448
452
|
parser,
|
|
449
|
-
resolveTypeAnnotation,
|
|
450
453
|
translateTypeAnnotation,
|
|
451
454
|
) {
|
|
452
455
|
switch (configType) {
|
|
@@ -468,7 +471,6 @@ function buildSchemaFromConfigType(
|
|
|
468
471
|
ast,
|
|
469
472
|
tryParse,
|
|
470
473
|
parser,
|
|
471
|
-
resolveTypeAnnotation,
|
|
472
474
|
translateTypeAnnotation,
|
|
473
475
|
),
|
|
474
476
|
);
|
|
@@ -503,7 +505,6 @@ function buildSchema(
|
|
|
503
505
|
buildModuleSchema,
|
|
504
506
|
Visitor,
|
|
505
507
|
parser,
|
|
506
|
-
resolveTypeAnnotation,
|
|
507
508
|
translateTypeAnnotation,
|
|
508
509
|
) {
|
|
509
510
|
// Early return for non-Spec JavaScript files
|
|
@@ -525,7 +526,6 @@ function buildSchema(
|
|
|
525
526
|
buildComponentSchema,
|
|
526
527
|
buildModuleSchema,
|
|
527
528
|
parser,
|
|
528
|
-
resolveTypeAnnotation,
|
|
529
529
|
translateTypeAnnotation,
|
|
530
530
|
);
|
|
531
531
|
}
|
|
@@ -600,7 +600,6 @@ const buildModuleSchema = (
|
|
|
600
600
|
ast,
|
|
601
601
|
tryParse,
|
|
602
602
|
parser,
|
|
603
|
-
resolveTypeAnnotation,
|
|
604
603
|
translateTypeAnnotation,
|
|
605
604
|
) => {
|
|
606
605
|
const language = parser.language();
|
|
@@ -658,7 +657,6 @@ const buildModuleSchema = (
|
|
|
658
657
|
enumMap,
|
|
659
658
|
tryParse,
|
|
660
659
|
cxxOnly,
|
|
661
|
-
resolveTypeAnnotation,
|
|
662
660
|
translateTypeAnnotation,
|
|
663
661
|
parser,
|
|
664
662
|
),
|
|
@@ -787,6 +785,121 @@ function getOptions(optionsExpression) {
|
|
|
787
785
|
}
|
|
788
786
|
return foundOptions;
|
|
789
787
|
}
|
|
788
|
+
function getCommandTypeNameAndOptionsExpression(namedExport, parser) {
|
|
789
|
+
let callExpression;
|
|
790
|
+
let calleeName;
|
|
791
|
+
try {
|
|
792
|
+
callExpression = namedExport.declaration.declarations[0].init;
|
|
793
|
+
calleeName = callExpression.callee.name;
|
|
794
|
+
} catch (e) {
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
if (calleeName !== 'codegenNativeCommands') {
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
if (callExpression.arguments.length !== 1) {
|
|
801
|
+
throw new Error(
|
|
802
|
+
'codegenNativeCommands must be passed options including the supported commands',
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
const typeArgumentParam =
|
|
806
|
+
parser.getTypeArgumentParamsFromDeclaration(callExpression)[0];
|
|
807
|
+
if (!parser.isGenericTypeAnnotation(typeArgumentParam.type)) {
|
|
808
|
+
throw new Error(
|
|
809
|
+
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
return {
|
|
813
|
+
commandTypeName: parser.nameForGenericTypeAnnotation(typeArgumentParam),
|
|
814
|
+
commandOptionsExpression: callExpression.arguments[0],
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
function propertyNames(properties) {
|
|
818
|
+
return properties
|
|
819
|
+
.map(property => property && property.key && property.key.name)
|
|
820
|
+
.filter(Boolean);
|
|
821
|
+
}
|
|
822
|
+
function extendsForProp(prop, types, parser) {
|
|
823
|
+
const argument = parser.argumentForProp(prop);
|
|
824
|
+
if (!argument) {
|
|
825
|
+
console.log('null', prop);
|
|
826
|
+
}
|
|
827
|
+
const name = parser.nameForArgument(prop);
|
|
828
|
+
if (types[name] != null) {
|
|
829
|
+
// This type is locally defined in the file
|
|
830
|
+
return null;
|
|
831
|
+
}
|
|
832
|
+
switch (name) {
|
|
833
|
+
case 'ViewProps':
|
|
834
|
+
return {
|
|
835
|
+
type: 'ReactNativeBuiltInType',
|
|
836
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
837
|
+
};
|
|
838
|
+
default: {
|
|
839
|
+
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
function buildPropSchema(property, types, parser) {
|
|
844
|
+
const getSchemaInfoFN = parser.getGetSchemaInfoFN();
|
|
845
|
+
const info = getSchemaInfoFN(property, types);
|
|
846
|
+
if (info == null) {
|
|
847
|
+
return null;
|
|
848
|
+
}
|
|
849
|
+
const name = info.name,
|
|
850
|
+
optional = info.optional,
|
|
851
|
+
typeAnnotation = info.typeAnnotation,
|
|
852
|
+
defaultValue = info.defaultValue,
|
|
853
|
+
withNullDefault = info.withNullDefault;
|
|
854
|
+
const getTypeAnnotationFN = parser.getGetTypeAnnotationFN();
|
|
855
|
+
return {
|
|
856
|
+
name,
|
|
857
|
+
optional,
|
|
858
|
+
typeAnnotation: getTypeAnnotationFN(
|
|
859
|
+
name,
|
|
860
|
+
typeAnnotation,
|
|
861
|
+
defaultValue,
|
|
862
|
+
withNullDefault,
|
|
863
|
+
types,
|
|
864
|
+
parser,
|
|
865
|
+
buildPropSchema,
|
|
866
|
+
),
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
871
|
+
* LTI update could not be added via codemod */
|
|
872
|
+
function getEventArgument(argumentProps, buildPropertiesForEvent, parser) {
|
|
873
|
+
return {
|
|
874
|
+
type: 'ObjectTypeAnnotation',
|
|
875
|
+
properties: argumentProps.map(member =>
|
|
876
|
+
buildPropertiesForEvent(member, parser),
|
|
877
|
+
),
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/* $FlowFixMe[signature-verification-failure] there's no flowtype for AST.
|
|
882
|
+
* TODO(T108222691): Use flow-types for @babel/parser */
|
|
883
|
+
function findComponentConfig(ast, parser) {
|
|
884
|
+
const foundConfigs = [];
|
|
885
|
+
const defaultExports = ast.body.filter(
|
|
886
|
+
node => node.type === 'ExportDefaultDeclaration',
|
|
887
|
+
);
|
|
888
|
+
defaultExports.forEach(statement => {
|
|
889
|
+
findNativeComponentType(statement, foundConfigs, parser);
|
|
890
|
+
});
|
|
891
|
+
throwIfConfigNotfound(foundConfigs);
|
|
892
|
+
throwIfMoreThanOneConfig(foundConfigs);
|
|
893
|
+
const foundConfig = foundConfigs[0];
|
|
894
|
+
const namedExports = ast.body.filter(
|
|
895
|
+
node => node.type === 'ExportNamedDeclaration',
|
|
896
|
+
);
|
|
897
|
+
const commandsTypeNames = namedExports
|
|
898
|
+
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
|
|
899
|
+
.filter(Boolean);
|
|
900
|
+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
901
|
+
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
902
|
+
}
|
|
790
903
|
module.exports = {
|
|
791
904
|
wrapModuleSchema,
|
|
792
905
|
unwrapNullable,
|
|
@@ -802,6 +915,12 @@ module.exports = {
|
|
|
802
915
|
parseModuleName,
|
|
803
916
|
buildModuleSchema,
|
|
804
917
|
findNativeComponentType,
|
|
918
|
+
propertyNames,
|
|
805
919
|
getCommandOptions,
|
|
806
920
|
getOptions,
|
|
921
|
+
getCommandTypeNameAndOptionsExpression,
|
|
922
|
+
extendsForProp,
|
|
923
|
+
buildPropSchema,
|
|
924
|
+
getEventArgument,
|
|
925
|
+
findComponentConfig,
|
|
807
926
|
};
|
|
@@ -23,11 +23,14 @@ import type {
|
|
|
23
23
|
SchemaType,
|
|
24
24
|
NativeModuleEnumMap,
|
|
25
25
|
OptionsShape,
|
|
26
|
+
PropTypeAnnotation,
|
|
27
|
+
EventTypeAnnotation,
|
|
28
|
+
ObjectTypeAnnotation,
|
|
26
29
|
} from '../CodegenSchema.js';
|
|
27
30
|
|
|
28
31
|
import type {Parser} from './parser';
|
|
29
32
|
import type {ParserType} from './errors';
|
|
30
|
-
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
|
|
33
|
+
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
|
|
31
34
|
import type {ComponentSchemaBuilderConfig} from './schema.js';
|
|
32
35
|
|
|
33
36
|
const {
|
|
@@ -38,6 +41,7 @@ const {
|
|
|
38
41
|
isModuleRegistryCall,
|
|
39
42
|
verifyPlatforms,
|
|
40
43
|
} = require('./utils');
|
|
44
|
+
|
|
41
45
|
const {
|
|
42
46
|
throwIfPropertyValueTypeIsUnsupported,
|
|
43
47
|
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
|
|
@@ -52,6 +56,9 @@ const {
|
|
|
52
56
|
throwIfModuleInterfaceNotFound,
|
|
53
57
|
throwIfMoreThanOneModuleInterfaceParserError,
|
|
54
58
|
throwIfModuleInterfaceIsMisnamed,
|
|
59
|
+
throwIfMoreThanOneCodegenNativecommands,
|
|
60
|
+
throwIfConfigNotfound,
|
|
61
|
+
throwIfMoreThanOneConfig,
|
|
55
62
|
} = require('./error-utils');
|
|
56
63
|
|
|
57
64
|
const {
|
|
@@ -69,6 +76,11 @@ export type CommandOptions = $ReadOnly<{
|
|
|
69
76
|
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
|
|
70
77
|
type OptionsAST = Object;
|
|
71
78
|
|
|
79
|
+
type ExtendedPropResult = {
|
|
80
|
+
type: 'ReactNativeBuiltInType',
|
|
81
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
82
|
+
} | null;
|
|
83
|
+
|
|
72
84
|
function wrapModuleSchema(
|
|
73
85
|
nativeModuleSchema: NativeModuleSchema,
|
|
74
86
|
hasteModuleName: string,
|
|
@@ -314,7 +326,6 @@ function buildPropertySchema(
|
|
|
314
326
|
enumMap: {...NativeModuleEnumMap},
|
|
315
327
|
tryParse: ParserErrorCapturer,
|
|
316
328
|
cxxOnly: boolean,
|
|
317
|
-
resolveTypeAnnotation: $FlowFixMe,
|
|
318
329
|
translateTypeAnnotation: $FlowFixMe,
|
|
319
330
|
parser: Parser,
|
|
320
331
|
): NativeModulePropertyShape {
|
|
@@ -329,7 +340,12 @@ function buildPropertySchema(
|
|
|
329
340
|
: property.typeAnnotation;
|
|
330
341
|
}
|
|
331
342
|
|
|
332
|
-
|
|
343
|
+
const resolveTypeAnnotationFN = parser.getResolveTypeAnnotationFN();
|
|
344
|
+
({nullable, typeAnnotation: value} = resolveTypeAnnotationFN(
|
|
345
|
+
value,
|
|
346
|
+
types,
|
|
347
|
+
parser,
|
|
348
|
+
));
|
|
333
349
|
|
|
334
350
|
throwIfModuleTypeIsUnsupported(
|
|
335
351
|
hasteModuleName,
|
|
@@ -373,11 +389,9 @@ function buildSchemaFromConfigType(
|
|
|
373
389
|
ast: $FlowFixMe,
|
|
374
390
|
tryParse: ParserErrorCapturer,
|
|
375
391
|
parser: Parser,
|
|
376
|
-
resolveTypeAnnotation: $FlowFixMe,
|
|
377
392
|
translateTypeAnnotation: $FlowFixMe,
|
|
378
393
|
) => NativeModuleSchema,
|
|
379
394
|
parser: Parser,
|
|
380
|
-
resolveTypeAnnotation: $FlowFixMe,
|
|
381
395
|
translateTypeAnnotation: $FlowFixMe,
|
|
382
396
|
): SchemaType {
|
|
383
397
|
switch (configType) {
|
|
@@ -398,7 +412,6 @@ function buildSchemaFromConfigType(
|
|
|
398
412
|
ast,
|
|
399
413
|
tryParse,
|
|
400
414
|
parser,
|
|
401
|
-
resolveTypeAnnotation,
|
|
402
415
|
translateTypeAnnotation,
|
|
403
416
|
),
|
|
404
417
|
);
|
|
@@ -440,14 +453,12 @@ function buildSchema(
|
|
|
440
453
|
ast: $FlowFixMe,
|
|
441
454
|
tryParse: ParserErrorCapturer,
|
|
442
455
|
parser: Parser,
|
|
443
|
-
resolveTypeAnnotation: $FlowFixMe,
|
|
444
456
|
translateTypeAnnotation: $FlowFixMe,
|
|
445
457
|
) => NativeModuleSchema,
|
|
446
458
|
Visitor: ({isComponent: boolean, isModule: boolean}) => {
|
|
447
459
|
[type: string]: (node: $FlowFixMe) => void,
|
|
448
460
|
},
|
|
449
461
|
parser: Parser,
|
|
450
|
-
resolveTypeAnnotation: $FlowFixMe,
|
|
451
462
|
translateTypeAnnotation: $FlowFixMe,
|
|
452
463
|
): SchemaType {
|
|
453
464
|
// Early return for non-Spec JavaScript files
|
|
@@ -469,7 +480,6 @@ function buildSchema(
|
|
|
469
480
|
buildComponentSchema,
|
|
470
481
|
buildModuleSchema,
|
|
471
482
|
parser,
|
|
472
|
-
resolveTypeAnnotation,
|
|
473
483
|
translateTypeAnnotation,
|
|
474
484
|
);
|
|
475
485
|
}
|
|
@@ -564,7 +574,6 @@ const buildModuleSchema = (
|
|
|
564
574
|
ast: $FlowFixMe,
|
|
565
575
|
tryParse: ParserErrorCapturer,
|
|
566
576
|
parser: Parser,
|
|
567
|
-
resolveTypeAnnotation: $FlowFixMe,
|
|
568
577
|
translateTypeAnnotation: $FlowFixMe,
|
|
569
578
|
): NativeModuleSchema => {
|
|
570
579
|
const language = parser.language();
|
|
@@ -632,7 +641,6 @@ const buildModuleSchema = (
|
|
|
632
641
|
enumMap,
|
|
633
642
|
tryParse,
|
|
634
643
|
cxxOnly,
|
|
635
|
-
resolveTypeAnnotation,
|
|
636
644
|
translateTypeAnnotation,
|
|
637
645
|
parser,
|
|
638
646
|
),
|
|
@@ -765,10 +773,166 @@ function getOptions(optionsExpression: OptionsAST): ?OptionsShape {
|
|
|
765
773
|
'Failed to parse codegen options, cannot use both paperComponentName and paperComponentNameDeprecated',
|
|
766
774
|
);
|
|
767
775
|
}
|
|
768
|
-
|
|
769
776
|
return foundOptions;
|
|
770
777
|
}
|
|
771
778
|
|
|
779
|
+
function getCommandTypeNameAndOptionsExpression(
|
|
780
|
+
namedExport: $FlowFixMe,
|
|
781
|
+
parser: Parser,
|
|
782
|
+
): {
|
|
783
|
+
commandOptionsExpression: OptionsAST,
|
|
784
|
+
commandTypeName: string,
|
|
785
|
+
} | void {
|
|
786
|
+
let callExpression;
|
|
787
|
+
let calleeName;
|
|
788
|
+
try {
|
|
789
|
+
callExpression = namedExport.declaration.declarations[0].init;
|
|
790
|
+
calleeName = callExpression.callee.name;
|
|
791
|
+
} catch (e) {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
if (calleeName !== 'codegenNativeCommands') {
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
if (callExpression.arguments.length !== 1) {
|
|
800
|
+
throw new Error(
|
|
801
|
+
'codegenNativeCommands must be passed options including the supported commands',
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const typeArgumentParam =
|
|
806
|
+
parser.getTypeArgumentParamsFromDeclaration(callExpression)[0];
|
|
807
|
+
|
|
808
|
+
if (!parser.isGenericTypeAnnotation(typeArgumentParam.type)) {
|
|
809
|
+
throw new Error(
|
|
810
|
+
"codegenNativeCommands doesn't support inline definitions. Specify a file local type alias",
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return {
|
|
815
|
+
commandTypeName: parser.nameForGenericTypeAnnotation(typeArgumentParam),
|
|
816
|
+
commandOptionsExpression: callExpression.arguments[0],
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
function propertyNames(
|
|
821
|
+
properties: $ReadOnlyArray<$FlowFixMe>,
|
|
822
|
+
): $ReadOnlyArray<$FlowFixMe> {
|
|
823
|
+
return properties
|
|
824
|
+
.map(property => property && property.key && property.key.name)
|
|
825
|
+
.filter(Boolean);
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function extendsForProp(
|
|
829
|
+
prop: PropAST,
|
|
830
|
+
types: TypeDeclarationMap,
|
|
831
|
+
parser: Parser,
|
|
832
|
+
): ExtendedPropResult {
|
|
833
|
+
const argument = parser.argumentForProp(prop);
|
|
834
|
+
|
|
835
|
+
if (!argument) {
|
|
836
|
+
console.log('null', prop);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const name = parser.nameForArgument(prop);
|
|
840
|
+
|
|
841
|
+
if (types[name] != null) {
|
|
842
|
+
// This type is locally defined in the file
|
|
843
|
+
return null;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
switch (name) {
|
|
847
|
+
case 'ViewProps':
|
|
848
|
+
return {
|
|
849
|
+
type: 'ReactNativeBuiltInType',
|
|
850
|
+
knownTypeName: 'ReactNativeCoreViewProps',
|
|
851
|
+
};
|
|
852
|
+
default: {
|
|
853
|
+
throw new Error(`Unable to handle prop spread: ${name}`);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
function buildPropSchema(
|
|
859
|
+
property: PropAST,
|
|
860
|
+
types: TypeDeclarationMap,
|
|
861
|
+
parser: Parser,
|
|
862
|
+
): ?NamedShape<PropTypeAnnotation> {
|
|
863
|
+
const getSchemaInfoFN = parser.getGetSchemaInfoFN();
|
|
864
|
+
const info = getSchemaInfoFN(property, types);
|
|
865
|
+
if (info == null) {
|
|
866
|
+
return null;
|
|
867
|
+
}
|
|
868
|
+
const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;
|
|
869
|
+
|
|
870
|
+
const getTypeAnnotationFN = parser.getGetTypeAnnotationFN();
|
|
871
|
+
|
|
872
|
+
return {
|
|
873
|
+
name,
|
|
874
|
+
optional,
|
|
875
|
+
typeAnnotation: getTypeAnnotationFN(
|
|
876
|
+
name,
|
|
877
|
+
typeAnnotation,
|
|
878
|
+
defaultValue,
|
|
879
|
+
withNullDefault,
|
|
880
|
+
types,
|
|
881
|
+
parser,
|
|
882
|
+
buildPropSchema,
|
|
883
|
+
),
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
|
|
888
|
+
* LTI update could not be added via codemod */
|
|
889
|
+
function getEventArgument(
|
|
890
|
+
argumentProps: PropAST,
|
|
891
|
+
buildPropertiesForEvent: (
|
|
892
|
+
property: PropAST,
|
|
893
|
+
parser: Parser,
|
|
894
|
+
) => NamedShape<EventTypeAnnotation>,
|
|
895
|
+
parser: Parser,
|
|
896
|
+
): ObjectTypeAnnotation<EventTypeAnnotation> {
|
|
897
|
+
return {
|
|
898
|
+
type: 'ObjectTypeAnnotation',
|
|
899
|
+
properties: argumentProps.map(member =>
|
|
900
|
+
buildPropertiesForEvent(member, parser),
|
|
901
|
+
),
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/* $FlowFixMe[signature-verification-failure] there's no flowtype for AST.
|
|
906
|
+
* TODO(T108222691): Use flow-types for @babel/parser */
|
|
907
|
+
function findComponentConfig(ast: $FlowFixMe, parser: Parser) {
|
|
908
|
+
const foundConfigs: Array<{[string]: string}> = [];
|
|
909
|
+
|
|
910
|
+
const defaultExports = ast.body.filter(
|
|
911
|
+
node => node.type === 'ExportDefaultDeclaration',
|
|
912
|
+
);
|
|
913
|
+
|
|
914
|
+
defaultExports.forEach(statement => {
|
|
915
|
+
findNativeComponentType(statement, foundConfigs, parser);
|
|
916
|
+
});
|
|
917
|
+
|
|
918
|
+
throwIfConfigNotfound(foundConfigs);
|
|
919
|
+
throwIfMoreThanOneConfig(foundConfigs);
|
|
920
|
+
|
|
921
|
+
const foundConfig = foundConfigs[0];
|
|
922
|
+
|
|
923
|
+
const namedExports = ast.body.filter(
|
|
924
|
+
node => node.type === 'ExportNamedDeclaration',
|
|
925
|
+
);
|
|
926
|
+
|
|
927
|
+
const commandsTypeNames = namedExports
|
|
928
|
+
.map(statement => getCommandTypeNameAndOptionsExpression(statement, parser))
|
|
929
|
+
.filter(Boolean);
|
|
930
|
+
|
|
931
|
+
throwIfMoreThanOneCodegenNativecommands(commandsTypeNames);
|
|
932
|
+
|
|
933
|
+
return createComponentConfig(foundConfig, commandsTypeNames);
|
|
934
|
+
}
|
|
935
|
+
|
|
772
936
|
module.exports = {
|
|
773
937
|
wrapModuleSchema,
|
|
774
938
|
unwrapNullable,
|
|
@@ -784,6 +948,12 @@ module.exports = {
|
|
|
784
948
|
parseModuleName,
|
|
785
949
|
buildModuleSchema,
|
|
786
950
|
findNativeComponentType,
|
|
951
|
+
propertyNames,
|
|
787
952
|
getCommandOptions,
|
|
788
953
|
getOptions,
|
|
954
|
+
getCommandTypeNameAndOptionsExpression,
|
|
955
|
+
extendsForProp,
|
|
956
|
+
buildPropSchema,
|
|
957
|
+
getEventArgument,
|
|
958
|
+
findComponentConfig,
|
|
789
959
|
};
|
|
@@ -126,6 +126,15 @@ function emitDouble(nullable) {
|
|
|
126
126
|
type: 'DoubleTypeAnnotation',
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
|
+
function emitDoubleProp(name, optional) {
|
|
130
|
+
return {
|
|
131
|
+
name,
|
|
132
|
+
optional,
|
|
133
|
+
typeAnnotation: {
|
|
134
|
+
type: 'DoubleTypeAnnotation',
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
129
138
|
function emitVoid(nullable) {
|
|
130
139
|
return wrapNullable(nullable, {
|
|
131
140
|
type: 'VoidTypeAnnotation',
|
|
@@ -171,6 +180,15 @@ function emitString(nullable) {
|
|
|
171
180
|
type: 'StringTypeAnnotation',
|
|
172
181
|
});
|
|
173
182
|
}
|
|
183
|
+
function emitStringProp(name, optional) {
|
|
184
|
+
return {
|
|
185
|
+
name,
|
|
186
|
+
optional,
|
|
187
|
+
typeAnnotation: {
|
|
188
|
+
type: 'StringTypeAnnotation',
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
174
192
|
function typeAliasResolution(
|
|
175
193
|
typeResolution,
|
|
176
194
|
objectTypeAnnotation,
|
|
@@ -315,6 +333,12 @@ function emitGenericObject(nullable) {
|
|
|
315
333
|
type: 'GenericObjectTypeAnnotation',
|
|
316
334
|
});
|
|
317
335
|
}
|
|
336
|
+
function emitDictionary(nullable, valueType) {
|
|
337
|
+
return wrapNullable(nullable, {
|
|
338
|
+
type: 'GenericObjectTypeAnnotation',
|
|
339
|
+
dictionaryValueType: valueType,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
318
342
|
function emitObject(nullable, properties) {
|
|
319
343
|
return wrapNullable(nullable, {
|
|
320
344
|
type: 'ObjectTypeAnnotation',
|
|
@@ -326,6 +350,15 @@ function emitFloat(nullable) {
|
|
|
326
350
|
type: 'FloatTypeAnnotation',
|
|
327
351
|
});
|
|
328
352
|
}
|
|
353
|
+
function emitFloatProp(name, optional) {
|
|
354
|
+
return {
|
|
355
|
+
name,
|
|
356
|
+
optional,
|
|
357
|
+
typeAnnotation: {
|
|
358
|
+
type: 'FloatTypeAnnotation',
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
329
362
|
function emitUnion(nullable, hasteModuleName, typeAnnotation, parser) {
|
|
330
363
|
const unionTypes = parser.remapUnionTypeAnnotationMemberNames(
|
|
331
364
|
typeAnnotation.types,
|
|
@@ -462,6 +495,7 @@ function Visitor(infoMap) {
|
|
|
462
495
|
};
|
|
463
496
|
}
|
|
464
497
|
function emitPartial(
|
|
498
|
+
nullable,
|
|
465
499
|
hasteModuleName,
|
|
466
500
|
typeAnnotation,
|
|
467
501
|
types,
|
|
@@ -469,7 +503,6 @@ function emitPartial(
|
|
|
469
503
|
enumMap,
|
|
470
504
|
tryParse,
|
|
471
505
|
cxxOnly,
|
|
472
|
-
nullable,
|
|
473
506
|
parser,
|
|
474
507
|
) {
|
|
475
508
|
throwIfPartialWithMoreParameter(typeAnnotation);
|
|
@@ -502,59 +535,75 @@ function emitCommonTypes(
|
|
|
502
535
|
nullable,
|
|
503
536
|
parser,
|
|
504
537
|
) {
|
|
538
|
+
const typeMap = {
|
|
539
|
+
Stringish: emitStringish,
|
|
540
|
+
Int32: emitInt32,
|
|
541
|
+
Double: emitDouble,
|
|
542
|
+
Float: emitFloat,
|
|
543
|
+
UnsafeObject: emitGenericObject,
|
|
544
|
+
Object: emitGenericObject,
|
|
545
|
+
$Partial: emitPartial,
|
|
546
|
+
Partial: emitPartial,
|
|
547
|
+
BooleanTypeAnnotation: emitBoolean,
|
|
548
|
+
NumberTypeAnnotation: emitNumber,
|
|
549
|
+
VoidTypeAnnotation: emitVoid,
|
|
550
|
+
StringTypeAnnotation: emitString,
|
|
551
|
+
MixedTypeAnnotation: cxxOnly ? emitMixed : emitGenericObject,
|
|
552
|
+
};
|
|
553
|
+
const typeAnnotationName = parser.convertKeywordToTypeAnnotation(
|
|
554
|
+
typeAnnotation.type,
|
|
555
|
+
);
|
|
556
|
+
const simpleEmitter = typeMap[typeAnnotationName];
|
|
557
|
+
if (simpleEmitter) {
|
|
558
|
+
return simpleEmitter(nullable);
|
|
559
|
+
}
|
|
505
560
|
const genericTypeAnnotationName =
|
|
506
561
|
parser.nameForGenericTypeAnnotation(typeAnnotation);
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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
|
-
}
|
|
562
|
+
const emitter = typeMap[genericTypeAnnotationName];
|
|
563
|
+
if (!emitter) {
|
|
564
|
+
return null;
|
|
541
565
|
}
|
|
566
|
+
return emitter(
|
|
567
|
+
nullable,
|
|
568
|
+
hasteModuleName,
|
|
569
|
+
typeAnnotation,
|
|
570
|
+
types,
|
|
571
|
+
aliasMap,
|
|
572
|
+
enumMap,
|
|
573
|
+
tryParse,
|
|
574
|
+
cxxOnly,
|
|
575
|
+
parser,
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
function emitBoolProp(name, optional) {
|
|
579
|
+
return {
|
|
580
|
+
name,
|
|
581
|
+
optional,
|
|
582
|
+
typeAnnotation: {
|
|
583
|
+
type: 'BooleanTypeAnnotation',
|
|
584
|
+
},
|
|
585
|
+
};
|
|
542
586
|
}
|
|
543
587
|
module.exports = {
|
|
544
588
|
emitArrayType,
|
|
545
589
|
emitBoolean,
|
|
590
|
+
emitBoolProp,
|
|
546
591
|
emitDouble,
|
|
592
|
+
emitDoubleProp,
|
|
547
593
|
emitFloat,
|
|
594
|
+
emitFloatProp,
|
|
548
595
|
emitFunction,
|
|
549
596
|
emitInt32,
|
|
550
597
|
emitNumber,
|
|
551
598
|
emitGenericObject,
|
|
599
|
+
emitDictionary,
|
|
552
600
|
emitObject,
|
|
553
601
|
emitPromise,
|
|
554
602
|
emitRootTag,
|
|
555
603
|
emitVoid,
|
|
556
604
|
emitString,
|
|
557
605
|
emitStringish,
|
|
606
|
+
emitStringProp,
|
|
558
607
|
emitMixed,
|
|
559
608
|
emitUnion,
|
|
560
609
|
emitPartial,
|