@react-native-windows/codegen 0.0.0-canary.13 → 0.0.0-canary.130

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 +1125 -16
  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 +469 -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 +39 -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 +616 -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 +309 -53
  52. package/src/generators/PropObjectTypes.ts +223 -0
  53. package/src/generators/ReturnTypes.ts +38 -40
  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 -726
  60. package/jest.config.js +0 -1
  61. package/tsconfig.json +0 -5
@@ -0,0 +1,616 @@
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 = std::move(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
+ // CreateAutomationPeer will only be called if this method is overridden
145
+ virtual winrt::Windows::Foundation::IInspectable CreateAutomationPeer(const winrt::Microsoft::ReactNative::ComponentView & /*view*/,
146
+ const winrt::Microsoft::ReactNative::CreateAutomationPeerArgs& /*args*/) noexcept {
147
+ return nullptr;
148
+ }
149
+
150
+ ::_COMPONENT_VIEW_COMMAND_HANDLERS_::
151
+
152
+ ::_COMPONENT_VIEW_COMMAND_HANDLER_::
153
+
154
+ const std::shared_ptr<::_COMPONENT_NAME_::EventEmitter>& EventEmitter() const { return m_eventEmitter; }
155
+ const winrt::com_ptr<::_COMPONENT_NAME_::Props>& Props() const { return m_props; }
156
+
157
+ private:
158
+ winrt::com_ptr<::_COMPONENT_NAME_::Props> m_props;
159
+ std::shared_ptr<::_COMPONENT_NAME_::EventEmitter> m_eventEmitter;
160
+ };
161
+ `;
162
+
163
+ const registerTemplate = `
164
+ template <typename TUserData>
165
+ void Register::_COMPONENT_NAME_::NativeComponent(
166
+ winrt::Microsoft::ReactNative::IReactPackageBuilder const &packageBuilder,
167
+ std::function<void(const winrt::Microsoft::ReactNative::Composition::IReactCompositionViewComponentBuilder&)> builderCallback) noexcept {
168
+ packageBuilder.as<winrt::Microsoft::ReactNative::IReactPackageBuilderFabric>().AddViewComponent(
169
+ L"::_COMPONENT_NAME_::", [builderCallback](winrt::Microsoft::ReactNative::IReactViewComponentBuilder const &builder) noexcept {
170
+ auto compBuilder = builder.as<winrt::Microsoft::ReactNative::Composition::IReactCompositionViewComponentBuilder>();
171
+
172
+ builder.SetCreateProps([](winrt::Microsoft::ReactNative::ViewProps props,
173
+ const winrt::Microsoft::ReactNative::IComponentProps& cloneFrom) noexcept {
174
+ return winrt::make<::_COMPONENT_NAME_::Props>(props, cloneFrom);
175
+ });
176
+
177
+ builder.SetUpdatePropsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
178
+ const winrt::Microsoft::ReactNative::IComponentProps &newProps,
179
+ const winrt::Microsoft::ReactNative::IComponentProps &oldProps) noexcept {
180
+ auto userData = view.UserData().as<TUserData>();
181
+ userData->UpdateProps(view, newProps ? newProps.as<::_COMPONENT_NAME_::Props>() : nullptr, oldProps ? oldProps.as<::_COMPONENT_NAME_::Props>() : nullptr);
182
+ });
183
+
184
+ compBuilder.SetUpdateLayoutMetricsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
185
+ const winrt::Microsoft::ReactNative::LayoutMetrics &newLayoutMetrics,
186
+ const winrt::Microsoft::ReactNative::LayoutMetrics &oldLayoutMetrics) noexcept {
187
+ auto userData = view.UserData().as<TUserData>();
188
+ userData->UpdateLayoutMetrics(view, newLayoutMetrics, oldLayoutMetrics);
189
+ });
190
+
191
+ builder.SetUpdateEventEmitterHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
192
+ const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter) noexcept {
193
+ auto userData = view.UserData().as<TUserData>();
194
+ userData->UpdateEventEmitter(std::make_shared<::_COMPONENT_NAME_::EventEmitter>(eventEmitter));
195
+ });
196
+
197
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::FinalizeUpdate != &Base::_COMPONENT_NAME_::<TUserData>::FinalizeUpdate) {
198
+ builder.SetFinalizeUpdateHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
199
+ winrt::Microsoft::ReactNative::ComponentViewUpdateMask mask) noexcept {
200
+ auto userData = view.UserData().as<TUserData>();
201
+ userData->FinalizeUpdate(view, mask);
202
+ });
203
+ }
204
+
205
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::UpdateState != &Base::_COMPONENT_NAME_::<TUserData>::UpdateState) {
206
+ builder.SetUpdateStateHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
207
+ const winrt::Microsoft::ReactNative::IComponentState &newState) noexcept {
208
+ auto userData = view.UserData().as<TUserData>();
209
+ userData->UpdateState(view, newState);
210
+ });
211
+ }
212
+
213
+ ::_REGISTER_CUSTOM_COMMAND_HANDLER_::
214
+
215
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::MountChildComponentView != &Base::_COMPONENT_NAME_::<TUserData>::MountChildComponentView) {
216
+ builder.SetMountChildComponentViewHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
217
+ const winrt::Microsoft::ReactNative::MountChildComponentViewArgs &args) noexcept {
218
+ auto userData = view.UserData().as<TUserData>();
219
+ return userData->MountChildComponentView(view, args);
220
+ });
221
+ }
222
+
223
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::UnmountChildComponentView != &Base::_COMPONENT_NAME_::<TUserData>::UnmountChildComponentView) {
224
+ builder.SetUnmountChildComponentViewHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
225
+ const winrt::Microsoft::ReactNative::UnmountChildComponentViewArgs &args) noexcept {
226
+ auto userData = view.UserData().as<TUserData>();
227
+ return userData->UnmountChildComponentView(view, args);
228
+ });
229
+ }
230
+
231
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::CreateAutomationPeer != &Base::_COMPONENT_NAME_::<TUserData>::CreateAutomationPeer) {
232
+ builder.SetCreateAutomationPeerHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
233
+ const winrt::Microsoft::ReactNative::CreateAutomationPeerArgs& args) noexcept {
234
+ auto userData = view.UserData().as<TUserData>();
235
+ return userData->CreateAutomationPeer(view, args);
236
+ });
237
+ }
238
+
239
+ compBuilder.SetViewComponentViewInitializer([](const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
240
+ auto userData = winrt::make_self<TUserData>();
241
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::Initialize != &Base::_COMPONENT_NAME_::<TUserData>::Initialize) {
242
+ userData->Initialize(view);
243
+ }
244
+ view.UserData(*userData);
245
+ });
246
+
247
+ if CONSTEXPR_SUPPORTED_ON_VIRTUAL_FN_ADDRESS (&TUserData::CreateVisual != &Base::_COMPONENT_NAME_::<TUserData>::CreateVisual) {
248
+ compBuilder.SetCreateVisualHandler([](const winrt::Microsoft::ReactNative::ComponentView &view) noexcept {
249
+ auto userData = view.UserData().as<TUserData>();
250
+ return userData->CreateVisual(view);
251
+ });
252
+ }
253
+
254
+ // Allow app to further customize the builder
255
+ if (builderCallback) {
256
+ builderCallback(compBuilder);
257
+ }
258
+ });
259
+ }
260
+ `;
261
+
262
+ const fileTemplate = `
263
+ ${headerTemplate}
264
+
265
+ #ifdef RNW_NEW_ARCH
266
+
267
+ namespace ::_NAMESPACE_:: {
268
+
269
+ ::_COMPONENT_PROP_OBJECT_TYPES_::
270
+ ::_COMPONENT_PROP_TYPES_::
271
+
272
+ ::_COMPONENT_EVENT_EMITTER_::
273
+
274
+ ::_BASE_COMPONENT_STRUCT_::
275
+
276
+ ::_COMPONENT_REGISTRATION_::
277
+ } // namespace ::_NAMESPACE_::
278
+
279
+ #endif // #ifdef RNW_NEW_ARCH
280
+ `;
281
+
282
+ function capitalizeFirstLetter(s: string) {
283
+ return s.charAt(0).toUpperCase() + s.slice(1);
284
+ }
285
+
286
+ export function createComponentGenerator({
287
+ namespace,
288
+ cppStringType,
289
+ }: {
290
+ namespace: string;
291
+ cppStringType: CppStringTypes;
292
+ }) {
293
+ return (
294
+ _libraryName: string,
295
+ schema: SchemaType,
296
+ _moduleSpecName: string,
297
+ ): FilesOutput => {
298
+ const files = new Map<string, string>();
299
+
300
+ const cppCodegenOptions = {cppStringType};
301
+
302
+ for (const componentName of Object.keys(schema.modules)) {
303
+ const component = schema.modules[componentName];
304
+ setPreferredModuleName(componentName);
305
+
306
+ if (component.type === 'Component') {
307
+ console.log(`Generating ${componentName}.g.h`);
308
+
309
+ const componentShape = component.components[componentName];
310
+
311
+ componentShape.extendsProps.forEach(propsBaseType => {
312
+ if (
313
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
314
+ propsBaseType.type !== 'ReactNativeBuiltInType' ||
315
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
316
+ propsBaseType.knownTypeName !== 'ReactNativeCoreViewProps'
317
+ ) {
318
+ throw new Error(
319
+ 'Currently only supports props extending from ViewProps',
320
+ );
321
+ }
322
+ });
323
+
324
+ // Props
325
+ const propObjectAliases: AliasMap<
326
+ ObjectTypeAnnotation<PropTypeAnnotation>
327
+ > = {types: {}, jobs: []};
328
+ const propsName = `${componentName}Props`;
329
+ const propsFields = componentShape.props
330
+ .map(prop => {
331
+ const propType = translateComponentPropsFieldType(
332
+ prop.typeAnnotation,
333
+ propObjectAliases,
334
+ `${propsName}_${prop.name}`,
335
+ cppCodegenOptions,
336
+ );
337
+ return ` REACT_FIELD(${prop.name})\n ${
338
+ prop.optional && !propType.alreadySupportsOptionalOrHasDefault
339
+ ? `std::optional<${propType.type}>`
340
+ : propType.type
341
+ } ${prop.name}${propType.initializer};\n`;
342
+ })
343
+ .join('\n');
344
+
345
+ const eventPropFields = componentShape.events.length
346
+ ? ` // These fields can be used to determine if JS has registered for this event\n` +
347
+ componentShape.events
348
+ .map(event => {
349
+ return ` REACT_FIELD(${event.name})\n bool ${event.name}{false};\n`;
350
+ })
351
+ .join('\n')
352
+ : '';
353
+
354
+ const propInitializers = componentShape.props
355
+ .map(prop => {
356
+ if (prop.typeAnnotation.type === 'MixedTypeAnnotation')
357
+ return ` ${prop.name} = cloneFromProps->${prop.name}.Copy();`;
358
+ else return ` ${prop.name} = cloneFromProps->${prop.name};`;
359
+ })
360
+ .join('\n');
361
+
362
+ const eventPropInitializers = componentShape.events.length
363
+ ? componentShape.events
364
+ .map(event => {
365
+ return ` ${event.name} = cloneFromProps->${event.name};`;
366
+ })
367
+ .join('\n')
368
+ : '';
369
+
370
+ const propObjectTypes = propObjectAliases.jobs
371
+ .map(propObjectTypeName => {
372
+ const propObjectType = propObjectAliases.types[propObjectTypeName]!;
373
+ const propsObjectFields = propObjectType.properties
374
+ .map(property => {
375
+ const propType = translateComponentPropsFieldType(
376
+ property.typeAnnotation,
377
+ propObjectAliases,
378
+ `${propsName}_${property.name}`,
379
+ cppCodegenOptions,
380
+ );
381
+ return ` REACT_FIELD(${property.name})\n ${
382
+ property.optional &&
383
+ !propType.alreadySupportsOptionalOrHasDefault
384
+ ? `std::optional<${propType.type}>`
385
+ : propType.type
386
+ } ${property.name}${propType.initializer};\n`;
387
+ })
388
+ .join('\n');
389
+
390
+ return propsObjectTemplate
391
+ .replace(
392
+ /::_OBJECT_NAME_::/g,
393
+ getAliasCppName(propObjectTypeName),
394
+ )
395
+ .replace(/::_OBJECT_FIELDS_::/g, propsObjectFields);
396
+ })
397
+ .join('\n');
398
+
399
+ // Events
400
+ const eventObjectAliases: AliasMap<
401
+ ObjectTypeAnnotation<EventTypeAnnotation>
402
+ > = {types: {}, jobs: []};
403
+ const eventEmitterName = `${componentName}EventEmitter`;
404
+ const eventEmitterMethods = componentShape.events
405
+ .filter(event => event.typeAnnotation.argument)
406
+ .map(event => {
407
+ if (event.typeAnnotation.argument?.baseTypes) {
408
+ throw new Error(
409
+ 'Events with base type arguments not currently supported',
410
+ );
411
+ }
412
+
413
+ // Called to collect the eventObjectAliases
414
+ translateComponentEventType(
415
+ event.typeAnnotation.argument!,
416
+ eventObjectAliases,
417
+ `${event.name}`,
418
+ cppCodegenOptions,
419
+ );
420
+
421
+ // onSomething -> something
422
+ let eventNameLower = event.name.replace('on', '');
423
+ eventNameLower =
424
+ eventNameLower[0].toLowerCase() + eventNameLower.slice(1);
425
+
426
+ return eventEmitterMethodTemplate
427
+ .replace(/::_EVENT_NAME_::/g, event.name)
428
+ .replace(/::_EVENT_NAME_NO_ON_::/g, eventNameLower)
429
+ .replace(
430
+ /::_EVENT_OBJECT_TYPE_::/g,
431
+ event.name.replace('on', 'On'),
432
+ );
433
+ })
434
+ .join('\n\n');
435
+
436
+ const eventObjectUsings = eventObjectAliases.jobs
437
+ .map(eventObjectTypeName => {
438
+ return ` using ${eventObjectTypeName.replace('on', 'On')} = ${
439
+ getAliasCppName(eventObjectTypeName) /*.replace('_on', '_On')*/
440
+ };`;
441
+ })
442
+ .join('\n');
443
+
444
+ // Collect all the alias types for the event objects so that we can generate the unnamed types within objects
445
+ eventObjectAliases.jobs.forEach(eventObjectTypeName => {
446
+ const eventObjectType =
447
+ eventObjectAliases.types[eventObjectTypeName]!;
448
+ eventObjectType.properties.forEach(property => {
449
+ translateComponentEventType(
450
+ property.typeAnnotation,
451
+ eventObjectAliases,
452
+ eventObjectTypeName,
453
+ cppCodegenOptions,
454
+ );
455
+ });
456
+ });
457
+
458
+ const eventObjects = eventObjectAliases.jobs
459
+ .reverse()
460
+ .map(eventObjectTypeName => {
461
+ const eventObjectType =
462
+ eventObjectAliases.types[eventObjectTypeName]!;
463
+ const eventObjectFields = eventObjectType.properties
464
+ .map(property => {
465
+ const eventPropType = translateComponentEventType(
466
+ property.typeAnnotation,
467
+ eventObjectAliases,
468
+ eventObjectTypeName,
469
+ cppCodegenOptions,
470
+ );
471
+ return ` REACT_FIELD(${property.name})\n ${
472
+ property.optional &&
473
+ !eventPropType.alreadySupportsOptionalOrHasDefault
474
+ ? `std::optional<${eventPropType.type}>`
475
+ : eventPropType.type
476
+ } ${property.name}${eventPropType.initializer};\n`;
477
+ })
478
+ .join('\n');
479
+ return eventsObjectTemplate
480
+ .replace(
481
+ /::_OBJECT_NAME_::/g,
482
+ getAliasCppName(eventObjectTypeName) /*.replace('_on', '_On')*/,
483
+ )
484
+ .replace(/::_OBJECT_FIELDS_::/g, eventObjectFields);
485
+ })
486
+ .join('\n');
487
+
488
+ const eventEmitter = eventEmitterTemplate
489
+ .replace(/::_COMPONENT_EVENT_OBJECT_TYPES_::/g, eventObjects)
490
+ .replace(/::_EVENT_EMITTER_METHODS_::/g, eventEmitterMethods)
491
+ .replace(/::_EVENT_EMITTER_USINGS_::/g, eventObjectUsings);
492
+
493
+ // Commands
494
+ const commandAliases: AliasMap<
495
+ ObjectTypeAnnotation<CommandParamTypeAnnotation>
496
+ > = {types: {}, jobs: []};
497
+ const hasAnyCommands = componentShape.commands.length !== 0;
498
+ const commandHandlers = hasAnyCommands
499
+ ? componentShape.commands
500
+ .map(command => {
501
+ const commandArgs = command.typeAnnotation.params
502
+ .map(param => {
503
+ const commandArgType = translateCommandParamType(
504
+ param.typeAnnotation,
505
+ commandAliases,
506
+ `${componentName}_${command.name}`,
507
+ cppCodegenOptions,
508
+ );
509
+ return `${
510
+ param.optional &&
511
+ !commandArgType.alreadySupportsOptionalOrHasDefault
512
+ ? `std::optional<${commandArgType.type}>`
513
+ : commandArgType.type
514
+ } ${param.name}`;
515
+ })
516
+ .join(', ');
517
+
518
+ return ` // You must provide an implementation of this method to handle the "${
519
+ command.name
520
+ }" command
521
+ virtual void Handle${capitalizeFirstLetter(
522
+ command.name,
523
+ )}Command(${commandArgs}) noexcept = 0;`;
524
+ })
525
+ .join('\n\n')
526
+ : '';
527
+
528
+ const commandHandler = hasAnyCommands
529
+ ? `void HandleCommand(const winrt::Microsoft::ReactNative::ComponentView &view, const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
530
+ auto userData = view.UserData().as<TUserData>();
531
+ auto commandName = args.CommandName();
532
+ ${componentShape.commands
533
+ .map(command => {
534
+ const commaSeparatedCommandArgs = command.typeAnnotation.params
535
+ .map(param => param.name)
536
+ .join(', ');
537
+ return ` if (commandName == L"${command.name}") {
538
+ ${
539
+ command.typeAnnotation.params.length !== 0
540
+ ? ` ${command.typeAnnotation.params
541
+ .map(param => {
542
+ const commandArgType = translateCommandParamType(
543
+ param.typeAnnotation,
544
+ commandAliases,
545
+ `${componentName}_${command.name}`,
546
+ cppCodegenOptions,
547
+ );
548
+ return `${
549
+ param.optional &&
550
+ !commandArgType.alreadySupportsOptionalOrHasDefault
551
+ ? `std::optional<${commandArgType.type}>`
552
+ : commandArgType.type
553
+ } ${param.name};`;
554
+ })
555
+ .join('\n')}
556
+ winrt::Microsoft::ReactNative::ReadArgs(args.CommandArgs(), ${commaSeparatedCommandArgs});`
557
+ : ''
558
+ }
559
+ userData->Handle${capitalizeFirstLetter(
560
+ command.name,
561
+ )}Command(${commaSeparatedCommandArgs});
562
+ return;
563
+ }`;
564
+ })
565
+ .join('\n\n')}
566
+ }`
567
+ : '';
568
+
569
+ const registerCommandHandler = hasAnyCommands
570
+ ? ` builder.SetCustomCommandHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
571
+ const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
572
+ auto userData = view.UserData().as<TUserData>();
573
+ userData->HandleCommand(view, args);
574
+ });`
575
+ : '';
576
+
577
+ const baseType = baseStructTemplate
578
+ .replace(/::_COMPONENT_VIEW_COMMAND_HANDLERS_::/g, commandHandlers)
579
+ .replace(/::_COMPONENT_VIEW_COMMAND_HANDLER_::/g, commandHandler);
580
+
581
+ // Registration
582
+ const componentRegistration = registerTemplate.replace(
583
+ /::_REGISTER_CUSTOM_COMMAND_HANDLER_::/g,
584
+ registerCommandHandler,
585
+ );
586
+
587
+ // Final output
588
+ const replaceContent = function (template: string): string {
589
+ return template
590
+ .replace(/::_COMPONENT_PROP_OBJECT_TYPES_::/g, propObjectTypes)
591
+ .replace(/::_COMPONENT_PROP_TYPES_::/g, propsTemplate)
592
+ .replace(/::_COMPONENT_EVENT_EMITTER_::/g, eventEmitter)
593
+ .replace(/::_BASE_COMPONENT_STRUCT_::/g, baseType)
594
+ .replace(/::_COMPONENT_REGISTRATION_::/g, componentRegistration)
595
+ .replace(/::_EVENT_EMITTER_NAME_::/g, eventEmitterName)
596
+ .replace(/::_PROPS_NAME_::/g, propsName)
597
+ .replace(/::_COMPONENT_NAME_::/g, componentName)
598
+ .replace(
599
+ /::_PROP_INITIALIZERS_::/g,
600
+ [propInitializers, eventPropInitializers].join('\n'),
601
+ )
602
+ .replace(
603
+ /::_PROPS_FIELDS_::/g,
604
+ [propsFields, eventPropFields].join('\n'),
605
+ )
606
+ .replace(/::_NAMESPACE_::/g, namespace)
607
+ .replace(/\n\n\n+/g, '\n\n');
608
+ };
609
+
610
+ files.set(`${componentName}.g.h`, replaceContent(fileTemplate));
611
+ }
612
+ }
613
+
614
+ return files;
615
+ };
616
+ }