@react-native/codegen 0.72.1
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 +21 -0
- package/lib/CodegenSchema.js +11 -0
- package/lib/CodegenSchema.js.flow +351 -0
- package/lib/SchemaValidator.js +54 -0
- package/lib/SchemaValidator.js.flow +67 -0
- package/lib/cli/combine/combine-js-to-schema-cli.js +50 -0
- package/lib/cli/combine/combine-js-to-schema-cli.js.flow +49 -0
- package/lib/cli/combine/combine-js-to-schema.js +110 -0
- package/lib/cli/combine/combine-js-to-schema.js.flow +48 -0
- package/lib/cli/combine/combine-utils.js +118 -0
- package/lib/cli/combine/combine-utils.js.flow +85 -0
- package/lib/cli/generators/generate-all.js +65 -0
- package/lib/cli/generators/generate-all.js.flow +66 -0
- package/lib/cli/parser/parser-cli.js +55 -0
- package/lib/cli/parser/parser-cli.js.flow +18 -0
- package/lib/cli/parser/parser.js +35 -0
- package/lib/cli/parser/parser.js.flow +37 -0
- package/lib/cli/parser/parser.sh +15 -0
- package/lib/generators/RNCodegen.js +207 -0
- package/lib/generators/RNCodegen.js.flow +284 -0
- package/lib/generators/Utils.js +31 -0
- package/lib/generators/Utils.js.flow +33 -0
- package/lib/generators/__test_fixtures__/fixtures.js +79 -0
- package/lib/generators/__test_fixtures__/fixtures.js.flow +82 -0
- package/lib/generators/components/ComponentsGeneratorUtils.js +217 -0
- package/lib/generators/components/ComponentsGeneratorUtils.js.flow +304 -0
- package/lib/generators/components/CppHelpers.js +188 -0
- package/lib/generators/components/CppHelpers.js.flow +228 -0
- package/lib/generators/components/GenerateComponentDescriptorH.js +75 -0
- package/lib/generators/components/GenerateComponentDescriptorH.js.flow +95 -0
- package/lib/generators/components/GenerateComponentHObjCpp.js +327 -0
- package/lib/generators/components/GenerateComponentHObjCpp.js.flow +418 -0
- package/lib/generators/components/GenerateEventEmitterCpp.js +199 -0
- package/lib/generators/components/GenerateEventEmitterCpp.js.flow +273 -0
- package/lib/generators/components/GenerateEventEmitterH.js +224 -0
- package/lib/generators/components/GenerateEventEmitterH.js.flow +315 -0
- package/lib/generators/components/GeneratePropsCpp.js +126 -0
- package/lib/generators/components/GeneratePropsCpp.js.flow +161 -0
- package/lib/generators/components/GeneratePropsH.js +606 -0
- package/lib/generators/components/GeneratePropsH.js.flow +778 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js +287 -0
- package/lib/generators/components/GeneratePropsJavaDelegate.js.flow +348 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js +237 -0
- package/lib/generators/components/GeneratePropsJavaInterface.js.flow +288 -0
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js +157 -0
- package/lib/generators/components/GeneratePropsJavaPojo/PojoCollector.js.flow +187 -0
- package/lib/generators/components/GeneratePropsJavaPojo/index.js +66 -0
- package/lib/generators/components/GeneratePropsJavaPojo/index.js.flow +80 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js +273 -0
- package/lib/generators/components/GeneratePropsJavaPojo/serializePojo.js.flow +296 -0
- package/lib/generators/components/GenerateShadowNodeCpp.js +73 -0
- package/lib/generators/components/GenerateShadowNodeCpp.js.flow +95 -0
- package/lib/generators/components/GenerateShadowNodeH.js +91 -0
- package/lib/generators/components/GenerateShadowNodeH.js.flow +124 -0
- package/lib/generators/components/GenerateStateCpp.js +70 -0
- package/lib/generators/components/GenerateStateCpp.js.flow +92 -0
- package/lib/generators/components/GenerateStateH.js +93 -0
- package/lib/generators/components/GenerateStateH.js.flow +111 -0
- package/lib/generators/components/GenerateTests.js +165 -0
- package/lib/generators/components/GenerateTests.js.flow +219 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js +91 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderH.js.flow +107 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js +89 -0
- package/lib/generators/components/GenerateThirdPartyFabricComponentsProviderObjCpp.js.flow +105 -0
- package/lib/generators/components/GenerateViewConfigJs.js +410 -0
- package/lib/generators/components/GenerateViewConfigJs.js.flow +484 -0
- package/lib/generators/components/JavaHelpers.js +87 -0
- package/lib/generators/components/JavaHelpers.js.flow +119 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js +1641 -0
- package/lib/generators/components/__test_fixtures__/fixtures.js.flow +1672 -0
- package/lib/generators/modules/GenerateModuleCpp.js +299 -0
- package/lib/generators/modules/GenerateModuleCpp.js.flow +278 -0
- package/lib/generators/modules/GenerateModuleH.js +373 -0
- package/lib/generators/modules/GenerateModuleH.js.flow +355 -0
- package/lib/generators/modules/GenerateModuleJavaSpec.js +571 -0
- package/lib/generators/modules/GenerateModuleJavaSpec.js.flow +553 -0
- package/lib/generators/modules/GenerateModuleJniCpp.js +505 -0
- package/lib/generators/modules/GenerateModuleJniCpp.js.flow +523 -0
- package/lib/generators/modules/GenerateModuleJniH.js +138 -0
- package/lib/generators/modules/GenerateModuleJniH.js.flow +148 -0
- package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js +262 -0
- package/lib/generators/modules/GenerateModuleObjCpp/StructCollector.js.flow +208 -0
- package/lib/generators/modules/GenerateModuleObjCpp/Utils.js +25 -0
- package/lib/generators/modules/GenerateModuleObjCpp/Utils.js.flow +32 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js +337 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js.flow +288 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js +330 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeRegularStruct.js.flow +279 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeStruct.js +25 -0
- package/lib/generators/modules/GenerateModuleObjCpp/header/serializeStruct.js.flow +35 -0
- package/lib/generators/modules/GenerateModuleObjCpp/index.js +185 -0
- package/lib/generators/modules/GenerateModuleObjCpp/index.js.flow +219 -0
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js +529 -0
- package/lib/generators/modules/GenerateModuleObjCpp/serializeMethod.js.flow +510 -0
- package/lib/generators/modules/GenerateModuleObjCpp/source/serializeModule.js +96 -0
- package/lib/generators/modules/GenerateModuleObjCpp/source/serializeModule.js.flow +121 -0
- package/lib/generators/modules/Utils.js +34 -0
- package/lib/generators/modules/Utils.js.flow +53 -0
- package/lib/generators/modules/__test_fixtures__/fixtures.js +1658 -0
- package/lib/generators/modules/__test_fixtures__/fixtures.js.flow +1669 -0
- package/lib/parsers/consistency/compareSnaps.js +84 -0
- package/lib/parsers/error-utils.js +284 -0
- package/lib/parsers/error-utils.js.flow +295 -0
- package/lib/parsers/errors.js +350 -0
- package/lib/parsers/errors.js.flow +439 -0
- package/lib/parsers/flow/components/__test_fixtures__/failures.js +583 -0
- package/lib/parsers/flow/components/__test_fixtures__/failures.js.flow +600 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js +1001 -0
- package/lib/parsers/flow/components/__test_fixtures__/fixtures.js.flow +1016 -0
- package/lib/parsers/flow/components/commands.js +104 -0
- package/lib/parsers/flow/components/commands.js.flow +123 -0
- package/lib/parsers/flow/components/componentsUtils.js +446 -0
- package/lib/parsers/flow/components/componentsUtils.js.flow +496 -0
- package/lib/parsers/flow/components/events.js +233 -0
- package/lib/parsers/flow/components/events.js.flow +262 -0
- package/lib/parsers/flow/components/extends.js +52 -0
- package/lib/parsers/flow/components/extends.js.flow +66 -0
- package/lib/parsers/flow/components/index.js +267 -0
- package/lib/parsers/flow/components/index.js.flow +226 -0
- package/lib/parsers/flow/components/options.js +72 -0
- package/lib/parsers/flow/components/options.js.flow +87 -0
- package/lib/parsers/flow/components/props.js +47 -0
- package/lib/parsers/flow/components/props.js.flow +60 -0
- package/lib/parsers/flow/components/schema.js +106 -0
- package/lib/parsers/flow/components/schema.js.flow +62 -0
- package/lib/parsers/flow/index.js +80 -0
- package/lib/parsers/flow/index.js.flow +85 -0
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js +215 -0
- package/lib/parsers/flow/modules/__test_fixtures__/failures.js.flow +223 -0
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js +724 -0
- package/lib/parsers/flow/modules/__test_fixtures__/fixtures.js.flow +747 -0
- package/lib/parsers/flow/modules/index.js +528 -0
- package/lib/parsers/flow/modules/index.js.flow +420 -0
- package/lib/parsers/flow/parser.js +109 -0
- package/lib/parsers/flow/parser.js.flow +83 -0
- package/lib/parsers/flow/utils.js +108 -0
- package/lib/parsers/flow/utils.js.flow +123 -0
- package/lib/parsers/parser.js +11 -0
- package/lib/parsers/parser.js.flow +70 -0
- package/lib/parsers/parserMock.js +96 -0
- package/lib/parsers/parserMock.js.flow +68 -0
- package/lib/parsers/parsers-commons.js +448 -0
- package/lib/parsers/parsers-commons.js.flow +447 -0
- package/lib/parsers/parsers-primitives.js +387 -0
- package/lib/parsers/parsers-primitives.js.flow +383 -0
- package/lib/parsers/parsers-utils.js +18 -0
- package/lib/parsers/parsers-utils.js.flow +19 -0
- package/lib/parsers/schema/index.js +23 -0
- package/lib/parsers/schema/index.js.flow +26 -0
- package/lib/parsers/typescript/components/__test_fixtures__/failures.js +489 -0
- package/lib/parsers/typescript/components/__test_fixtures__/failures.js.flow +505 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js +1151 -0
- package/lib/parsers/typescript/components/__test_fixtures__/fixtures.js.flow +1168 -0
- package/lib/parsers/typescript/components/commands.js +132 -0
- package/lib/parsers/typescript/components/commands.js.flow +151 -0
- package/lib/parsers/typescript/components/componentsUtils.js +471 -0
- package/lib/parsers/typescript/components/componentsUtils.js.flow +521 -0
- package/lib/parsers/typescript/components/events.js +240 -0
- package/lib/parsers/typescript/components/events.js.flow +262 -0
- package/lib/parsers/typescript/components/extends.js +88 -0
- package/lib/parsers/typescript/components/extends.js.flow +103 -0
- package/lib/parsers/typescript/components/index.js +277 -0
- package/lib/parsers/typescript/components/index.js.flow +239 -0
- package/lib/parsers/typescript/components/options.js +72 -0
- package/lib/parsers/typescript/components/options.js.flow +87 -0
- package/lib/parsers/typescript/components/props.js +39 -0
- package/lib/parsers/typescript/components/props.js.flow +48 -0
- package/lib/parsers/typescript/components/schema.js +106 -0
- package/lib/parsers/typescript/components/schema.js.flow +62 -0
- package/lib/parsers/typescript/index.js +85 -0
- package/lib/parsers/typescript/index.js.flow +94 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js +160 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/failures.js.flow +167 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js +701 -0
- package/lib/parsers/typescript/modules/__test_fixtures__/fixtures.js.flow +727 -0
- package/lib/parsers/typescript/modules/index.js +543 -0
- package/lib/parsers/typescript/modules/index.js.flow +435 -0
- package/lib/parsers/typescript/parseTopLevelType.js +203 -0
- package/lib/parsers/typescript/parseTopLevelType.js.flow +243 -0
- package/lib/parsers/typescript/parser.js +105 -0
- package/lib/parsers/typescript/parser.js.flow +87 -0
- package/lib/parsers/typescript/utils.js +101 -0
- package/lib/parsers/typescript/utils.js.flow +115 -0
- package/lib/parsers/utils.js +273 -0
- package/lib/parsers/utils.js.flow +266 -0
- package/package.json +45 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
function upperCaseFirst(inString) {
|
|
14
|
+
if (inString.length === 0) {
|
|
15
|
+
return inString;
|
|
16
|
+
}
|
|
17
|
+
return inString[0].toUpperCase() + inString.slice(1);
|
|
18
|
+
}
|
|
19
|
+
function toSafeCppString(input) {
|
|
20
|
+
return input.split('-').map(upperCaseFirst).join('');
|
|
21
|
+
}
|
|
22
|
+
function toIntEnumValueName(propName, value) {
|
|
23
|
+
return `${toSafeCppString(propName)}${value}`;
|
|
24
|
+
}
|
|
25
|
+
function getCppTypeForAnnotation(type) {
|
|
26
|
+
switch (type) {
|
|
27
|
+
case 'BooleanTypeAnnotation':
|
|
28
|
+
return 'bool';
|
|
29
|
+
case 'StringTypeAnnotation':
|
|
30
|
+
return 'std::string';
|
|
31
|
+
case 'Int32TypeAnnotation':
|
|
32
|
+
return 'int';
|
|
33
|
+
case 'DoubleTypeAnnotation':
|
|
34
|
+
return 'double';
|
|
35
|
+
case 'FloatTypeAnnotation':
|
|
36
|
+
return 'Float';
|
|
37
|
+
default:
|
|
38
|
+
type;
|
|
39
|
+
throw new Error(`Received invalid typeAnnotation ${type}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function getImports(properties) {
|
|
43
|
+
const imports = new Set();
|
|
44
|
+
function addImportsForNativeName(name) {
|
|
45
|
+
switch (name) {
|
|
46
|
+
case 'ColorPrimitive':
|
|
47
|
+
return;
|
|
48
|
+
case 'PointPrimitive':
|
|
49
|
+
return;
|
|
50
|
+
case 'EdgeInsetsPrimitive':
|
|
51
|
+
return;
|
|
52
|
+
case 'ImageRequestPrimitive':
|
|
53
|
+
return;
|
|
54
|
+
case 'ImageSourcePrimitive':
|
|
55
|
+
imports.add('#include <react/renderer/components/image/conversions.h>');
|
|
56
|
+
return;
|
|
57
|
+
default:
|
|
58
|
+
name;
|
|
59
|
+
throw new Error(`Invalid name, got ${name}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
properties.forEach(prop => {
|
|
63
|
+
const typeAnnotation = prop.typeAnnotation;
|
|
64
|
+
if (typeAnnotation.type === 'ReservedPropTypeAnnotation') {
|
|
65
|
+
addImportsForNativeName(typeAnnotation.name);
|
|
66
|
+
}
|
|
67
|
+
if (
|
|
68
|
+
typeAnnotation.type === 'ArrayTypeAnnotation' &&
|
|
69
|
+
typeAnnotation.elementType.type === 'ReservedPropTypeAnnotation'
|
|
70
|
+
) {
|
|
71
|
+
addImportsForNativeName(typeAnnotation.elementType.name);
|
|
72
|
+
}
|
|
73
|
+
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
|
|
74
|
+
const objectImports = getImports(typeAnnotation.properties);
|
|
75
|
+
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
76
|
+
objectImports.forEach(imports.add, imports);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return imports;
|
|
80
|
+
}
|
|
81
|
+
function generateEventStructName(parts = []) {
|
|
82
|
+
const additional = parts.map(toSafeCppString).join('');
|
|
83
|
+
return `${additional}`;
|
|
84
|
+
}
|
|
85
|
+
function generateStructName(componentName, parts = []) {
|
|
86
|
+
const additional = parts.map(toSafeCppString).join('');
|
|
87
|
+
return `${componentName}${additional}Struct`;
|
|
88
|
+
}
|
|
89
|
+
function getEnumName(componentName, propName) {
|
|
90
|
+
const uppercasedPropName = toSafeCppString(propName);
|
|
91
|
+
return `${componentName}${uppercasedPropName}`;
|
|
92
|
+
}
|
|
93
|
+
function getEnumMaskName(enumName) {
|
|
94
|
+
return `${enumName}Mask`;
|
|
95
|
+
}
|
|
96
|
+
function convertDefaultTypeToString(componentName, prop) {
|
|
97
|
+
const typeAnnotation = prop.typeAnnotation;
|
|
98
|
+
switch (typeAnnotation.type) {
|
|
99
|
+
case 'BooleanTypeAnnotation':
|
|
100
|
+
if (typeAnnotation.default == null) {
|
|
101
|
+
return '';
|
|
102
|
+
}
|
|
103
|
+
return String(typeAnnotation.default);
|
|
104
|
+
case 'StringTypeAnnotation':
|
|
105
|
+
if (typeAnnotation.default == null) {
|
|
106
|
+
return '';
|
|
107
|
+
}
|
|
108
|
+
return `"${typeAnnotation.default}"`;
|
|
109
|
+
case 'Int32TypeAnnotation':
|
|
110
|
+
return String(typeAnnotation.default);
|
|
111
|
+
case 'DoubleTypeAnnotation':
|
|
112
|
+
const defaultDoubleVal = typeAnnotation.default;
|
|
113
|
+
return parseInt(defaultDoubleVal, 10) === defaultDoubleVal
|
|
114
|
+
? typeAnnotation.default.toFixed(1)
|
|
115
|
+
: String(typeAnnotation.default);
|
|
116
|
+
case 'FloatTypeAnnotation':
|
|
117
|
+
const defaultFloatVal = typeAnnotation.default;
|
|
118
|
+
if (defaultFloatVal == null) {
|
|
119
|
+
return '';
|
|
120
|
+
}
|
|
121
|
+
return parseInt(defaultFloatVal, 10) === defaultFloatVal
|
|
122
|
+
? defaultFloatVal.toFixed(1)
|
|
123
|
+
: String(typeAnnotation.default);
|
|
124
|
+
case 'ReservedPropTypeAnnotation':
|
|
125
|
+
switch (typeAnnotation.name) {
|
|
126
|
+
case 'ColorPrimitive':
|
|
127
|
+
return '';
|
|
128
|
+
case 'ImageSourcePrimitive':
|
|
129
|
+
return '';
|
|
130
|
+
case 'ImageRequestPrimitive':
|
|
131
|
+
return '';
|
|
132
|
+
case 'PointPrimitive':
|
|
133
|
+
return '';
|
|
134
|
+
case 'EdgeInsetsPrimitive':
|
|
135
|
+
return '';
|
|
136
|
+
default:
|
|
137
|
+
typeAnnotation.name;
|
|
138
|
+
throw new Error(
|
|
139
|
+
`Unsupported type annotation: ${typeAnnotation.name}`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
case 'ArrayTypeAnnotation': {
|
|
143
|
+
const elementType = typeAnnotation.elementType;
|
|
144
|
+
switch (elementType.type) {
|
|
145
|
+
case 'StringEnumTypeAnnotation':
|
|
146
|
+
if (elementType.default == null) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
'A default is required for array StringEnumTypeAnnotation',
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
const enumName = getEnumName(componentName, prop.name);
|
|
152
|
+
const enumMaskName = getEnumMaskName(enumName);
|
|
153
|
+
const defaultValue = `${enumName}::${toSafeCppString(
|
|
154
|
+
elementType.default,
|
|
155
|
+
)}`;
|
|
156
|
+
return `static_cast<${enumMaskName}>(${defaultValue})`;
|
|
157
|
+
default:
|
|
158
|
+
return '';
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
case 'ObjectTypeAnnotation': {
|
|
162
|
+
return '';
|
|
163
|
+
}
|
|
164
|
+
case 'StringEnumTypeAnnotation':
|
|
165
|
+
return `${getEnumName(componentName, prop.name)}::${toSafeCppString(
|
|
166
|
+
typeAnnotation.default,
|
|
167
|
+
)}`;
|
|
168
|
+
case 'Int32EnumTypeAnnotation':
|
|
169
|
+
return `${getEnumName(componentName, prop.name)}::${toIntEnumValueName(
|
|
170
|
+
prop.name,
|
|
171
|
+
typeAnnotation.default,
|
|
172
|
+
)}`;
|
|
173
|
+
default:
|
|
174
|
+
typeAnnotation;
|
|
175
|
+
throw new Error(`Unsupported type annotation: ${typeAnnotation.type}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
module.exports = {
|
|
179
|
+
convertDefaultTypeToString,
|
|
180
|
+
getCppTypeForAnnotation,
|
|
181
|
+
getEnumName,
|
|
182
|
+
getEnumMaskName,
|
|
183
|
+
getImports,
|
|
184
|
+
toSafeCppString,
|
|
185
|
+
toIntEnumValueName,
|
|
186
|
+
generateStructName,
|
|
187
|
+
generateEventStructName,
|
|
188
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
import type {NamedShape, PropTypeAnnotation} from '../../CodegenSchema';
|
|
13
|
+
|
|
14
|
+
function upperCaseFirst(inString: string): string {
|
|
15
|
+
if (inString.length === 0) {
|
|
16
|
+
return inString;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return inString[0].toUpperCase() + inString.slice(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function toSafeCppString(input: string): string {
|
|
23
|
+
return input.split('-').map(upperCaseFirst).join('');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function toIntEnumValueName(propName: string, value: number): string {
|
|
27
|
+
return `${toSafeCppString(propName)}${value}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getCppTypeForAnnotation(
|
|
31
|
+
type:
|
|
32
|
+
| 'BooleanTypeAnnotation'
|
|
33
|
+
| 'StringTypeAnnotation'
|
|
34
|
+
| 'Int32TypeAnnotation'
|
|
35
|
+
| 'DoubleTypeAnnotation'
|
|
36
|
+
| 'FloatTypeAnnotation',
|
|
37
|
+
): string {
|
|
38
|
+
switch (type) {
|
|
39
|
+
case 'BooleanTypeAnnotation':
|
|
40
|
+
return 'bool';
|
|
41
|
+
case 'StringTypeAnnotation':
|
|
42
|
+
return 'std::string';
|
|
43
|
+
case 'Int32TypeAnnotation':
|
|
44
|
+
return 'int';
|
|
45
|
+
case 'DoubleTypeAnnotation':
|
|
46
|
+
return 'double';
|
|
47
|
+
case 'FloatTypeAnnotation':
|
|
48
|
+
return 'Float';
|
|
49
|
+
default:
|
|
50
|
+
(type: empty);
|
|
51
|
+
throw new Error(`Received invalid typeAnnotation ${type}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getImports(
|
|
56
|
+
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
57
|
+
): Set<string> {
|
|
58
|
+
const imports: Set<string> = new Set();
|
|
59
|
+
|
|
60
|
+
function addImportsForNativeName(
|
|
61
|
+
name:
|
|
62
|
+
| 'ColorPrimitive'
|
|
63
|
+
| 'EdgeInsetsPrimitive'
|
|
64
|
+
| 'ImageRequestPrimitive'
|
|
65
|
+
| 'ImageSourcePrimitive'
|
|
66
|
+
| 'PointPrimitive',
|
|
67
|
+
) {
|
|
68
|
+
switch (name) {
|
|
69
|
+
case 'ColorPrimitive':
|
|
70
|
+
return;
|
|
71
|
+
case 'PointPrimitive':
|
|
72
|
+
return;
|
|
73
|
+
case 'EdgeInsetsPrimitive':
|
|
74
|
+
return;
|
|
75
|
+
case 'ImageRequestPrimitive':
|
|
76
|
+
return;
|
|
77
|
+
case 'ImageSourcePrimitive':
|
|
78
|
+
imports.add('#include <react/renderer/components/image/conversions.h>');
|
|
79
|
+
return;
|
|
80
|
+
default:
|
|
81
|
+
(name: empty);
|
|
82
|
+
throw new Error(`Invalid name, got ${name}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
properties.forEach(prop => {
|
|
87
|
+
const typeAnnotation = prop.typeAnnotation;
|
|
88
|
+
|
|
89
|
+
if (typeAnnotation.type === 'ReservedPropTypeAnnotation') {
|
|
90
|
+
addImportsForNativeName(typeAnnotation.name);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (
|
|
94
|
+
typeAnnotation.type === 'ArrayTypeAnnotation' &&
|
|
95
|
+
typeAnnotation.elementType.type === 'ReservedPropTypeAnnotation'
|
|
96
|
+
) {
|
|
97
|
+
addImportsForNativeName(typeAnnotation.elementType.name);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
|
|
101
|
+
const objectImports = getImports(typeAnnotation.properties);
|
|
102
|
+
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
103
|
+
objectImports.forEach(imports.add, imports);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return imports;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function generateEventStructName(parts: $ReadOnlyArray<string> = []): string {
|
|
111
|
+
const additional = parts.map(toSafeCppString).join('');
|
|
112
|
+
return `${additional}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function generateStructName(
|
|
116
|
+
componentName: string,
|
|
117
|
+
parts: $ReadOnlyArray<string> = [],
|
|
118
|
+
): string {
|
|
119
|
+
const additional = parts.map(toSafeCppString).join('');
|
|
120
|
+
return `${componentName}${additional}Struct`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function getEnumName(componentName: string, propName: string): string {
|
|
124
|
+
const uppercasedPropName = toSafeCppString(propName);
|
|
125
|
+
return `${componentName}${uppercasedPropName}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getEnumMaskName(enumName: string): string {
|
|
129
|
+
return `${enumName}Mask`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function convertDefaultTypeToString(
|
|
133
|
+
componentName: string,
|
|
134
|
+
prop: NamedShape<PropTypeAnnotation>,
|
|
135
|
+
): string {
|
|
136
|
+
const typeAnnotation = prop.typeAnnotation;
|
|
137
|
+
switch (typeAnnotation.type) {
|
|
138
|
+
case 'BooleanTypeAnnotation':
|
|
139
|
+
if (typeAnnotation.default == null) {
|
|
140
|
+
return '';
|
|
141
|
+
}
|
|
142
|
+
return String(typeAnnotation.default);
|
|
143
|
+
case 'StringTypeAnnotation':
|
|
144
|
+
if (typeAnnotation.default == null) {
|
|
145
|
+
return '';
|
|
146
|
+
}
|
|
147
|
+
return `"${typeAnnotation.default}"`;
|
|
148
|
+
case 'Int32TypeAnnotation':
|
|
149
|
+
return String(typeAnnotation.default);
|
|
150
|
+
case 'DoubleTypeAnnotation':
|
|
151
|
+
const defaultDoubleVal = typeAnnotation.default;
|
|
152
|
+
return parseInt(defaultDoubleVal, 10) === defaultDoubleVal
|
|
153
|
+
? typeAnnotation.default.toFixed(1)
|
|
154
|
+
: String(typeAnnotation.default);
|
|
155
|
+
case 'FloatTypeAnnotation':
|
|
156
|
+
const defaultFloatVal = typeAnnotation.default;
|
|
157
|
+
if (defaultFloatVal == null) {
|
|
158
|
+
return '';
|
|
159
|
+
}
|
|
160
|
+
return parseInt(defaultFloatVal, 10) === defaultFloatVal
|
|
161
|
+
? defaultFloatVal.toFixed(1)
|
|
162
|
+
: String(typeAnnotation.default);
|
|
163
|
+
case 'ReservedPropTypeAnnotation':
|
|
164
|
+
switch (typeAnnotation.name) {
|
|
165
|
+
case 'ColorPrimitive':
|
|
166
|
+
return '';
|
|
167
|
+
case 'ImageSourcePrimitive':
|
|
168
|
+
return '';
|
|
169
|
+
case 'ImageRequestPrimitive':
|
|
170
|
+
return '';
|
|
171
|
+
case 'PointPrimitive':
|
|
172
|
+
return '';
|
|
173
|
+
case 'EdgeInsetsPrimitive':
|
|
174
|
+
return '';
|
|
175
|
+
default:
|
|
176
|
+
(typeAnnotation.name: empty);
|
|
177
|
+
throw new Error(
|
|
178
|
+
`Unsupported type annotation: ${typeAnnotation.name}`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
case 'ArrayTypeAnnotation': {
|
|
182
|
+
const elementType = typeAnnotation.elementType;
|
|
183
|
+
switch (elementType.type) {
|
|
184
|
+
case 'StringEnumTypeAnnotation':
|
|
185
|
+
if (elementType.default == null) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
'A default is required for array StringEnumTypeAnnotation',
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
const enumName = getEnumName(componentName, prop.name);
|
|
191
|
+
const enumMaskName = getEnumMaskName(enumName);
|
|
192
|
+
const defaultValue = `${enumName}::${toSafeCppString(
|
|
193
|
+
elementType.default,
|
|
194
|
+
)}`;
|
|
195
|
+
return `static_cast<${enumMaskName}>(${defaultValue})`;
|
|
196
|
+
default:
|
|
197
|
+
return '';
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
case 'ObjectTypeAnnotation': {
|
|
201
|
+
return '';
|
|
202
|
+
}
|
|
203
|
+
case 'StringEnumTypeAnnotation':
|
|
204
|
+
return `${getEnumName(componentName, prop.name)}::${toSafeCppString(
|
|
205
|
+
typeAnnotation.default,
|
|
206
|
+
)}`;
|
|
207
|
+
case 'Int32EnumTypeAnnotation':
|
|
208
|
+
return `${getEnumName(componentName, prop.name)}::${toIntEnumValueName(
|
|
209
|
+
prop.name,
|
|
210
|
+
typeAnnotation.default,
|
|
211
|
+
)}`;
|
|
212
|
+
default:
|
|
213
|
+
(typeAnnotation: empty);
|
|
214
|
+
throw new Error(`Unsupported type annotation: ${typeAnnotation.type}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
module.exports = {
|
|
219
|
+
convertDefaultTypeToString,
|
|
220
|
+
getCppTypeForAnnotation,
|
|
221
|
+
getEnumName,
|
|
222
|
+
getEnumMaskName,
|
|
223
|
+
getImports,
|
|
224
|
+
toSafeCppString,
|
|
225
|
+
toIntEnumValueName,
|
|
226
|
+
generateStructName,
|
|
227
|
+
generateEventStructName,
|
|
228
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
// File path -> contents
|
|
14
|
+
|
|
15
|
+
const FileTemplate = ({componentDescriptors, libraryName}) => `
|
|
16
|
+
/**
|
|
17
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
18
|
+
*
|
|
19
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
20
|
+
* once the code is regenerated.
|
|
21
|
+
*
|
|
22
|
+
* ${'@'}generated by codegen project: GenerateComponentDescriptorH.js
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
#pragma once
|
|
26
|
+
|
|
27
|
+
#include <react/renderer/components/${libraryName}/ShadowNodes.h>
|
|
28
|
+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
|
|
29
|
+
|
|
30
|
+
namespace facebook {
|
|
31
|
+
namespace react {
|
|
32
|
+
|
|
33
|
+
${componentDescriptors}
|
|
34
|
+
|
|
35
|
+
} // namespace react
|
|
36
|
+
} // namespace facebook
|
|
37
|
+
`;
|
|
38
|
+
const ComponentTemplate = ({className}) =>
|
|
39
|
+
`
|
|
40
|
+
using ${className}ComponentDescriptor = ConcreteComponentDescriptor<${className}ShadowNode>;
|
|
41
|
+
`.trim();
|
|
42
|
+
module.exports = {
|
|
43
|
+
generate(libraryName, schema, packageName, assumeNonnull = false) {
|
|
44
|
+
const fileName = 'ComponentDescriptors.h';
|
|
45
|
+
const componentDescriptors = Object.keys(schema.modules)
|
|
46
|
+
.map(moduleName => {
|
|
47
|
+
const module = schema.modules[moduleName];
|
|
48
|
+
if (module.type !== 'Component') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const components = module.components;
|
|
52
|
+
// No components in this module
|
|
53
|
+
if (components == null) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return Object.keys(components)
|
|
57
|
+
.map(componentName => {
|
|
58
|
+
if (components[componentName].interfaceOnly === true) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
return ComponentTemplate({
|
|
62
|
+
className: componentName,
|
|
63
|
+
});
|
|
64
|
+
})
|
|
65
|
+
.join('\n');
|
|
66
|
+
})
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.join('\n');
|
|
69
|
+
const replacedTemplate = FileTemplate({
|
|
70
|
+
componentDescriptors,
|
|
71
|
+
libraryName,
|
|
72
|
+
});
|
|
73
|
+
return new Map([[fileName, replacedTemplate]]);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
import type {SchemaType} from '../../CodegenSchema';
|
|
14
|
+
|
|
15
|
+
// File path -> contents
|
|
16
|
+
type FilesOutput = Map<string, string>;
|
|
17
|
+
|
|
18
|
+
const FileTemplate = ({
|
|
19
|
+
componentDescriptors,
|
|
20
|
+
libraryName,
|
|
21
|
+
}: {
|
|
22
|
+
componentDescriptors: string,
|
|
23
|
+
libraryName: string,
|
|
24
|
+
}) => `
|
|
25
|
+
/**
|
|
26
|
+
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
|
|
27
|
+
*
|
|
28
|
+
* Do not edit this file as changes may cause incorrect behavior and will be lost
|
|
29
|
+
* once the code is regenerated.
|
|
30
|
+
*
|
|
31
|
+
* ${'@'}generated by codegen project: GenerateComponentDescriptorH.js
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
#pragma once
|
|
35
|
+
|
|
36
|
+
#include <react/renderer/components/${libraryName}/ShadowNodes.h>
|
|
37
|
+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
|
|
38
|
+
|
|
39
|
+
namespace facebook {
|
|
40
|
+
namespace react {
|
|
41
|
+
|
|
42
|
+
${componentDescriptors}
|
|
43
|
+
|
|
44
|
+
} // namespace react
|
|
45
|
+
} // namespace facebook
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
const ComponentTemplate = ({className}: {className: string}) =>
|
|
49
|
+
`
|
|
50
|
+
using ${className}ComponentDescriptor = ConcreteComponentDescriptor<${className}ShadowNode>;
|
|
51
|
+
`.trim();
|
|
52
|
+
|
|
53
|
+
module.exports = {
|
|
54
|
+
generate(
|
|
55
|
+
libraryName: string,
|
|
56
|
+
schema: SchemaType,
|
|
57
|
+
packageName?: string,
|
|
58
|
+
assumeNonnull: boolean = false,
|
|
59
|
+
): FilesOutput {
|
|
60
|
+
const fileName = 'ComponentDescriptors.h';
|
|
61
|
+
|
|
62
|
+
const componentDescriptors = Object.keys(schema.modules)
|
|
63
|
+
.map(moduleName => {
|
|
64
|
+
const module = schema.modules[moduleName];
|
|
65
|
+
if (module.type !== 'Component') {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const {components} = module;
|
|
70
|
+
// No components in this module
|
|
71
|
+
if (components == null) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return Object.keys(components)
|
|
76
|
+
.map(componentName => {
|
|
77
|
+
if (components[componentName].interfaceOnly === true) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return ComponentTemplate({className: componentName});
|
|
82
|
+
})
|
|
83
|
+
.join('\n');
|
|
84
|
+
})
|
|
85
|
+
.filter(Boolean)
|
|
86
|
+
.join('\n');
|
|
87
|
+
|
|
88
|
+
const replacedTemplate = FileTemplate({
|
|
89
|
+
componentDescriptors,
|
|
90
|
+
libraryName,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return new Map([[fileName, replacedTemplate]]);
|
|
94
|
+
},
|
|
95
|
+
};
|