@react-native-windows/codegen 0.0.0-canary.12 → 0.0.0-canary.120

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +1029 -12
  2. package/README.md +1 -1
  3. package/bin.js +0 -0
  4. package/lib-commonjs/Cli.d.ts +7 -0
  5. package/lib-commonjs/Cli.js +103 -0
  6. package/lib-commonjs/Cli.js.map +1 -0
  7. package/lib-commonjs/generators/AliasGen.d.ts +12 -0
  8. package/lib-commonjs/generators/AliasGen.js +115 -0
  9. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  10. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  11. package/lib-commonjs/generators/AliasManaging.js +49 -0
  12. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  13. package/lib-commonjs/generators/GenerateComponentWindows.d.ts +13 -0
  14. package/lib-commonjs/generators/GenerateComponentWindows.js +429 -0
  15. package/lib-commonjs/generators/GenerateComponentWindows.js.map +1 -0
  16. package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
  17. package/lib-commonjs/generators/GenerateNM2.js +145 -0
  18. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  19. package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
  20. package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
  21. package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
  22. package/lib-commonjs/generators/ObjectTypes.d.ts +13 -0
  23. package/lib-commonjs/generators/ObjectTypes.js +81 -0
  24. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  25. package/lib-commonjs/generators/ParamTypes.d.ts +13 -0
  26. package/lib-commonjs/generators/ParamTypes.js +183 -0
  27. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  28. package/lib-commonjs/generators/PropObjectTypes.d.ts +18 -0
  29. package/lib-commonjs/generators/PropObjectTypes.js +208 -0
  30. package/lib-commonjs/generators/PropObjectTypes.js.map +1 -0
  31. package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
  32. package/lib-commonjs/generators/ReturnTypes.js +29 -0
  33. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  34. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  35. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  36. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  37. package/lib-commonjs/generators/ValidateMethods.d.ts +14 -0
  38. package/lib-commonjs/generators/ValidateMethods.js +112 -0
  39. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  40. package/lib-commonjs/index.d.ts +39 -0
  41. package/lib-commonjs/index.js +227 -0
  42. package/lib-commonjs/index.js.map +1 -0
  43. package/package.json +38 -21
  44. package/src/Cli.ts +69 -232
  45. package/src/generators/AliasGen.ts +195 -0
  46. package/src/generators/AliasManaging.ts +75 -0
  47. package/src/generators/GenerateComponentWindows.ts +563 -0
  48. package/src/generators/GenerateNM2.ts +128 -131
  49. package/src/generators/GenerateTypeScript.ts +250 -0
  50. package/src/generators/ObjectTypes.ts +95 -37
  51. package/src/generators/ParamTypes.ts +311 -64
  52. package/src/generators/PropObjectTypes.ts +223 -0
  53. package/src/generators/ReturnTypes.ts +38 -41
  54. package/src/generators/ValidateConstants.ts +50 -0
  55. package/src/generators/ValidateMethods.ts +270 -0
  56. package/src/index.ts +415 -0
  57. package/.eslintrc.js +0 -4
  58. package/.vscode/launch.json +0 -23
  59. package/CHANGELOG.json +0 -705
  60. package/jest.config.js +0 -1
  61. package/tsconfig.json +0 -5
