@react-native-windows/codegen 0.0.0-canary.100 → 0.0.0-canary.102
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/CHANGELOG.md +20 -4
- package/lib-commonjs/Cli.js +11 -0
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/AliasGen.js +1 -2
- package/lib-commonjs/generators/AliasGen.js.map +1 -1
- package/lib-commonjs/generators/AliasManaging.d.ts +3 -3
- package/lib-commonjs/generators/AliasManaging.js.map +1 -1
- package/lib-commonjs/generators/GenerateComponentWindows.d.ts +13 -0
- package/lib-commonjs/generators/GenerateComponentWindows.js +337 -0
- package/lib-commonjs/generators/GenerateComponentWindows.js.map +1 -0
- package/lib-commonjs/generators/GenerateTypeScript.js +1 -1
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -1
- package/lib-commonjs/generators/ObjectTypes.d.ts +2 -2
- package/lib-commonjs/generators/ObjectTypes.js +3 -1
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -1
- package/lib-commonjs/generators/ParamTypes.js +3 -1
- package/lib-commonjs/generators/ParamTypes.js.map +1 -1
- package/lib-commonjs/generators/PropObjectTypes.d.ts +18 -0
- package/lib-commonjs/generators/PropObjectTypes.js +217 -0
- package/lib-commonjs/generators/PropObjectTypes.js.map +1 -0
- package/lib-commonjs/index.d.ts +3 -1
- package/lib-commonjs/index.js +16 -13
- package/lib-commonjs/index.js.map +1 -1
- package/package.json +1 -1
- package/src/Cli.ts +11 -0
- package/src/generators/AliasGen.ts +1 -1
- package/src/generators/AliasManaging.ts +12 -12
- package/src/generators/GenerateComponentWindows.ts +393 -0
- package/src/generators/GenerateTypeScript.ts +3 -2
- package/src/generators/ObjectTypes.ts +5 -2
- package/src/generators/ParamTypes.ts +6 -3
- package/src/generators/PropObjectTypes.ts +233 -0
- package/src/index.ts +25 -2
|
@@ -16,6 +16,7 @@ import type {
|
|
|
16
16
|
NativeModuleFunctionTypeAnnotation,
|
|
17
17
|
NativeModuleParamTypeAnnotation,
|
|
18
18
|
NativeModuleUnionTypeAnnotation,
|
|
19
|
+
UnsafeAnyTypeAnnotation,
|
|
19
20
|
Nullable,
|
|
20
21
|
} from '@react-native/codegen/lib/CodegenSchema';
|
|
21
22
|
import {
|
|
@@ -116,7 +117,7 @@ function translateArray(
|
|
|
116
117
|
target: ParamTarget,
|
|
117
118
|
options: CppCodegenOptions,
|
|
118
119
|
): string {
|
|
119
|
-
if (param.elementType) {
|
|
120
|
+
if (param.elementType.type !== 'AnyTypeAnnotation') {
|
|
120
121
|
switch (target) {
|
|
121
122
|
case 'spec':
|
|
122
123
|
case 'template':
|
|
@@ -177,7 +178,7 @@ function translateEventEmitterArray(
|
|
|
177
178
|
}
|
|
178
179
|
|
|
179
180
|
function translateParam(
|
|
180
|
-
param: NativeModuleParamTypeAnnotation,
|
|
181
|
+
param: NativeModuleParamTypeAnnotation | UnsafeAnyTypeAnnotation,
|
|
181
182
|
aliases: AliasMap,
|
|
182
183
|
baseAliasName: string,
|
|
183
184
|
target: ParamTarget,
|
|
@@ -223,6 +224,8 @@ function translateParam(
|
|
|
223
224
|
case 'EnumDeclaration':
|
|
224
225
|
case 'UnionTypeAnnotation':
|
|
225
226
|
return translateUnionReturnType(param, target, options);
|
|
227
|
+
case 'AnyTypeAnnotation':
|
|
228
|
+
return decorateType('::React::JSValue', target);
|
|
226
229
|
default:
|
|
227
230
|
throw new Error(`Unhandled type in translateParam: ${paramType}`);
|
|
228
231
|
}
|
|
@@ -262,7 +265,7 @@ function translateEventEmitterParam(
|
|
|
262
265
|
}
|
|
263
266
|
|
|
264
267
|
function translateNullableParamType(
|
|
265
|
-
paramType: Nullable<NativeModuleParamTypeAnnotation
|
|
268
|
+
paramType: Nullable<NativeModuleParamTypeAnnotation> | UnsafeAnyTypeAnnotation,
|
|
266
269
|
aliases: AliasMap,
|
|
267
270
|
baseAliasName: string,
|
|
268
271
|
nullableTarget: ParamTarget,
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import type { PropTypeAnnotation, ObjectTypeAnnotation, EventTypeAnnotation, CommandParamTypeAnnotation } from '@react-native/codegen/lib/CodegenSchema';
|
|
2
|
+
import type { CppCodegenOptions } from './ObjectTypes';
|
|
3
|
+
import {
|
|
4
|
+
AliasMap,
|
|
5
|
+
getAnonymousAliasCppName,
|
|
6
|
+
} from './AliasManaging';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line complexity
|
|
10
|
+
export function translateComponentPropsFieldType(type: PropTypeAnnotation,
|
|
11
|
+
aliases: AliasMap<ObjectTypeAnnotation<PropTypeAnnotation>>,
|
|
12
|
+
baseAliasName: string,
|
|
13
|
+
options: CppCodegenOptions): { type: string, initializer: string, alreadySupportsOptionalOrHasDefault?: boolean } {
|
|
14
|
+
switch (type.type) {
|
|
15
|
+
case 'StringTypeAnnotation':
|
|
16
|
+
return { type: options.cppStringType, initializer: type.default ? `{${type.default}}` : '', alreadySupportsOptionalOrHasDefault: !!type.default };
|
|
17
|
+
case 'FloatTypeAnnotation':
|
|
18
|
+
return { type: 'float', initializer: type.default ? `{${type.default}}` : '{}', alreadySupportsOptionalOrHasDefault: !!type.default };
|
|
19
|
+
case 'DoubleTypeAnnotation':
|
|
20
|
+
return { type: 'double', initializer: type.default ? `{${type.default}}` : '{}', alreadySupportsOptionalOrHasDefault: !!type.default };
|
|
21
|
+
case 'Int32TypeAnnotation':
|
|
22
|
+
return { type: 'int32_t', initializer: type.default ? `{${type.default}}` : '{}', alreadySupportsOptionalOrHasDefault: !!type.default };
|
|
23
|
+
case 'BooleanTypeAnnotation':
|
|
24
|
+
return { type: 'bool', initializer: type.default ? `{${type.default}}` : '{}', alreadySupportsOptionalOrHasDefault: !!type.default };
|
|
25
|
+
case 'ArrayTypeAnnotation':
|
|
26
|
+
|
|
27
|
+
let arrayTemplateArg = '';
|
|
28
|
+
switch (type.elementType.type) {
|
|
29
|
+
case 'BooleanTypeAnnotation':
|
|
30
|
+
arrayTemplateArg = 'bool';
|
|
31
|
+
break;
|
|
32
|
+
case 'DoubleTypeAnnotation':
|
|
33
|
+
arrayTemplateArg = 'double';
|
|
34
|
+
break;
|
|
35
|
+
case 'FloatTypeAnnotation':
|
|
36
|
+
arrayTemplateArg = 'float';
|
|
37
|
+
break;
|
|
38
|
+
case 'Int32TypeAnnotation':
|
|
39
|
+
arrayTemplateArg = 'int32_t';
|
|
40
|
+
break;
|
|
41
|
+
case 'StringTypeAnnotation':
|
|
42
|
+
arrayTemplateArg = options.cppStringType;
|
|
43
|
+
break;
|
|
44
|
+
case 'ArrayTypeAnnotation':
|
|
45
|
+
const innerType = translateComponentPropsFieldType(type.elementType, aliases, baseAliasName, options);
|
|
46
|
+
arrayTemplateArg = `std::vector<${innerType.type}>`;
|
|
47
|
+
break;
|
|
48
|
+
case 'ObjectTypeAnnotation':
|
|
49
|
+
arrayTemplateArg = translateComponentPropsFieldType(type.elementType, aliases, baseAliasName, options).type;
|
|
50
|
+
break;
|
|
51
|
+
case 'ReservedPropTypeAnnotation':
|
|
52
|
+
switch (type.elementType.name) {
|
|
53
|
+
case 'ColorPrimitive':
|
|
54
|
+
arrayTemplateArg = 'winrt::Microsoft::ReactNative::Color';
|
|
55
|
+
break;
|
|
56
|
+
case 'DimensionPrimitive':
|
|
57
|
+
case 'EdgeInsetsPrimitive':
|
|
58
|
+
case 'ImageRequestPrimitive':
|
|
59
|
+
case 'ImageSourcePrimitive':
|
|
60
|
+
case 'PointPrimitive':
|
|
61
|
+
arrayTemplateArg = 'winrt::Microsoft::ReactNative::JSValue'; // TODO - better handling for these types than JSValue
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
throw new Error(`Unhandled ReservedPropTypeAnnotation type: ${type.elementType.name}`);
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case 'StringEnumTypeAnnotation':
|
|
68
|
+
arrayTemplateArg = options.cppStringType; // TODO - better enum type handling than just passing a string
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
throw new Error(`Unhandled type: ${type.type}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { type: `std::vector<${arrayTemplateArg}>`, initializer: '' };
|
|
75
|
+
case 'ReservedPropTypeAnnotation':
|
|
76
|
+
switch (type.name) {
|
|
77
|
+
case 'ColorPrimitive':
|
|
78
|
+
return { type: 'winrt::Microsoft::ReactNative::Color', initializer: '{nullptr}', alreadySupportsOptionalOrHasDefault: true };
|
|
79
|
+
case 'DimensionPrimitive':
|
|
80
|
+
case 'EdgeInsetsPrimitive':
|
|
81
|
+
case 'ImageRequestPrimitive':
|
|
82
|
+
case 'ImageSourcePrimitive':
|
|
83
|
+
case 'PointPrimitive':
|
|
84
|
+
return { type: 'winrt::Microsoft::ReactNative::JSValue', initializer: '{nullptr}', alreadySupportsOptionalOrHasDefault: true }; // TODO - better handling for these types than JSValue
|
|
85
|
+
default:
|
|
86
|
+
throw new Error(`Unhandled ReservedPropTypeAnnotation type: ${type.name}`);
|
|
87
|
+
}
|
|
88
|
+
case 'ObjectTypeAnnotation': {
|
|
89
|
+
return { type: getAnonymousAliasCppName<ObjectTypeAnnotation<PropTypeAnnotation>>(aliases, baseAliasName, type), initializer: '' };
|
|
90
|
+
}
|
|
91
|
+
case 'MixedTypeAnnotation':
|
|
92
|
+
return { type: 'winrt::Microsoft::ReactNative::JSValue', initializer: '{nullptr}', alreadySupportsOptionalOrHasDefault: true };
|
|
93
|
+
case 'Int32EnumTypeAnnotation':
|
|
94
|
+
return { type: 'int32_t', initializer: '' }; // TODO - better enum type handling than just passing a string
|
|
95
|
+
case 'StringEnumTypeAnnotation':
|
|
96
|
+
return { type: options.cppStringType, initializer: '' }; // TODO - better enum type handling than just passing an int
|
|
97
|
+
default:
|
|
98
|
+
throw new Error(`Unhandled type: ${(type as any).type}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function translateComponentEventType(type: EventTypeAnnotation,
|
|
103
|
+
aliases: AliasMap<ObjectTypeAnnotation<EventTypeAnnotation>>,
|
|
104
|
+
baseAliasName: string,
|
|
105
|
+
options: CppCodegenOptions): { type: string, initializer: string, alreadySupportsOptionalOrHasDefault?: boolean } {
|
|
106
|
+
switch (type.type) {
|
|
107
|
+
case 'StringTypeAnnotation':
|
|
108
|
+
return { type: options.cppStringType, initializer: '' };
|
|
109
|
+
case 'FloatTypeAnnotation':
|
|
110
|
+
return { type: 'float', initializer: '{}' };
|
|
111
|
+
case 'DoubleTypeAnnotation':
|
|
112
|
+
return { type: 'double', initializer: '{}' };
|
|
113
|
+
case 'Int32TypeAnnotation':
|
|
114
|
+
return { type: 'int32_t', initializer: '{}' };
|
|
115
|
+
case 'BooleanTypeAnnotation':
|
|
116
|
+
return { type: 'bool', initializer: '{}' };
|
|
117
|
+
case 'ArrayTypeAnnotation':
|
|
118
|
+
{
|
|
119
|
+
let arrayTemplateArg = '';
|
|
120
|
+
switch (type.elementType.type) {
|
|
121
|
+
case 'BooleanTypeAnnotation':
|
|
122
|
+
arrayTemplateArg = 'bool';
|
|
123
|
+
break;
|
|
124
|
+
case 'DoubleTypeAnnotation':
|
|
125
|
+
arrayTemplateArg = 'double';
|
|
126
|
+
break;
|
|
127
|
+
case 'FloatTypeAnnotation':
|
|
128
|
+
arrayTemplateArg = 'float';
|
|
129
|
+
break;
|
|
130
|
+
case 'Int32TypeAnnotation':
|
|
131
|
+
arrayTemplateArg = 'int32_t';
|
|
132
|
+
break;
|
|
133
|
+
case 'StringTypeAnnotation':
|
|
134
|
+
arrayTemplateArg = options.cppStringType;
|
|
135
|
+
break;
|
|
136
|
+
case 'ArrayTypeAnnotation':
|
|
137
|
+
const innerType = translateComponentEventType(type.elementType, aliases, baseAliasName, options);
|
|
138
|
+
arrayTemplateArg = `std::vector<${innerType.type}>`;
|
|
139
|
+
break;
|
|
140
|
+
case 'MixedTypeAnnotation':
|
|
141
|
+
arrayTemplateArg = 'winrt::Microsoft::ReactNative::JSValue';
|
|
142
|
+
break;
|
|
143
|
+
case 'ObjectTypeAnnotation':
|
|
144
|
+
arrayTemplateArg = translateComponentEventType(type.elementType, aliases, baseAliasName, options).type;
|
|
145
|
+
break;
|
|
146
|
+
case 'StringEnumTypeAnnotation':
|
|
147
|
+
arrayTemplateArg = options.cppStringType; // TODO - better enum type handling than just passing a string
|
|
148
|
+
break;
|
|
149
|
+
default:
|
|
150
|
+
throw new Error(`Unhandled type: ${type.type}`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return { type: `std::vector<${arrayTemplateArg}>`, initializer: '{}' };
|
|
154
|
+
}
|
|
155
|
+
case 'ObjectTypeAnnotation': {
|
|
156
|
+
return { type: getAnonymousAliasCppName<ObjectTypeAnnotation<EventTypeAnnotation>>(aliases, baseAliasName, type), initializer: '' };
|
|
157
|
+
}
|
|
158
|
+
case 'MixedTypeAnnotation': {
|
|
159
|
+
return { type: 'winrt::Microsoft::ReactNative::JSValue', initializer: '{nullptr}', alreadySupportsOptionalOrHasDefault: true };
|
|
160
|
+
}
|
|
161
|
+
case 'StringEnumTypeAnnotation':
|
|
162
|
+
return { type: options.cppStringType, initializer: '' }; // TODO - better enum type handling than just passing a string
|
|
163
|
+
default:
|
|
164
|
+
throw new Error(`Unhandled type: ${(type as any).type}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
export function translateCommandParamType(type: CommandParamTypeAnnotation,
|
|
170
|
+
aliases: AliasMap<ObjectTypeAnnotation<CommandParamTypeAnnotation>>,
|
|
171
|
+
baseAliasName: string,
|
|
172
|
+
options: CppCodegenOptions): { type: string, initializer: string, alreadySupportsOptionalOrHasDefault?: boolean } {
|
|
173
|
+
switch (type.type) {
|
|
174
|
+
case 'StringTypeAnnotation':
|
|
175
|
+
return { type: options.cppStringType, initializer: '' };
|
|
176
|
+
case 'FloatTypeAnnotation':
|
|
177
|
+
return { type: 'float', initializer: '{}' };
|
|
178
|
+
case 'DoubleTypeAnnotation':
|
|
179
|
+
return { type: 'double', initializer: '{}' };
|
|
180
|
+
case 'Int32TypeAnnotation':
|
|
181
|
+
return { type: 'int32_t', initializer: '{}' };
|
|
182
|
+
case 'BooleanTypeAnnotation':
|
|
183
|
+
return { type: 'bool', initializer: '{}' };
|
|
184
|
+
case 'ArrayTypeAnnotation':
|
|
185
|
+
{
|
|
186
|
+
let arrayTemplateArg = '';
|
|
187
|
+
switch (type.elementType.type) {
|
|
188
|
+
case 'BooleanTypeAnnotation':
|
|
189
|
+
arrayTemplateArg = 'bool';
|
|
190
|
+
break;
|
|
191
|
+
case 'DoubleTypeAnnotation':
|
|
192
|
+
arrayTemplateArg = 'double';
|
|
193
|
+
break;
|
|
194
|
+
case 'FloatTypeAnnotation':
|
|
195
|
+
arrayTemplateArg = 'float';
|
|
196
|
+
break;
|
|
197
|
+
case 'Int32TypeAnnotation':
|
|
198
|
+
arrayTemplateArg = 'int32_t';
|
|
199
|
+
break;
|
|
200
|
+
case 'StringTypeAnnotation':
|
|
201
|
+
arrayTemplateArg = options.cppStringType;
|
|
202
|
+
break;
|
|
203
|
+
case 'ArrayTypeAnnotation':
|
|
204
|
+
const innerType = translateCommandParamType(type.elementType, aliases, baseAliasName, options);
|
|
205
|
+
arrayTemplateArg = `std::vector<${innerType.type}>`;
|
|
206
|
+
break;
|
|
207
|
+
case 'ReservedPropTypeAnnotation':
|
|
208
|
+
arrayTemplateArg = 'winrt::Microsoft::ReactNative::JSValue';
|
|
209
|
+
break;
|
|
210
|
+
case 'ObjectTypeAnnotation':
|
|
211
|
+
arrayTemplateArg = 'winrt::Microsoft::ReactNative::JSValueObject'; // TODO - better typing
|
|
212
|
+
break;
|
|
213
|
+
case 'StringEnumTypeAnnotation':
|
|
214
|
+
arrayTemplateArg = options.cppStringType; // TODO - better enum type handling than just passing a string
|
|
215
|
+
break;
|
|
216
|
+
case 'GenericTypeAnnotation' as any: // TODO verify schema - Getting this type when running codegen on all the built in types
|
|
217
|
+
arrayTemplateArg = 'winrt::Microsoft::ReactNative::JSValue';
|
|
218
|
+
break;
|
|
219
|
+
default:
|
|
220
|
+
throw new Error(`Unhandled type: ${(type.elementType as any).type} - ${JSON.stringify(type.elementType, null, 2)}`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return { type: `std::vector<${arrayTemplateArg}>`, initializer: '{}' };
|
|
224
|
+
}
|
|
225
|
+
case 'ReservedTypeAnnotation':
|
|
226
|
+
if ((type.name as any) !== 'RootTag') {
|
|
227
|
+
throw new Error(`Unhandled ReservedTypeAnnotation: ${type.name}`)
|
|
228
|
+
}
|
|
229
|
+
return { type: 'bool', initializer: '{-1}' };
|
|
230
|
+
default:
|
|
231
|
+
throw new Error(`Unhandled type: ${(type as any).type}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ import fs from '@react-native-windows/fs';
|
|
|
10
10
|
import globby from 'globby';
|
|
11
11
|
import type {CppStringTypes} from './generators/GenerateNM2';
|
|
12
12
|
import {createNM2Generator} from './generators/GenerateNM2';
|
|
13
|
+
import {createComponentGenerator} from './generators/GenerateComponentWindows';
|
|
13
14
|
import {
|
|
14
15
|
generateTypeScript,
|
|
15
16
|
setOptionalTurboModule,
|
|
@@ -49,6 +50,8 @@ export interface SharedOptions {
|
|
|
49
50
|
modulesCxx: boolean;
|
|
50
51
|
modulesTypeScriptTypes: boolean;
|
|
51
52
|
modulesWindows: boolean;
|
|
53
|
+
componentsWindows: boolean,
|
|
54
|
+
internalComponents: boolean,
|
|
52
55
|
namespace: string;
|
|
53
56
|
outputDirectory: string;
|
|
54
57
|
cppStringType: CppStringTypes;
|
|
@@ -214,6 +217,8 @@ export function generate(
|
|
|
214
217
|
modulesCxx,
|
|
215
218
|
modulesTypeScriptTypes,
|
|
216
219
|
modulesWindows,
|
|
220
|
+
internalComponents,
|
|
221
|
+
componentsWindows,
|
|
217
222
|
namespace,
|
|
218
223
|
outputDirectory,
|
|
219
224
|
cppStringType,
|
|
@@ -320,6 +325,10 @@ export function generate(
|
|
|
320
325
|
)
|
|
321
326
|
) {
|
|
322
327
|
const componentGenerators = [
|
|
328
|
+
];
|
|
329
|
+
|
|
330
|
+
if (internalComponents) {
|
|
331
|
+
componentGenerators.push(
|
|
323
332
|
generatorComponentDescriptorH,
|
|
324
333
|
generatorEventEmitterCPP,
|
|
325
334
|
generatorEventEmitterH,
|
|
@@ -328,8 +337,18 @@ export function generate(
|
|
|
328
337
|
generatorShadowNodeCPP,
|
|
329
338
|
generatorShadowNodeH,
|
|
330
339
|
generatorStateCPP,
|
|
331
|
-
generatorStateH
|
|
332
|
-
|
|
340
|
+
generatorStateH
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (componentsWindows) {
|
|
345
|
+
const generateComponentWindows = createComponentGenerator({
|
|
346
|
+
namespace,
|
|
347
|
+
cppStringType,
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
componentGenerators.push(generateComponentWindows);
|
|
351
|
+
}
|
|
333
352
|
|
|
334
353
|
componentGenerators.forEach(generator => {
|
|
335
354
|
const generated: Map<string, string> = generator(
|
|
@@ -369,6 +388,8 @@ export function runCodeGen(options: CodeGenOptions): boolean {
|
|
|
369
388
|
modulesCxx,
|
|
370
389
|
modulesTypeScriptTypes,
|
|
371
390
|
modulesWindows,
|
|
391
|
+
componentsWindows,
|
|
392
|
+
internalComponents,
|
|
372
393
|
namespace,
|
|
373
394
|
outputDirectory,
|
|
374
395
|
cppStringType,
|
|
@@ -381,6 +402,8 @@ export function runCodeGen(options: CodeGenOptions): boolean {
|
|
|
381
402
|
modulesCxx,
|
|
382
403
|
modulesTypeScriptTypes,
|
|
383
404
|
modulesWindows,
|
|
405
|
+
componentsWindows,
|
|
406
|
+
internalComponents,
|
|
384
407
|
namespace,
|
|
385
408
|
outputDirectory,
|
|
386
409
|
cppStringType,
|