@react-native-windows/codegen 0.0.0-canary.11 → 0.0.0-canary.111

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