@react-native-windows/codegen 0.76.0-preview.1 → 0.76.0-preview.2
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 +14 -5
- package/lib-commonjs/Cli.js +1 -1
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/AliasGen.js +5 -2
- package/lib-commonjs/generators/AliasGen.js.map +1 -1
- package/lib-commonjs/generators/GenerateComponentWindows.js +97 -42
- package/lib-commonjs/generators/GenerateComponentWindows.js.map +1 -1
- package/lib-commonjs/generators/GenerateNM2.js +3 -1
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -1
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -1
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -1
- package/lib-commonjs/generators/ParamTypes.js.map +1 -1
- package/lib-commonjs/generators/ValidateMethods.js +9 -2
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -1
- package/lib-commonjs/index.js.map +1 -1
- package/package.json +4 -4
- package/src/Cli.ts +3 -2
- package/src/generators/AliasGen.ts +27 -18
- package/src/generators/GenerateComponentWindows.ts +232 -100
- package/src/generators/GenerateNM2.ts +7 -2
- package/src/generators/GenerateTypeScript.ts +7 -5
- package/src/generators/ObjectTypes.ts +7 -5
- package/src/generators/ParamTypes.ts +21 -11
- package/src/generators/ValidateMethods.ts +29 -19
- package/src/index.ts +13 -14
|
@@ -6,9 +6,19 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
SchemaType,
|
|
11
|
+
EventTypeAnnotation,
|
|
12
|
+
PropTypeAnnotation,
|
|
13
|
+
ObjectTypeAnnotation,
|
|
14
|
+
CommandParamTypeAnnotation,
|
|
15
|
+
} from '@react-native/codegen/lib/CodegenSchema';
|
|
10
16
|
import {getAliasCppName, setPreferredModuleName} from './AliasManaging';
|
|
11
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
translateComponentPropsFieldType,
|
|
19
|
+
translateComponentEventType,
|
|
20
|
+
translateCommandParamType,
|
|
21
|
+
} from './PropObjectTypes';
|
|
12
22
|
import type {CppStringTypes} from './ObjectTypes';
|
|
13
23
|
import type {AliasMap} from './AliasManaging';
|
|
14
24
|
|
|
@@ -24,8 +34,7 @@ const headerTemplate = `/*
|
|
|
24
34
|
#include <JSValueComposition.h>
|
|
25
35
|
#include <NativeModules.h>
|
|
26
36
|
#include <winrt/Microsoft.ReactNative.Composition.h>
|
|
27
|
-
#include <winrt/Microsoft.UI.Composition.h
|
|
28
|
-
|
|
37
|
+
#include <winrt/Microsoft.UI.Composition.h>`;
|
|
29
38
|
|
|
30
39
|
const propsTemplate = `REACT_STRUCT(::_PROPS_NAME_::)
|
|
31
40
|
struct ::_PROPS_NAME_:: : winrt::implements<::_PROPS_NAME_::, winrt::Microsoft::ReactNative::IComponentProps> {
|
|
@@ -37,16 +46,16 @@ struct ::_PROPS_NAME_:: : winrt::implements<::_PROPS_NAME_::, winrt::Microsoft::
|
|
|
37
46
|
|
|
38
47
|
::_PROPS_FIELDS_::
|
|
39
48
|
const winrt::Microsoft::ReactNative::ViewProps ViewProps;
|
|
40
|
-
}
|
|
49
|
+
};`;
|
|
41
50
|
|
|
42
51
|
const propsObjectTemplate = `REACT_STRUCT(::_OBJECT_NAME_::)
|
|
43
52
|
struct ::_OBJECT_NAME_:: {
|
|
44
53
|
::_OBJECT_FIELDS_::};
|
|
45
|
-
|
|
54
|
+
`;
|
|
46
55
|
const eventsObjectTemplate = `REACT_STRUCT(::_OBJECT_NAME_::)
|
|
47
56
|
struct ::_OBJECT_NAME_:: {
|
|
48
57
|
::_OBJECT_FIELDS_::};
|
|
49
|
-
|
|
58
|
+
`;
|
|
50
59
|
|
|
51
60
|
const eventEmitterMethodTemplate = ` void ::_EVENT_NAME_::(::_EVENT_OBJECT_TYPE_:: &value) const {
|
|
52
61
|
m_eventEmitter.DispatchEvent(L"::_EVENT_NAME_NO_ON_::", [value](const winrt::Microsoft::ReactNative::IJSValueWriter writer) {
|
|
@@ -54,7 +63,6 @@ const eventEmitterMethodTemplate = ` void ::_EVENT_NAME_::(::_EVENT_OBJECT_TYPE
|
|
|
54
63
|
});
|
|
55
64
|
}`;
|
|
56
65
|
|
|
57
|
-
|
|
58
66
|
const eventEmitterTemplate = `::_COMPONENT_EVENT_OBJECT_TYPES_::
|
|
59
67
|
|
|
60
68
|
struct ::_EVENT_EMITTER_NAME_:: {
|
|
@@ -67,7 +75,7 @@ struct ::_EVENT_EMITTER_NAME_:: {
|
|
|
67
75
|
|
|
68
76
|
private:
|
|
69
77
|
winrt::Microsoft::ReactNative::EventEmitter m_eventEmitter{nullptr};
|
|
70
|
-
}
|
|
78
|
+
};`;
|
|
71
79
|
|
|
72
80
|
const baseStructTemplate = `
|
|
73
81
|
template<typename TUserData>
|
|
@@ -227,7 +235,7 @@ namespace ::_NAMESPACE_:: {
|
|
|
227
235
|
|
|
228
236
|
function capitalizeFirstLetter(s: string) {
|
|
229
237
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
230
|
-
|
|
238
|
+
}
|
|
231
239
|
|
|
232
240
|
export function createComponentGenerator({
|
|
233
241
|
namespace,
|
|
@@ -255,114 +263,241 @@ export function createComponentGenerator({
|
|
|
255
263
|
const componentShape = component.components[componentName];
|
|
256
264
|
|
|
257
265
|
componentShape.extendsProps.forEach(propsBaseType => {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
266
|
+
if (
|
|
267
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
268
|
+
propsBaseType.type !== 'ReactNativeBuiltInType' ||
|
|
269
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
270
|
+
propsBaseType.knownTypeName !== 'ReactNativeCoreViewProps'
|
|
271
|
+
) {
|
|
272
|
+
throw new Error(
|
|
273
|
+
'Currently only supports props extending from ViewProps',
|
|
274
|
+
);
|
|
261
275
|
}
|
|
262
276
|
});
|
|
263
277
|
|
|
264
278
|
// Props
|
|
265
|
-
const propObjectAliases: AliasMap<
|
|
279
|
+
const propObjectAliases: AliasMap<
|
|
280
|
+
ObjectTypeAnnotation<PropTypeAnnotation>
|
|
281
|
+
> = {types: {}, jobs: []};
|
|
266
282
|
const propsName = `${componentName}Props`;
|
|
267
|
-
const propsFields = componentShape.props
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
283
|
+
const propsFields = componentShape.props
|
|
284
|
+
.map(prop => {
|
|
285
|
+
const propType = translateComponentPropsFieldType(
|
|
286
|
+
prop.typeAnnotation,
|
|
287
|
+
propObjectAliases,
|
|
288
|
+
`${propsName}_${prop.name}`,
|
|
289
|
+
cppCodegenOptions,
|
|
290
|
+
);
|
|
291
|
+
return ` REACT_FIELD(${prop.name})\n ${
|
|
292
|
+
prop.optional && !propType.alreadySupportsOptionalOrHasDefault
|
|
293
|
+
? `std::optional<${propType.type}>`
|
|
294
|
+
: propType.type
|
|
295
|
+
} ${prop.name}${propType.initializer};\n`;
|
|
296
|
+
})
|
|
297
|
+
.join('\n');
|
|
298
|
+
|
|
299
|
+
const propObjectTypes = propObjectAliases.jobs
|
|
300
|
+
.map(propObjectTypeName => {
|
|
301
|
+
const propObjectType = propObjectAliases.types[propObjectTypeName]!;
|
|
302
|
+
const propsObjectFields = propObjectType.properties
|
|
303
|
+
.map(property => {
|
|
304
|
+
const propType = translateComponentPropsFieldType(
|
|
305
|
+
property.typeAnnotation,
|
|
306
|
+
propObjectAliases,
|
|
307
|
+
`${propsName}_${property.name}`,
|
|
308
|
+
cppCodegenOptions,
|
|
309
|
+
);
|
|
310
|
+
return ` REACT_FIELD(${property.name})\n ${
|
|
311
|
+
property.optional &&
|
|
312
|
+
!propType.alreadySupportsOptionalOrHasDefault
|
|
313
|
+
? `std::optional<${propType.type}>`
|
|
314
|
+
: propType.type
|
|
315
|
+
} ${property.name}${propType.initializer};\n`;
|
|
316
|
+
})
|
|
317
|
+
.join('\n');
|
|
318
|
+
|
|
319
|
+
return propsObjectTemplate
|
|
320
|
+
.replace(
|
|
321
|
+
/::_OBJECT_NAME_::/g,
|
|
322
|
+
getAliasCppName(propObjectTypeName),
|
|
323
|
+
)
|
|
324
|
+
.replace(/::_OBJECT_FIELDS_::/g, propsObjectFields);
|
|
325
|
+
})
|
|
326
|
+
.join('\n');
|
|
282
327
|
|
|
283
328
|
// Events
|
|
284
|
-
const eventObjectAliases: AliasMap<
|
|
329
|
+
const eventObjectAliases: AliasMap<
|
|
330
|
+
ObjectTypeAnnotation<EventTypeAnnotation>
|
|
331
|
+
> = {types: {}, jobs: []};
|
|
285
332
|
const eventEmitterName = `${componentName}EventEmitter`;
|
|
286
|
-
const eventEmitterMethods = componentShape.events
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
333
|
+
const eventEmitterMethods = componentShape.events
|
|
334
|
+
.filter(event => event.typeAnnotation.argument)
|
|
335
|
+
.map(event => {
|
|
336
|
+
if (event.typeAnnotation.argument?.baseTypes) {
|
|
337
|
+
throw new Error(
|
|
338
|
+
'Events with base type arguments not currently supported',
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Called to collect the eventObjectAliases
|
|
343
|
+
translateComponentEventType(
|
|
344
|
+
event.typeAnnotation.argument!,
|
|
345
|
+
eventObjectAliases,
|
|
346
|
+
`${event.name}`,
|
|
347
|
+
cppCodegenOptions,
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
// onSomething -> something
|
|
351
|
+
let eventNameLower = event.name.replace('on', '');
|
|
352
|
+
eventNameLower =
|
|
353
|
+
eventNameLower[0].toLowerCase() + eventNameLower.slice(1);
|
|
354
|
+
|
|
355
|
+
return eventEmitterMethodTemplate
|
|
356
|
+
.replace(/::_EVENT_NAME_::/g, event.name)
|
|
357
|
+
.replace(/::_EVENT_NAME_NO_ON_::/g, eventNameLower)
|
|
358
|
+
.replace(
|
|
359
|
+
/::_EVENT_OBJECT_TYPE_::/g,
|
|
360
|
+
event.name.replace('on', 'On'),
|
|
361
|
+
);
|
|
362
|
+
})
|
|
363
|
+
.join('\n\n');
|
|
364
|
+
|
|
365
|
+
const eventObjects = eventObjectAliases.jobs
|
|
366
|
+
.map(eventObjectTypeName => {
|
|
367
|
+
const eventObjectType =
|
|
368
|
+
eventObjectAliases.types[eventObjectTypeName]!;
|
|
369
|
+
const eventObjectFields = eventObjectType.properties
|
|
370
|
+
.map(property => {
|
|
371
|
+
const eventPropType = translateComponentEventType(
|
|
372
|
+
property.typeAnnotation,
|
|
373
|
+
eventObjectAliases,
|
|
374
|
+
eventObjectTypeName,
|
|
375
|
+
cppCodegenOptions,
|
|
376
|
+
);
|
|
377
|
+
return ` REACT_FIELD(${property.name})\n ${
|
|
378
|
+
property.optional &&
|
|
379
|
+
!eventPropType.alreadySupportsOptionalOrHasDefault
|
|
380
|
+
? `std::optional<${eventPropType.type}>`
|
|
381
|
+
: eventPropType.type
|
|
382
|
+
} ${property.name}${eventPropType.initializer};\n`;
|
|
383
|
+
})
|
|
384
|
+
.join('\n');
|
|
385
|
+
return eventsObjectTemplate
|
|
386
|
+
.replace(
|
|
387
|
+
/::_OBJECT_NAME_::/g,
|
|
388
|
+
`${componentName}_${eventObjectTypeName.replace('on', 'On')}`,
|
|
389
|
+
)
|
|
390
|
+
.replace(/::_OBJECT_FIELDS_::/g, eventObjectFields);
|
|
391
|
+
})
|
|
392
|
+
.join('\n');
|
|
393
|
+
|
|
394
|
+
const eventObjectUsings = eventObjectAliases.jobs
|
|
395
|
+
.map(eventObjectTypeName => {
|
|
396
|
+
return ` using ${eventObjectTypeName.replace(
|
|
397
|
+
'on',
|
|
398
|
+
'On',
|
|
399
|
+
)} = ${componentName}_${eventObjectTypeName.replace('on', 'On')};`;
|
|
400
|
+
})
|
|
401
|
+
.join('\n');
|
|
290
402
|
|
|
291
|
-
// Called to collect the eventObjectAliases
|
|
292
|
-
translateComponentEventType(event.typeAnnotation.argument!, eventObjectAliases, `${event.name}`, cppCodegenOptions);
|
|
293
|
-
|
|
294
|
-
// onSomething -> something
|
|
295
|
-
let eventNameLower = event.name.replace('on', '');
|
|
296
|
-
eventNameLower = eventNameLower[0].toLowerCase() + eventNameLower.slice(1);
|
|
297
|
-
|
|
298
|
-
return eventEmitterMethodTemplate
|
|
299
|
-
.replace(/::_EVENT_NAME_::/g,event.name)
|
|
300
|
-
.replace(/::_EVENT_NAME_NO_ON_::/g,eventNameLower)
|
|
301
|
-
.replace(/::_EVENT_OBJECT_TYPE_::/g, event.name.replace('on', 'On'));
|
|
302
|
-
}).join('\n\n');
|
|
303
|
-
|
|
304
|
-
const eventObjects = eventObjectAliases.jobs.map(eventObjectTypeName => {
|
|
305
|
-
const eventObjectType = eventObjectAliases.types[eventObjectTypeName]!;
|
|
306
|
-
const eventObjectFields = eventObjectType.properties.map(property => {
|
|
307
|
-
const eventPropType = translateComponentEventType(property.typeAnnotation, eventObjectAliases, eventObjectTypeName, cppCodegenOptions);
|
|
308
|
-
return ` REACT_FIELD(${property.name})\n ${(property.optional && !eventPropType.alreadySupportsOptionalOrHasDefault) ? `std::optional<${eventPropType.type}>` : eventPropType.type} ${property.name}${eventPropType.initializer};\n`;
|
|
309
|
-
}).join('\n');
|
|
310
|
-
return eventsObjectTemplate.replace(/::_OBJECT_NAME_::/g, `${componentName}_${eventObjectTypeName.replace('on', 'On')}`).replace(/::_OBJECT_FIELDS_::/g, eventObjectFields);
|
|
311
|
-
}).join('\n');
|
|
312
|
-
|
|
313
|
-
const eventObjectUsings = eventObjectAliases.jobs.map(eventObjectTypeName => {
|
|
314
|
-
return ` using ${eventObjectTypeName.replace('on', 'On')} = ${componentName}_${eventObjectTypeName.replace('on', 'On')};`
|
|
315
|
-
}).join('\n');
|
|
316
|
-
|
|
317
403
|
const eventEmitter = eventEmitterTemplate
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
404
|
+
.replace(/::_COMPONENT_EVENT_OBJECT_TYPES_::/g, eventObjects)
|
|
405
|
+
.replace(/::_EVENT_EMITTER_METHODS_::/g, eventEmitterMethods)
|
|
406
|
+
.replace(/::_EVENT_EMITTER_USINGS_::/g, eventObjectUsings);
|
|
322
407
|
|
|
323
408
|
// Commands
|
|
324
|
-
const commandAliases: AliasMap<
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
409
|
+
const commandAliases: AliasMap<
|
|
410
|
+
ObjectTypeAnnotation<CommandParamTypeAnnotation>
|
|
411
|
+
> = {types: {}, jobs: []};
|
|
412
|
+
const hasAnyCommands = componentShape.commands.length !== 0;
|
|
413
|
+
const commandHandlers = hasAnyCommands
|
|
414
|
+
? componentShape.commands
|
|
415
|
+
.map(command => {
|
|
416
|
+
const commandArgs = command.typeAnnotation.params
|
|
417
|
+
.map(param => {
|
|
418
|
+
const commandArgType = translateCommandParamType(
|
|
419
|
+
param.typeAnnotation,
|
|
420
|
+
commandAliases,
|
|
421
|
+
`${componentName}_${command.name}`,
|
|
422
|
+
cppCodegenOptions,
|
|
423
|
+
);
|
|
424
|
+
return `${
|
|
425
|
+
param.optional &&
|
|
426
|
+
!commandArgType.alreadySupportsOptionalOrHasDefault
|
|
427
|
+
? `std::optional<${commandArgType.type}>`
|
|
428
|
+
: commandArgType.type
|
|
429
|
+
} ${param.name}`;
|
|
430
|
+
})
|
|
431
|
+
.join(', ');
|
|
432
|
+
|
|
433
|
+
return ` // You must provide an implementation of this method to handle the "${
|
|
434
|
+
command.name
|
|
435
|
+
}" command
|
|
436
|
+
virtual void Handle${capitalizeFirstLetter(
|
|
437
|
+
command.name,
|
|
438
|
+
)}Command(${commandArgs}) noexcept = 0;`;
|
|
439
|
+
})
|
|
440
|
+
.join('\n\n')
|
|
441
|
+
: '';
|
|
442
|
+
|
|
443
|
+
const commandHandler = hasAnyCommands
|
|
444
|
+
? `void HandleCommand(const winrt::Microsoft::ReactNative::ComponentView &view, const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
|
|
338
445
|
auto userData = view.UserData().as<TUserData>();
|
|
339
446
|
auto commandName = args.CommandName();
|
|
340
|
-
${componentShape.commands
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
447
|
+
${componentShape.commands
|
|
448
|
+
.map(command => {
|
|
449
|
+
const commaSeparatedCommandArgs = command.typeAnnotation.params
|
|
450
|
+
.map(param => param.name)
|
|
451
|
+
.join(', ');
|
|
452
|
+
return ` if (commandName == L"${command.name}") {
|
|
453
|
+
${
|
|
454
|
+
command.typeAnnotation.params.length !== 0
|
|
455
|
+
? ` ${command.typeAnnotation.params
|
|
456
|
+
.map(param => {
|
|
457
|
+
const commandArgType = translateCommandParamType(
|
|
458
|
+
param.typeAnnotation,
|
|
459
|
+
commandAliases,
|
|
460
|
+
`${componentName}_${command.name}`,
|
|
461
|
+
cppCodegenOptions,
|
|
462
|
+
);
|
|
463
|
+
return `${
|
|
464
|
+
param.optional &&
|
|
465
|
+
!commandArgType.alreadySupportsOptionalOrHasDefault
|
|
466
|
+
? `std::optional<${commandArgType.type}>`
|
|
467
|
+
: commandArgType.type
|
|
468
|
+
} ${param.name};`;
|
|
469
|
+
})
|
|
470
|
+
.join('\n')}
|
|
471
|
+
winrt::Microsoft::ReactNative::ReadArgs(args.CommandArgs(), ${commaSeparatedCommandArgs});`
|
|
472
|
+
: ''
|
|
473
|
+
}
|
|
474
|
+
userData->Handle${capitalizeFirstLetter(
|
|
475
|
+
command.name,
|
|
476
|
+
)}Command(${commaSeparatedCommandArgs});
|
|
349
477
|
return;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
478
|
+
}`;
|
|
479
|
+
})
|
|
480
|
+
.join('\n\n')}
|
|
481
|
+
}`
|
|
482
|
+
: '';
|
|
483
|
+
|
|
484
|
+
const registerCommandHandler = hasAnyCommands
|
|
485
|
+
? ` builder.SetCustomCommandHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
|
|
355
486
|
const winrt::Microsoft::ReactNative::HandleCommandArgs& args) noexcept {
|
|
356
487
|
auto userData = view.UserData().as<TUserData>();
|
|
357
488
|
userData->HandleCommand(view, args);
|
|
358
|
-
});`
|
|
489
|
+
});`
|
|
490
|
+
: '';
|
|
359
491
|
|
|
360
492
|
const baseType = baseStructTemplate
|
|
361
|
-
|
|
362
|
-
|
|
493
|
+
.replace(/::_COMPONENT_VIEW_COMMAND_HANDLERS_::/g, commandHandlers)
|
|
494
|
+
.replace(/::_COMPONENT_VIEW_COMMAND_HANDLER_::/g, commandHandler);
|
|
363
495
|
|
|
364
496
|
// Registration
|
|
365
|
-
const componentRegistration = registerTemplate.replace(
|
|
497
|
+
const componentRegistration = registerTemplate.replace(
|
|
498
|
+
/::_REGISTER_CUSTOM_COMMAND_HANDLER_::/g,
|
|
499
|
+
registerCommandHandler,
|
|
500
|
+
);
|
|
366
501
|
|
|
367
502
|
// Final output
|
|
368
503
|
const replaceContent = function (template: string): string {
|
|
@@ -380,10 +515,7 @@ ${command.typeAnnotation.params.length !== 0 ? ` ${command.typeAnnotation.p
|
|
|
380
515
|
.replace(/\n\n\n+/g, '\n\n');
|
|
381
516
|
};
|
|
382
517
|
|
|
383
|
-
files.set(
|
|
384
|
-
`${componentName}.g.h`,
|
|
385
|
-
replaceContent(fileTemplate),
|
|
386
|
-
);
|
|
518
|
+
files.set(`${componentName}.g.h`, replaceContent(fileTemplate));
|
|
387
519
|
}
|
|
388
520
|
}
|
|
389
521
|
|
|
@@ -118,11 +118,16 @@ export function createNM2Generator({
|
|
|
118
118
|
});
|
|
119
119
|
let tuples = `
|
|
120
120
|
static constexpr auto methods = std::tuple{
|
|
121
|
-
${methods.traversedPropertyTuples}${
|
|
121
|
+
${methods.traversedPropertyTuples}${
|
|
122
|
+
methods.traversedEventEmitterTuples ? '\n' : ''
|
|
123
|
+
}${methods.traversedEventEmitterTuples}
|
|
122
124
|
};`;
|
|
123
125
|
let checks = `
|
|
124
126
|
constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
|
|
125
|
-
let errors =
|
|
127
|
+
let errors =
|
|
128
|
+
methods.traversedProperties +
|
|
129
|
+
(methods.traversedEventEmitters ? '\n' : '') +
|
|
130
|
+
methods.traversedEventEmitters;
|
|
126
131
|
|
|
127
132
|
// prepare constants
|
|
128
133
|
const constants = generateValidateConstants(nativeModule, aliases);
|
|
@@ -62,11 +62,13 @@ function optionalSign<T>(obj: NamedShape<T>): string {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function translateType(
|
|
65
|
-
type:
|
|
66
|
-
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
type:
|
|
66
|
+
| Nullable<
|
|
67
|
+
| NativeModuleBaseTypeAnnotation
|
|
68
|
+
| NativeModuleParamTypeAnnotation
|
|
69
|
+
| NativeModuleReturnTypeAnnotation
|
|
70
|
+
>
|
|
71
|
+
| UnsafeAnyTypeAnnotation,
|
|
70
72
|
): string {
|
|
71
73
|
// avoid: Property 'type' does not exist on type 'never'
|
|
72
74
|
const returnType = type.type;
|
|
@@ -47,11 +47,13 @@ function translateUnionReturnType(
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
export function translateFieldOrReturnType(
|
|
50
|
-
type:
|
|
51
|
-
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
type:
|
|
51
|
+
| Nullable<
|
|
52
|
+
| NativeModuleBaseTypeAnnotation
|
|
53
|
+
| NativeModuleStringTypeAnnotation
|
|
54
|
+
| NativeModuleFunctionTypeAnnotation
|
|
55
|
+
>
|
|
56
|
+
| UnsafeAnyTypeAnnotation,
|
|
55
57
|
aliases: AliasMap,
|
|
56
58
|
baseAliasName: string,
|
|
57
59
|
callerName: 'translateField' | 'translateReturnType',
|
|
@@ -149,7 +149,9 @@ function translateArray(
|
|
|
149
149
|
function translateEventEmitterArray(
|
|
150
150
|
param: {
|
|
151
151
|
readonly type: 'ArrayTypeAnnotation';
|
|
152
|
-
readonly elementType:
|
|
152
|
+
readonly elementType:
|
|
153
|
+
| NativeModuleEventEmitterBaseTypeAnnotation
|
|
154
|
+
| {type: string};
|
|
153
155
|
},
|
|
154
156
|
aliases: AliasMap,
|
|
155
157
|
baseAliasName: string,
|
|
@@ -254,7 +256,13 @@ function translateEventEmitterParam(
|
|
|
254
256
|
case 'BooleanTypeAnnotation':
|
|
255
257
|
return 'bool';
|
|
256
258
|
case 'ArrayTypeAnnotation':
|
|
257
|
-
return translateEventEmitterArray(
|
|
259
|
+
return translateEventEmitterArray(
|
|
260
|
+
param,
|
|
261
|
+
aliases,
|
|
262
|
+
baseAliasName,
|
|
263
|
+
target,
|
|
264
|
+
options,
|
|
265
|
+
);
|
|
258
266
|
case 'TypeAliasTypeAnnotation':
|
|
259
267
|
return decorateType(getAliasCppName(param.name), target);
|
|
260
268
|
case 'VoidTypeAnnotation':
|
|
@@ -265,7 +273,9 @@ function translateEventEmitterParam(
|
|
|
265
273
|
}
|
|
266
274
|
|
|
267
275
|
function translateNullableParamType(
|
|
268
|
-
paramType:
|
|
276
|
+
paramType:
|
|
277
|
+
| Nullable<NativeModuleParamTypeAnnotation>
|
|
278
|
+
| UnsafeAnyTypeAnnotation,
|
|
269
279
|
aliases: AliasMap,
|
|
270
280
|
baseAliasName: string,
|
|
271
281
|
nullableTarget: ParamTarget,
|
|
@@ -357,14 +367,14 @@ export function translateEventEmitterArgs(
|
|
|
357
367
|
baseAliasName: string,
|
|
358
368
|
options: CppCodegenOptions,
|
|
359
369
|
) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
370
|
+
const translatedParam = translateEventEmitterParam(
|
|
371
|
+
params,
|
|
372
|
+
aliases,
|
|
373
|
+
baseAliasName,
|
|
374
|
+
'spec',
|
|
375
|
+
options,
|
|
376
|
+
);
|
|
377
|
+
return `${translatedParam}`;
|
|
368
378
|
}
|
|
369
379
|
|
|
370
380
|
export function translateArgs(
|
|
@@ -14,7 +14,11 @@ import type {
|
|
|
14
14
|
} from '@react-native/codegen/lib/CodegenSchema';
|
|
15
15
|
import {AliasMap} from './AliasManaging';
|
|
16
16
|
import type {CppCodegenOptions} from './ObjectTypes';
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
translateArgs,
|
|
19
|
+
translateSpecArgs,
|
|
20
|
+
translateEventEmitterArgs,
|
|
21
|
+
} from './ParamTypes';
|
|
18
22
|
import {translateImplReturnType, translateSpecReturnType} from './ReturnTypes';
|
|
19
23
|
|
|
20
24
|
function isMethodSync(funcType: NativeModuleFunctionTypeAnnotation) {
|
|
@@ -92,7 +96,7 @@ function renderProperties(
|
|
|
92
96
|
aliases: AliasMap,
|
|
93
97
|
tuple: boolean,
|
|
94
98
|
options: CppCodegenOptions,
|
|
95
|
-
): {
|
|
99
|
+
): {code: string; numberOfProperties: number} {
|
|
96
100
|
// TODO: generate code for constants
|
|
97
101
|
const properties = methods
|
|
98
102
|
.filter(prop => prop.name !== 'getConstants')
|
|
@@ -154,32 +158,31 @@ function renderProperties(
|
|
|
154
158
|
}
|
|
155
159
|
});
|
|
156
160
|
|
|
157
|
-
|
|
161
|
+
return {code: properties.join('\n'), numberOfProperties: properties.length};
|
|
158
162
|
}
|
|
159
163
|
|
|
160
164
|
function getPossibleEventEmitterSignatures(
|
|
161
165
|
eventEmitter: NativeModuleEventEmitterShape,
|
|
162
166
|
aliases: AliasMap,
|
|
163
|
-
options: CppCodegenOptions
|
|
164
|
-
|
|
167
|
+
options: CppCodegenOptions,
|
|
168
|
+
): string[] {
|
|
165
169
|
const traversedArgs = translateEventEmitterArgs(
|
|
166
170
|
eventEmitter.typeAnnotation.typeAnnotation,
|
|
167
171
|
aliases,
|
|
168
172
|
eventEmitter.name,
|
|
169
173
|
options,
|
|
170
174
|
);
|
|
171
|
-
return [
|
|
175
|
+
return [
|
|
176
|
+
`REACT_EVENT(${eventEmitter.name}) std::function<void(${traversedArgs})> ${eventEmitter.name};`,
|
|
177
|
+
];
|
|
172
178
|
}
|
|
173
179
|
|
|
174
180
|
function translatePossibleEventSignatures(
|
|
175
181
|
eventEmitter: NativeModuleEventEmitterShape,
|
|
176
182
|
aliases: AliasMap,
|
|
177
|
-
options: CppCodegenOptions
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
aliases,
|
|
181
|
-
options
|
|
182
|
-
)
|
|
183
|
+
options: CppCodegenOptions,
|
|
184
|
+
): string {
|
|
185
|
+
return getPossibleEventEmitterSignatures(eventEmitter, aliases, options)
|
|
183
186
|
.map(sig => `" ${sig}\\n"`)
|
|
184
187
|
.join('\n ');
|
|
185
188
|
}
|
|
@@ -201,7 +204,9 @@ function renderEventEmitters(
|
|
|
201
204
|
);
|
|
202
205
|
|
|
203
206
|
if (tuple) {
|
|
204
|
-
return ` EventEmitter<void(${traversedArgs})>{${
|
|
207
|
+
return ` EventEmitter<void(${traversedArgs})>{${
|
|
208
|
+
index + indexOffset
|
|
209
|
+
}, L"${eventEmitter.name}"},`;
|
|
205
210
|
} else {
|
|
206
211
|
return ` REACT_SHOW_EVENTEMITTER_SPEC_ERRORS(
|
|
207
212
|
${index + indexOffset},
|
|
@@ -210,7 +215,7 @@ function renderEventEmitters(
|
|
|
210
215
|
eventEmitter,
|
|
211
216
|
aliases,
|
|
212
217
|
options,
|
|
213
|
-
|
|
218
|
+
)});`;
|
|
214
219
|
}
|
|
215
220
|
})
|
|
216
221
|
.join('\n');
|
|
@@ -221,10 +226,10 @@ export function generateValidateMethods(
|
|
|
221
226
|
aliases: AliasMap,
|
|
222
227
|
options: CppCodegenOptions,
|
|
223
228
|
): {
|
|
224
|
-
traversedProperties: string
|
|
225
|
-
traversedEventEmitters: string
|
|
226
|
-
traversedPropertyTuples: string
|
|
227
|
-
traversedEventEmitterTuples: string
|
|
229
|
+
traversedProperties: string;
|
|
230
|
+
traversedEventEmitters: string;
|
|
231
|
+
traversedPropertyTuples: string;
|
|
232
|
+
traversedEventEmitterTuples: string;
|
|
228
233
|
} {
|
|
229
234
|
const methods = nativeModule.spec.methods;
|
|
230
235
|
const eventEmitters = nativeModule.spec.eventEmitters;
|
|
@@ -254,5 +259,10 @@ export function generateValidateMethods(
|
|
|
254
259
|
true,
|
|
255
260
|
options,
|
|
256
261
|
);
|
|
257
|
-
return {
|
|
262
|
+
return {
|
|
263
|
+
traversedPropertyTuples: traversedPropertyTuples.code,
|
|
264
|
+
traversedEventEmitterTuples,
|
|
265
|
+
traversedProperties: traversedProperties.code,
|
|
266
|
+
traversedEventEmitters,
|
|
267
|
+
};
|
|
258
268
|
}
|