@@ -0,0 +1,563 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {
10
+ SchemaType,
11
+ EventTypeAnnotation,
12
+ PropTypeAnnotation,
13
+ ObjectTypeAnnotation,
14
+ CommandParamTypeAnnotation,
15
+ } from '@react-native/codegen/lib/CodegenSchema';
16
+ import {getAliasCppName, setPreferredModuleName} from './AliasManaging';
17
+ import {
18
+ translateComponentPropsFieldType,
19
+ translateComponentEventType,
20
+ translateCommandParamType,
21
+ } from './PropObjectTypes';
22
+ import type {CppStringTypes} from './ObjectTypes';
23
+ import type {AliasMap} from './AliasManaging';
24
+
25
+ export type {CppStringTypes} from './ObjectTypes';
26
+
27
+ type FilesOutput = Map<string, string>;
28
+
29
+ const headerTemplate = `/*
30
+ * This file is auto-generated from ::_COMPONENT_NAME_::NativeComponent spec file in flow / TypeScript.
31
+ */
32
+ // clang-format off
33
+ #pragma once
34
+
35
+ #include <NativeModules.h>
36
+
37
+ #ifdef RNW_NEW_ARCH
38
+ #include <JSValueComposition.h>
39
+
40
+ #include <winrt/Microsoft.ReactNative.Composition.h>
41
+ #include <winrt/Microsoft.UI.Composition.h>
42
+ #endif // #ifdef RNW_NEW_ARCH`;
43
+
44
+ const propsTemplate = `REACT_STRUCT(::_PROPS_NAME_::)
45
+ struct ::_PROPS_NAME_:: : winrt::implements<::_PROPS_NAME_::, winrt::Microsoft::ReactNative::IComponentProps> {
46
+ ::_PROPS_NAME_::(winrt::Microsoft::ReactNative::ViewProps props, const winrt::Microsoft::ReactNative::IComponentProps& cloneFrom)
47
+ : ViewProps(props)
48
+ {
49
+ if (cloneFrom) {
50
+ auto cloneFromProps = cloneFrom.as<::_PROPS_NAME_::>();
51
+ ::_PROP_INITIALIZERS_::
52
+ }
53
+ }
54
+
55
+ void SetProp(uint32_t hash, winrt::hstring propName, winrt::Microsoft::ReactNative::IJSValueReader value) noexcept {
56
+ winrt::Microsoft::ReactNative::ReadProp(hash, propName, value, *this);
57
+ }
58
+
59
+ ::_PROPS_FIELDS_::
60
+ const winrt::Microsoft::ReactNative::ViewProps ViewProps;
61
+ };`;
62
+
63
+ const propsObjectTemplate = `REACT_STRUCT(::_OBJECT_NAME_::)
64
+ struct ::_OBJECT_NAME_:: {
65
+ ::_OBJECT_FIELDS_::};
66
+ `;
67
+ const eventsObjectTemplate = `REACT_STRUCT(::_OBJECT_NAME_::)
68
+ struct ::_OBJECT_NAME_:: {
69
+ ::_OBJECT_FIELDS_::};
70
+ `;
71
+
72
+ const eventEmitterMethodTemplate = ` void ::_EVENT_NAME_::(::_EVENT_OBJECT_TYPE_:: &value) const {
73
+ m_eventEmitter.DispatchEvent(L"::_EVENT_NAME_NO_ON_::", [value](const winrt::Microsoft::ReactNative::IJSValueWriter writer) {
74
+ winrt::Microsoft::ReactNative::WriteValue(writer, value);
75
+ });
76
+ }`;
77
+
78
+ const eventEmitterTemplate = `::_COMPONENT_EVENT_OBJECT_TYPES_::
79
+
80
+ struct ::_EVENT_EMITTER_NAME_:: {
81
+ ::_EVENT_EMITTER_NAME_::(const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter)
82
+ : m_eventEmitter(eventEmitter) {}
83
+
84
+ ::_EVENT_EMITTER_USINGS_::
85
+
86
+ ::_EVENT_EMITTER_METHODS_::
87
+
88
+ private:
89
+ winrt::Microsoft::ReactNative::EventEmitter m_eventEmitter{nullptr};
90
+ };`;
91
+
92
+ const baseStructTemplate = `
93
+ template<typename TUserData>
94
+ struct Base::_COMPONENT_NAME_:: {
95
+
96
+ virtual void UpdateProps(
97
+ const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
98
+ const winrt::com_ptr<::_COMPONENT_NAME_::Props> &newProps,
99
+ const winrt::com_ptr<::_COMPONENT_NAME_::Props> &/*oldProps*/) noexcept {
100
+ m_props = newProps;
101
+ }
102
+
103
+ // UpdateLayoutMetrics will only be called if this method is overridden
104
+ virtual void UpdateLayoutMetrics(
105
+ const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
106
+ const winrt::Microsoft::ReactNative::LayoutMetrics &/*newLayoutMetrics*/,
107
+ const winrt::Microsoft::ReactNative::LayoutMetrics &/*oldLayoutMetrics*/) noexcept {
108
+ }
109
+
110
+ // UpdateState will only be called if this method is overridden
111
+ virtual void UpdateState(
112
+ const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
113
+ const winrt::Microsoft::ReactNative::IComponentState &/*newState*/) noexcept {
114
+ }
115
+
116
+ virtual void UpdateEventEmitter(const std::shared_ptr<::_COMPONENT_NAME_::EventEmitter> &eventEmitter) noexcept {
117
+ m_eventEmitter = eventEmitter;
118
+ }
119
+
120
+ // MountChildComponentView will only be called if this method is overridden
121
+ virtual void MountChildComponentView(const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
122
+ const winrt::Microsoft::ReactNative::MountChildComponentViewArgs &/*args*/) noexcept {
123
+ }
124
+
125
+ // UnmountChildComponentView will only be called if this method is overridden
126
+ virtual void UnmountChildComponentView(const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
127
+ const winrt::Microsoft::ReactNative::UnmountChildComponentViewArgs &/*args*/) noexcept {
128
+ }
129
+
130
+ // Initialize will only be called if this method is overridden
131
+ virtual void Initialize(const winrt::Microsoft::ReactNative::ComponentView &/*view*/) noexcept {
132
+ }
133
+
134
+ // CreateVisual will only be called if this method is overridden
135
+ virtual winrt::Microsoft::UI::Composition::Visual CreateVisual(const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
136
+ return view.as<winrt::Microsoft::ReactNative::Composition::ComponentView>().Compositor().CreateSpriteVisual();
137
+ }
138
+
139
+ // FinalizeUpdate will only be called if this method is overridden
140
+ virtual void FinalizeUpdate(const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
141
+ winrt::Microsoft::ReactNative::ComponentViewUpdateMask /*mask*/) noexcept {
142
+ }
143
+
144
+ ::_COMPONENT_VIEW_COMMAND_HANDLERS_::
145
+
146
+ ::_COMPONENT_VIEW_COMMAND_HANDLER_::
147
+
148
+ const std::shared_ptr<::_COMPONENT_NAME_::EventEmitter>& EventEmitter() const { return m_eventEmitter; }
149
+ const winrt::com_ptr<::_COMPONENT_NAME_::Props>& Props() const { return m_props; }
150
+
151
+ private:
152
+ winrt::com_ptr<::_COMPONENT_NAME_::Props> m_props;
153
+ std::shared_ptr<::_COMPONENT_NAME_::EventEmitter> m_eventEmitter;
154
+ };
155
+ `;
156
+
157
+ const registerTemplate = `
158
+ template <typename TUserData>
159
+ void Register::_COMPONENT_NAME_::NativeComponent(
160
+ winrt::Microsoft::ReactNative::IReactPackageBuilder const &packageBuilder,
161
+ std::function<void(const winrt::Microsoft::ReactNative::Composition::IReactCompositionViewComponentBuilder&)> builderCallback) noexcept {
162
+ packageBuilder.as<winrt::Microsoft::ReactNative::IReactPackageBuilderFabric>().AddViewComponent(
163
+ L"::_COMPONENT_NAME_::", [builderCallback](winrt::Microsoft::ReactNative::IReactViewComponentBuilder const &builder) noexcept {
164
+ auto compBuilder = builder.as<winrt::Microsoft::ReactNative::Composition::IReactCompositionViewComponentBuilder>();
165
+
166
+ builder.SetCreateProps([](winrt::Microsoft::ReactNative::ViewProps props,
167
+ const winrt::Microsoft::ReactNative::IComponentProps& cloneFrom) noexcept {
168
+ return winrt::make<::_COMPONENT_NAME_::Props>(props, cloneFrom);
169
+ });
170
+
171
+ builder.SetUpdatePropsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
172
+ const winrt::Microsoft::ReactNative::IComponentProps &newProps,
173
+ const winrt::Microsoft::ReactNative::IComponentProps &oldProps) noexcept {
174
+ auto userData = view.UserData().as<TUserData>();
175
+ userData->UpdateProps(view, newProps ? newProps.as<::_COMPONENT_NAME_::Props>() : nullptr, oldProps ? oldProps.as<::_COMPONENT_NAME_::Props>() : nullptr);
176
+ });
177
+
178
+ compBuilder.SetUpdateLayoutMetricsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
179
+ const winrt::Microsoft::ReactNative::LayoutMetrics &newLayoutMetrics,
180
+ const winrt::Microsoft::ReactNative::LayoutMetrics &oldLayoutMetrics) noexcept {
181
+ auto userData = view.UserData().as<TUserData>();
182
+ userData->UpdateLayoutMetrics(view, newLayoutMetrics, oldLayoutMetrics);
183
+ });
184
+
185
+ builder.SetUpdateEventEmitterHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
186
+ const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter) noexcept {
187
+ auto userData = view.UserData().as<TUserData>();
188
+ userData->UpdateEventEmitter(std::make_shared<::_COMPONENT_NAME_::EventEmitter>(eventEmitter));
189
+ });
190
+
191
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::FinalizeUpdate != &Base::_COMPONENT_NAME_::<TUserData>::FinalizeUpdate) {
192
+ builder.SetFinalizeUpdateHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
193
+ winrt::Microsoft::ReactNative::ComponentViewUpdateMask mask) noexcept {
194
+ auto userData = view.UserData().as<TUserData>();
195
+ userData->FinalizeUpdate(view, mask);
196
+ });
197
+ }
198
+
199
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::UpdateState != &Base::_COMPONENT_NAME_::<TUserData>::UpdateState) {
200
+ builder.SetUpdateStateHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
201
+ const winrt::Microsoft::ReactNative::IComponentState &newState) noexcept {
202
+ auto userData = view.UserData().as<TUserData>();
203
+ userData->UpdateState(view, newState);
204
+ });
205
+ }
206
+
207
+ ::_REGISTER_CUSTOM_COMMAND_HANDLER_::
208
+
209
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::MountChildComponentView != &Base::_COMPONENT_NAME_::<TUserData>::MountChildComponentView) {
210
+ builder.SetMountChildComponentViewHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
211
+ const winrt::Microsoft::ReactNative::MountChildComponentViewArgs &args) noexcept {
212
+ auto userData = view.UserData().as<TUserData>();
213
+ return userData->MountChildComponentView(view, args);
214
+ });
215
+ }
216
+
217
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::UnmountChildComponentView != &Base::_COMPONENT_NAME_::<TUserData>::UnmountChildComponentView) {
218
+ builder.SetUnmountChildComponentViewHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
219
+ const winrt::Microsoft::ReactNative::UnmountChildComponentViewArgs &args) noexcept {
220
+ auto userData = view.UserData().as<TUserData>();
221
+ return userData->UnmountChildComponentView(view, args);
222
+ });
223
+ }
224
+
225
+ compBuilder.SetViewComponentViewInitializer([](const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
226
+ auto userData = winrt::make_self<TUserData>();
227
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::Initialize != &Base::_COMPONENT_NAME_::<TUserData>::Initialize) {
228
+ userData->Initialize(view);
229
+ }
230
+ view.UserData(*userData);
231
+ });
232
+
233
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::CreateVisual != &Base::_COMPONENT_NAME_::<TUserData>::CreateVisual) {
234
+ compBuilder.SetCreateVisualHandler([](const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
235
+ auto userData = view.UserData().as<TUserData>();
236
+ return userData->CreateVisual(view);
237
+ });
238
+ }
239
+
240
+ // Allow app to further customize the builder
241
+ if (builderCallback) {
242
+ builderCallback(compBuilder);
243
+ }
244
+ });
245
+ }
246
+ `;
247
+
248
+ const fileTemplate = `
249
+ ${headerTemplate}
250
+
251
+ #ifdef RNW_NEW_ARCH
252
+
253
+ namespace ::_NAMESPACE_:: {
254
+
255
+ ::_COMPONENT_PROP_OBJECT_TYPES_::
256
+ ::_COMPONENT_PROP_TYPES_::
257
+
258
+ ::_COMPONENT_EVENT_EMITTER_::
259
+
260
+ ::_BASE_COMPONENT_STRUCT_::
261
+
262
+ ::_COMPONENT_REGISTRATION_::
263
+ } // namespace ::_NAMESPACE_::
264
+
265
+ #endif // #ifdef RNW_NEW_ARCH
266
+ `;
267
+
268
+ function capitalizeFirstLetter(s: string) {
269
+ return s.charAt(0).toUpperCase() + s.slice(1);
270
+ }
271
+
272
+ export function createComponentGenerator({
273
+ namespace,
274
+ cppStringType,
275
+ }: {
276
+ namespace: string;
277
+ cppStringType: CppStringTypes;
278
+ }) {
279
+ return (
280
+ _libraryName: string,
281
+ schema: SchemaType,
282
+ _moduleSpecName: string,
283
+ ): FilesOutput => {
284
+ const files = new Map<string, string>();
285
+
286
+ const cppCodegenOptions = {cppStringType};
287
+
288
+ for (const componentName of Object.keys(schema.modules)) {
289
+ const component = schema.modules[componentName];
290
+ setPreferredModuleName(componentName);
291
+
292
+ if (component.type === 'Component') {
293
+ console.log(`Generating ${componentName}.g.h`);
294
+
295
+ const componentShape = component.components[componentName];
296
+
297
+ componentShape.extendsProps.forEach(propsBaseType => {
298
+ if (
299
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
300
+ propsBaseType.type !== 'ReactNativeBuiltInType' ||
301
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
302
+ propsBaseType.knownTypeName !== 'ReactNativeCoreViewProps'
303
+ ) {
304
+ throw new Error(
305
+ 'Currently only supports props extending from ViewProps',
306
+ );
307
+ }
308
+ });
309
+
310
+ // Props
311
+ const propObjectAliases: AliasMap<
312
+ ObjectTypeAnnotation<PropTypeAnnotation>
313
+ > = {types: {}, jobs: []};
314
+ const propsName = `${componentName}Props`;
315
+ const propsFields = componentShape.props
316
+ .map(prop => {
317
+ const propType = translateComponentPropsFieldType(
318
+ prop.typeAnnotation,
319
+ propObjectAliases,
320
+ `${propsName}_${prop.name}`,
321
+ cppCodegenOptions,
322
+ );
323
+ return ` REACT_FIELD(${prop.name})\n ${
324
+ prop.optional && !propType.alreadySupportsOptionalOrHasDefault
325
+ ? `std::optional<${propType.type}>`
326
+ : propType.type
327
+ } ${prop.name}${propType.initializer};\n`;
328
+ })
329
+ .join('\n');
330
+
331
+ const propInitializers = componentShape.props
332
+ .map(prop => {
333
+ return ` ${prop.name} = cloneFromProps->${prop.name};`;
334
+ })
335
+ .join('\n');
336
+
337
+ const propObjectTypes = propObjectAliases.jobs
338
+ .map(propObjectTypeName => {
339
+ const propObjectType = propObjectAliases.types[propObjectTypeName]!;
340
+ const propsObjectFields = propObjectType.properties
341
+ .map(property => {
342
+ const propType = translateComponentPropsFieldType(
343
+ property.typeAnnotation,
344
+ propObjectAliases,
345
+ `${propsName}_${property.name}`,
346
+ cppCodegenOptions,
347
+ );
348
+ return ` REACT_FIELD(${property.name})\n ${
349
+ property.optional &&
350
+ !propType.alreadySupportsOptionalOrHasDefault
351
+ ? `std::optional<${propType.type}>`
352
+ : propType.type
353
+ } ${property.name}${propType.initializer};\n`;
354
+ })
355
+ .join('\n');
356
+
357
+ return propsObjectTemplate
358
+ .replace(
359
+ /::_OBJECT_NAME_::/g,
360
+ getAliasCppName(propObjectTypeName),
361
+ )
362
+ .replace(/::_OBJECT_FIELDS_::/g, propsObjectFields);
363
+ })
364
+ .join('\n');
365
+
366
+ // Events
367
+ const eventObjectAliases: AliasMap<
368
+ ObjectTypeAnnotation<EventTypeAnnotation>
369
+ > = {types: {}, jobs: []};
370
+ const eventEmitterName = `${componentName}EventEmitter`;
371
+ const eventEmitterMethods = componentShape.events
372
+ .filter(event => event.typeAnnotation.argument)
373
+ .map(event => {
374
+ if (event.typeAnnotation.argument?.baseTypes) {
375
+ throw new Error(
376
+ 'Events with base type arguments not currently supported',
377
+ );
378
+ }
379
+
380
+ // Called to collect the eventObjectAliases
381
+ translateComponentEventType(
382
+ event.typeAnnotation.argument!,
383
+ eventObjectAliases,
384
+ `${event.name}`,
385
+ cppCodegenOptions,
386
+ );
387
+
388
+ // onSomething -> something
389
+ let eventNameLower = event.name.replace('on', '');
390
+ eventNameLower =
391
+ eventNameLower[0].toLowerCase() + eventNameLower.slice(1);
392
+
393
+ return eventEmitterMethodTemplate
394
+ .replace(/::_EVENT_NAME_::/g, event.name)
395
+ .replace(/::_EVENT_NAME_NO_ON_::/g, eventNameLower)
396
+ .replace(
397
+ /::_EVENT_OBJECT_TYPE_::/g,
398
+ event.name.replace('on', 'On'),
399
+ );
400
+ })
401
+ .join('\n\n');
402
+
403
+ const eventObjects = eventObjectAliases.jobs
404
+ .map(eventObjectTypeName => {
405
+ const eventObjectType =
406
+ eventObjectAliases.types[eventObjectTypeName]!;
407
+ const eventObjectFields = eventObjectType.properties
408
+ .map(property => {
409
+ const eventPropType = translateComponentEventType(
410
+ property.typeAnnotation,
411
+ eventObjectAliases,
412
+ eventObjectTypeName,
413
+ cppCodegenOptions,
414
+ );
415
+ return ` REACT_FIELD(${property.name})\n ${
416
+ property.optional &&
417
+ !eventPropType.alreadySupportsOptionalOrHasDefault
418
+ ? `std::optional<${eventPropType.type}>`
419
+ : eventPropType.type
420
+ } ${property.name}${eventPropType.initializer};\n`;
421
+ })
422
+ .join('\n');
423
+ return eventsObjectTemplate
424
+ .replace(
425
+ /::_OBJECT_NAME_::/g,
426
+ `${componentName}_${eventObjectTypeName.replace('on', 'On')}`,
427
+ )
428
+ .replace(/::_OBJECT_FIELDS_::/g, eventObjectFields);
429
+ })
430
+ .join('\n');
431
+
432
+ const eventObjectUsings = eventObjectAliases.jobs
433
+ .map(eventObjectTypeName => {
434
+ return ` using ${eventObjectTypeName.replace(
435
+ 'on',
436
+ 'On',
437
+ )} = ${componentName}_${eventObjectTypeName.replace('on', 'On')};`;
438
+ })
439
+ .join('\n');
440
+
441
+ const eventEmitter = eventEmitterTemplate
442
+ .replace(/::_COMPONENT_EVENT_OBJECT_TYPES_::/g, eventObjects)
443
+ .replace(/::_EVENT_EMITTER_METHODS_::/g, eventEmitterMethods)
444
+ .replace(/::_EVENT_EMITTER_USINGS_::/g, eventObjectUsings);
445
+
446
+ // Commands
447
+ const commandAliases: AliasMap<
448
+ ObjectTypeAnnotation<CommandParamTypeAnnotation>
449
+ > = {types: {}, jobs: []};
450
+ const hasAnyCommands = componentShape.commands.length !== 0;
451
+ const commandHandlers = hasAnyCommands
452
+ ? componentShape.commands
453
+ .map(command => {
454
+ const commandArgs = command.typeAnnotation.params
455
+ .map(param => {
456
+ const commandArgType = translateCommandParamType(
457
+ param.typeAnnotation,
458
+ commandAliases,
459
+ `${componentName}_${command.name}`,
460
+ cppCodegenOptions,
461
+ );
462
+ return `${
463
+ param.optional &&
464
+ !commandArgType.alreadySupportsOptionalOrHasDefault
465
+ ? `std::optional<${commandArgType.type}>`
466
+ : commandArgType.type
467
+ } ${param.name}`;
468
+ })
469
+ .join(', ');
470
+
471
+ return ` // You must provide an implementation of this method to handle the "${
472
+ command.name
473
+ }" command
474
+ virtual void Handle${capitalizeFirstLetter(
475
+ command.name,
476
+ )}Command(${commandArgs}) noexcept = 0;`;
477
+ })
478
+ .join('\n\n')
479
+ : '';
480
+
481
+ const commandHandler = hasAnyCommands
482
+ ? `void HandleCommand(const winrt::Microsoft::ReactNative::ComponentView &view, const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
483
+ auto userData = view.UserData().as<TUserData>();
484
+ auto commandName = args.CommandName();
485
+ ${componentShape.commands
486
+ .map(command => {
487
+ const commaSeparatedCommandArgs = command.typeAnnotation.params
488
+ .map(param => param.name)
489
+ .join(', ');
490
+ return ` if (commandName == L"${command.name}") {
491
+ ${
492
+ command.typeAnnotation.params.length !== 0
493
+ ? ` ${command.typeAnnotation.params
494
+ .map(param => {
495
+ const commandArgType = translateCommandParamType(
496
+ param.typeAnnotation,
497
+ commandAliases,
498
+ `${componentName}_${command.name}`,
499
+ cppCodegenOptions,
500
+ );
501
+ return `${
502
+ param.optional &&
503
+ !commandArgType.alreadySupportsOptionalOrHasDefault
504
+ ? `std::optional<${commandArgType.type}>`
505
+ : commandArgType.type
506
+ } ${param.name};`;
507
+ })
508
+ .join('\n')}
509
+ winrt::Microsoft::ReactNative::ReadArgs(args.CommandArgs(), ${commaSeparatedCommandArgs});`
510
+ : ''
511
+ }
512
+ userData->Handle${capitalizeFirstLetter(
513
+ command.name,
514
+ )}Command(${commaSeparatedCommandArgs});
515
+ return;
516
+ }`;
517
+ })
518
+ .join('\n\n')}
519
+ }`
520
+ : '';
521
+
522
+ const registerCommandHandler = hasAnyCommands
523
+ ? ` builder.SetCustomCommandHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
524
+ const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
525
+ auto userData = view.UserData().as<TUserData>();
526
+ userData->HandleCommand(view, args);
527
+ });`
528
+ : '';
529
+
530
+ const baseType = baseStructTemplate
531
+ .replace(/::_COMPONENT_VIEW_COMMAND_HANDLERS_::/g, commandHandlers)
532
+ .replace(/::_COMPONENT_VIEW_COMMAND_HANDLER_::/g, commandHandler);
533
+
534
+ // Registration
535
+ const componentRegistration = registerTemplate.replace(
536
+ /::_REGISTER_CUSTOM_COMMAND_HANDLER_::/g,
537
+ registerCommandHandler,
538
+ );
539
+
540
+ // Final output
541
+ const replaceContent = function (template: string): string {
542
+ return template
543
+ .replace(/::_COMPONENT_PROP_OBJECT_TYPES_::/g, propObjectTypes)
544
+ .replace(/::_COMPONENT_PROP_TYPES_::/g, propsTemplate)
545
+ .replace(/::_COMPONENT_EVENT_EMITTER_::/g, eventEmitter)
546
+ .replace(/::_BASE_COMPONENT_STRUCT_::/g, baseType)
547
+ .replace(/::_COMPONENT_REGISTRATION_::/g, componentRegistration)
548
+ .replace(/::_EVENT_EMITTER_NAME_::/g, eventEmitterName)
549
+ .replace(/::_PROPS_NAME_::/g, propsName)
550
+ .replace(/::_COMPONENT_NAME_::/g, componentName)
551
+ .replace(/::_PROP_INITIALIZERS_::/g, propInitializers)
552
+ .replace(/::_PROPS_FIELDS_::/g, propsFields)
553
+ .replace(/::_NAMESPACE_::/g, namespace)
554
+ .replace(/\n\n\n+/g, '\n\n');
555
+ };
556
+
557
+ files.set(`${componentName}.g.h`, replaceContent(fileTemplate));
558
+ }
559
+ }
560
+
561
+ return files;
562
+ };
563
+ }