@react-native/codegen 0.84.0-nightly-20251210-3e9083b42 → 0.84.0-nightly-20251212-dd390dbbe
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/lib/CodegenSchema.js.flow +4 -0
- package/lib/generators/components/GeneratePropsCpp.js +84 -5
- package/lib/generators/components/GeneratePropsCpp.js.flow +84 -5
- package/lib/generators/components/GeneratePropsH.js +69 -7
- package/lib/generators/components/GeneratePropsH.js.flow +54 -4
- package/package.json +1 -1
|
@@ -128,6 +128,10 @@ export type OptionsShape = $ReadOnly<{
|
|
|
128
128
|
// Use for components currently being renamed in paper
|
|
129
129
|
// Will use new name if it is available and fallback to this name
|
|
130
130
|
paperComponentNameDeprecated?: string,
|
|
131
|
+
// Use to generate C++ Props with optional types for properties defined as optional
|
|
132
|
+
generateOptionalProperties?: boolean,
|
|
133
|
+
// Use to generate C++ Props with optional types for object properties defined as optional
|
|
134
|
+
generateOptionalObjectProperties?: boolean,
|
|
131
135
|
}>;
|
|
132
136
|
|
|
133
137
|
export type ExtendsPropsShape = $ReadOnly<{
|
|
@@ -59,6 +59,7 @@ function generatePropsDiffString(
|
|
|
59
59
|
component,
|
|
60
60
|
debugProps = '',
|
|
61
61
|
includeGetDebugPropsImplementation = false,
|
|
62
|
+
generateOptionalProperties = false,
|
|
62
63
|
) {
|
|
63
64
|
const diffProps = component.props
|
|
64
65
|
.map(prop => {
|
|
@@ -68,39 +69,116 @@ function generatePropsDiffString(
|
|
|
68
69
|
case 'Int32TypeAnnotation':
|
|
69
70
|
case 'BooleanTypeAnnotation':
|
|
70
71
|
case 'MixedTypeAnnotation':
|
|
71
|
-
|
|
72
|
+
if (
|
|
73
|
+
prop.optional &&
|
|
74
|
+
prop.typeAnnotation.default == null &&
|
|
75
|
+
generateOptionalProperties
|
|
76
|
+
) {
|
|
77
|
+
return `
|
|
78
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
79
|
+
if (${prop.name}.has_value()) {
|
|
80
|
+
result["${prop.name}"] = ${prop.name}.value();
|
|
81
|
+
} else {
|
|
82
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
83
|
+
}
|
|
84
|
+
}`;
|
|
85
|
+
} else {
|
|
86
|
+
return `
|
|
72
87
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
73
88
|
result["${prop.name}"] = ${prop.name};
|
|
74
89
|
}`;
|
|
90
|
+
}
|
|
75
91
|
case 'DoubleTypeAnnotation':
|
|
76
92
|
case 'FloatTypeAnnotation':
|
|
77
|
-
|
|
93
|
+
if (
|
|
94
|
+
prop.optional &&
|
|
95
|
+
prop.typeAnnotation.default == null &&
|
|
96
|
+
generateOptionalProperties
|
|
97
|
+
) {
|
|
98
|
+
return `
|
|
99
|
+
if ((${prop.name} != oldProps->${prop.name})) {
|
|
100
|
+
if (${prop.name}.has_value()) {
|
|
101
|
+
if (!oldProps->${prop.name}.has_value() || !(std::isnan(${prop.name}.value()) && std::isnan(oldProps->${prop.name}.value()))) {
|
|
102
|
+
result["${prop.name}"] = ${prop.name}.value();
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
106
|
+
}
|
|
107
|
+
}`;
|
|
108
|
+
} else {
|
|
109
|
+
return `
|
|
78
110
|
if ((${prop.name} != oldProps->${prop.name}) && !(std::isnan(${prop.name}) && std::isnan(oldProps->${prop.name}))) {
|
|
79
111
|
result["${prop.name}"] = ${prop.name};
|
|
80
112
|
}`;
|
|
113
|
+
}
|
|
81
114
|
case 'ArrayTypeAnnotation':
|
|
82
115
|
case 'ObjectTypeAnnotation':
|
|
83
116
|
case 'StringEnumTypeAnnotation':
|
|
84
117
|
case 'Int32EnumTypeAnnotation':
|
|
85
|
-
|
|
118
|
+
if (
|
|
119
|
+
prop.optional &&
|
|
120
|
+
prop.typeAnnotation.default == null &&
|
|
121
|
+
generateOptionalProperties
|
|
122
|
+
) {
|
|
123
|
+
return `
|
|
124
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
125
|
+
if (${prop.name}.has_value()) {
|
|
126
|
+
result["${prop.name}"] = toDynamic(${prop.name}.value());
|
|
127
|
+
} else {
|
|
128
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
129
|
+
}
|
|
130
|
+
}`;
|
|
131
|
+
} else {
|
|
132
|
+
return `
|
|
86
133
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
87
134
|
result["${prop.name}"] = toDynamic(${prop.name});
|
|
88
135
|
}`;
|
|
136
|
+
}
|
|
89
137
|
case 'ReservedPropTypeAnnotation':
|
|
90
138
|
switch (typeAnnotation.name) {
|
|
91
139
|
case 'ColorPrimitive':
|
|
92
|
-
|
|
140
|
+
if (
|
|
141
|
+
prop.optional &&
|
|
142
|
+
prop.typeAnnotation.default == null &&
|
|
143
|
+
generateOptionalProperties
|
|
144
|
+
) {
|
|
145
|
+
return `
|
|
146
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
147
|
+
if (${prop.name}.has_value()) {
|
|
148
|
+
result["${prop.name}"] = *${prop.name}.value();
|
|
149
|
+
} else {
|
|
150
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
151
|
+
}
|
|
152
|
+
}`;
|
|
153
|
+
} else {
|
|
154
|
+
return `
|
|
93
155
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
94
156
|
result["${prop.name}"] = *${prop.name};
|
|
95
157
|
}`;
|
|
158
|
+
}
|
|
96
159
|
case 'ImageSourcePrimitive':
|
|
97
160
|
case 'PointPrimitive':
|
|
98
161
|
case 'EdgeInsetsPrimitive':
|
|
99
162
|
case 'DimensionPrimitive':
|
|
100
|
-
|
|
163
|
+
if (
|
|
164
|
+
prop.optional &&
|
|
165
|
+
prop.typeAnnotation.default == null &&
|
|
166
|
+
generateOptionalProperties
|
|
167
|
+
) {
|
|
168
|
+
return `
|
|
169
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
170
|
+
if (${prop.name}.has_value()) {
|
|
171
|
+
result["${prop.name}"] = toDynamic(${prop.name}.value());
|
|
172
|
+
} else {
|
|
173
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
174
|
+
}
|
|
175
|
+
}`;
|
|
176
|
+
} else {
|
|
177
|
+
return `
|
|
101
178
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
102
179
|
result["${prop.name}"] = toDynamic(${prop.name});
|
|
103
180
|
}`;
|
|
181
|
+
}
|
|
104
182
|
case 'ImageRequestPrimitive':
|
|
105
183
|
// Shouldn't be used in props
|
|
106
184
|
throw new Error(
|
|
@@ -239,6 +317,7 @@ module.exports = {
|
|
|
239
317
|
component,
|
|
240
318
|
debugProps,
|
|
241
319
|
includeGetDebugPropsImplementation,
|
|
320
|
+
component.generateOptionalProperties,
|
|
242
321
|
);
|
|
243
322
|
const imports = getImports(component.props);
|
|
244
323
|
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
|
|
@@ -79,6 +79,7 @@ function generatePropsDiffString(
|
|
|
79
79
|
component: ComponentShape,
|
|
80
80
|
debugProps: string = '',
|
|
81
81
|
includeGetDebugPropsImplementation?: boolean = false,
|
|
82
|
+
generateOptionalProperties?: boolean = false,
|
|
82
83
|
) {
|
|
83
84
|
const diffProps = component.props
|
|
84
85
|
.map(prop => {
|
|
@@ -88,39 +89,116 @@ function generatePropsDiffString(
|
|
|
88
89
|
case 'Int32TypeAnnotation':
|
|
89
90
|
case 'BooleanTypeAnnotation':
|
|
90
91
|
case 'MixedTypeAnnotation':
|
|
91
|
-
|
|
92
|
+
if (
|
|
93
|
+
prop.optional &&
|
|
94
|
+
prop.typeAnnotation.default == null &&
|
|
95
|
+
generateOptionalProperties
|
|
96
|
+
) {
|
|
97
|
+
return `
|
|
98
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
99
|
+
if (${prop.name}.has_value()) {
|
|
100
|
+
result["${prop.name}"] = ${prop.name}.value();
|
|
101
|
+
} else {
|
|
102
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
103
|
+
}
|
|
104
|
+
}`;
|
|
105
|
+
} else {
|
|
106
|
+
return `
|
|
92
107
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
93
108
|
result["${prop.name}"] = ${prop.name};
|
|
94
109
|
}`;
|
|
110
|
+
}
|
|
95
111
|
case 'DoubleTypeAnnotation':
|
|
96
112
|
case 'FloatTypeAnnotation':
|
|
97
|
-
|
|
113
|
+
if (
|
|
114
|
+
prop.optional &&
|
|
115
|
+
prop.typeAnnotation.default == null &&
|
|
116
|
+
generateOptionalProperties
|
|
117
|
+
) {
|
|
118
|
+
return `
|
|
119
|
+
if ((${prop.name} != oldProps->${prop.name})) {
|
|
120
|
+
if (${prop.name}.has_value()) {
|
|
121
|
+
if (!oldProps->${prop.name}.has_value() || !(std::isnan(${prop.name}.value()) && std::isnan(oldProps->${prop.name}.value()))) {
|
|
122
|
+
result["${prop.name}"] = ${prop.name}.value();
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
126
|
+
}
|
|
127
|
+
}`;
|
|
128
|
+
} else {
|
|
129
|
+
return `
|
|
98
130
|
if ((${prop.name} != oldProps->${prop.name}) && !(std::isnan(${prop.name}) && std::isnan(oldProps->${prop.name}))) {
|
|
99
131
|
result["${prop.name}"] = ${prop.name};
|
|
100
132
|
}`;
|
|
133
|
+
}
|
|
101
134
|
case 'ArrayTypeAnnotation':
|
|
102
135
|
case 'ObjectTypeAnnotation':
|
|
103
136
|
case 'StringEnumTypeAnnotation':
|
|
104
137
|
case 'Int32EnumTypeAnnotation':
|
|
105
|
-
|
|
138
|
+
if (
|
|
139
|
+
prop.optional &&
|
|
140
|
+
prop.typeAnnotation.default == null &&
|
|
141
|
+
generateOptionalProperties
|
|
142
|
+
) {
|
|
143
|
+
return `
|
|
144
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
145
|
+
if (${prop.name}.has_value()) {
|
|
146
|
+
result["${prop.name}"] = toDynamic(${prop.name}.value());
|
|
147
|
+
} else {
|
|
148
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
149
|
+
}
|
|
150
|
+
}`;
|
|
151
|
+
} else {
|
|
152
|
+
return `
|
|
106
153
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
107
154
|
result["${prop.name}"] = toDynamic(${prop.name});
|
|
108
155
|
}`;
|
|
156
|
+
}
|
|
109
157
|
case 'ReservedPropTypeAnnotation':
|
|
110
158
|
switch (typeAnnotation.name) {
|
|
111
159
|
case 'ColorPrimitive':
|
|
112
|
-
|
|
160
|
+
if (
|
|
161
|
+
prop.optional &&
|
|
162
|
+
prop.typeAnnotation.default == null &&
|
|
163
|
+
generateOptionalProperties
|
|
164
|
+
) {
|
|
165
|
+
return `
|
|
166
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
167
|
+
if (${prop.name}.has_value()) {
|
|
168
|
+
result["${prop.name}"] = *${prop.name}.value();
|
|
169
|
+
} else {
|
|
170
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
171
|
+
}
|
|
172
|
+
}`;
|
|
173
|
+
} else {
|
|
174
|
+
return `
|
|
113
175
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
114
176
|
result["${prop.name}"] = *${prop.name};
|
|
115
177
|
}`;
|
|
178
|
+
}
|
|
116
179
|
case 'ImageSourcePrimitive':
|
|
117
180
|
case 'PointPrimitive':
|
|
118
181
|
case 'EdgeInsetsPrimitive':
|
|
119
182
|
case 'DimensionPrimitive':
|
|
120
|
-
|
|
183
|
+
if (
|
|
184
|
+
prop.optional &&
|
|
185
|
+
prop.typeAnnotation.default == null &&
|
|
186
|
+
generateOptionalProperties
|
|
187
|
+
) {
|
|
188
|
+
return `
|
|
189
|
+
if (${prop.name} != oldProps->${prop.name}) {
|
|
190
|
+
if (${prop.name}.has_value()) {
|
|
191
|
+
result["${prop.name}"] = toDynamic(${prop.name}.value());
|
|
192
|
+
} else {
|
|
193
|
+
result["${prop.name}"] = folly::dynamic(nullptr);
|
|
194
|
+
}
|
|
195
|
+
}`;
|
|
196
|
+
} else {
|
|
197
|
+
return `
|
|
121
198
|
if (${prop.name} != oldProps->${prop.name}) {
|
|
122
199
|
result["${prop.name}"] = toDynamic(${prop.name});
|
|
123
200
|
}`;
|
|
201
|
+
}
|
|
124
202
|
case 'ImageRequestPrimitive':
|
|
125
203
|
// Shouldn't be used in props
|
|
126
204
|
throw new Error(
|
|
@@ -275,6 +353,7 @@ module.exports = {
|
|
|
275
353
|
component,
|
|
276
354
|
debugProps,
|
|
277
355
|
includeGetDebugPropsImplementation,
|
|
356
|
+
component.generateOptionalProperties,
|
|
278
357
|
);
|
|
279
358
|
|
|
280
359
|
const imports = getImports(component.props);
|
|
@@ -402,7 +402,12 @@ function generateEnumString(componentName, component) {
|
|
|
402
402
|
.filter(Boolean)
|
|
403
403
|
.join('\n');
|
|
404
404
|
}
|
|
405
|
-
function generatePropsString(
|
|
405
|
+
function generatePropsString(
|
|
406
|
+
componentName,
|
|
407
|
+
props,
|
|
408
|
+
nameParts,
|
|
409
|
+
generateOptionalProperties = false,
|
|
410
|
+
) {
|
|
406
411
|
return props
|
|
407
412
|
.map(prop => {
|
|
408
413
|
const nativeType = getNativeTypeFromAnnotation(
|
|
@@ -414,6 +419,13 @@ function generatePropsString(componentName, props, nameParts) {
|
|
|
414
419
|
componentName,
|
|
415
420
|
prop,
|
|
416
421
|
);
|
|
422
|
+
if (
|
|
423
|
+
prop.optional &&
|
|
424
|
+
prop.typeAnnotation.default == null &&
|
|
425
|
+
generateOptionalProperties
|
|
426
|
+
) {
|
|
427
|
+
return `std::optional<${nativeType}> ${prop.name}${defaultInitializer};`;
|
|
428
|
+
}
|
|
417
429
|
return `${nativeType} ${prop.name}${defaultInitializer};`;
|
|
418
430
|
})
|
|
419
431
|
.join('\n' + ' ');
|
|
@@ -443,14 +455,24 @@ function getExtendsImports(extendsProps) {
|
|
|
443
455
|
return imports;
|
|
444
456
|
}
|
|
445
457
|
function generateStructsForComponent(componentName, component) {
|
|
446
|
-
const structs = generateStructs(
|
|
458
|
+
const structs = generateStructs(
|
|
459
|
+
componentName,
|
|
460
|
+
component.props,
|
|
461
|
+
[],
|
|
462
|
+
component.generateOptionalObjectProperties,
|
|
463
|
+
);
|
|
447
464
|
const structArray = Array.from(structs.values());
|
|
448
465
|
if (structArray.length < 1) {
|
|
449
466
|
return '';
|
|
450
467
|
}
|
|
451
468
|
return structArray.join('\n\n');
|
|
452
469
|
}
|
|
453
|
-
function generateStructs(
|
|
470
|
+
function generateStructs(
|
|
471
|
+
componentName,
|
|
472
|
+
properties,
|
|
473
|
+
nameParts,
|
|
474
|
+
generateOptionalObjectProperties = false,
|
|
475
|
+
) {
|
|
454
476
|
const structs = new Map();
|
|
455
477
|
properties.forEach(prop => {
|
|
456
478
|
const typeAnnotation = prop.typeAnnotation;
|
|
@@ -462,6 +484,7 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
462
484
|
componentName,
|
|
463
485
|
elementProperties,
|
|
464
486
|
nameParts.concat([prop.name]),
|
|
487
|
+
generateOptionalObjectProperties,
|
|
465
488
|
);
|
|
466
489
|
nestedStructs.forEach(function (value, key) {
|
|
467
490
|
structs.set(key, value);
|
|
@@ -471,6 +494,7 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
471
494
|
componentName,
|
|
472
495
|
nameParts.concat([prop.name]),
|
|
473
496
|
typeAnnotation.properties,
|
|
497
|
+
generateOptionalObjectProperties,
|
|
474
498
|
);
|
|
475
499
|
}
|
|
476
500
|
if (
|
|
@@ -484,6 +508,7 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
484
508
|
componentName,
|
|
485
509
|
elementProperties,
|
|
486
510
|
nameParts.concat([prop.name]),
|
|
511
|
+
generateOptionalObjectProperties,
|
|
487
512
|
);
|
|
488
513
|
nestedStructs.forEach(function (value, key) {
|
|
489
514
|
structs.set(key, value);
|
|
@@ -495,6 +520,7 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
495
520
|
componentName,
|
|
496
521
|
nameParts.concat([prop.name]),
|
|
497
522
|
elementProperties,
|
|
523
|
+
generateOptionalObjectProperties,
|
|
498
524
|
);
|
|
499
525
|
|
|
500
526
|
// Generate the conversion function for std:vector<Object>.
|
|
@@ -523,6 +549,7 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
523
549
|
componentName,
|
|
524
550
|
elementProperties,
|
|
525
551
|
nameParts.concat([prop.name]),
|
|
552
|
+
generateOptionalObjectProperties,
|
|
526
553
|
);
|
|
527
554
|
nestedStructs.forEach(function (value, key) {
|
|
528
555
|
structs.set(key, value);
|
|
@@ -534,6 +561,7 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
534
561
|
componentName,
|
|
535
562
|
nameParts.concat([prop.name]),
|
|
536
563
|
elementProperties,
|
|
564
|
+
generateOptionalObjectProperties,
|
|
537
565
|
);
|
|
538
566
|
|
|
539
567
|
// Generate the conversion function for std:vector<Object>.
|
|
@@ -551,13 +579,20 @@ function generateStructs(componentName, properties, nameParts) {
|
|
|
551
579
|
});
|
|
552
580
|
return structs;
|
|
553
581
|
}
|
|
554
|
-
function generateStruct(
|
|
582
|
+
function generateStruct(
|
|
583
|
+
structs,
|
|
584
|
+
componentName,
|
|
585
|
+
nameParts,
|
|
586
|
+
properties,
|
|
587
|
+
generateOptionalObjectProperties = false,
|
|
588
|
+
) {
|
|
555
589
|
const structNameParts = nameParts;
|
|
556
590
|
const structName = generateStructName(componentName, structNameParts);
|
|
557
591
|
const fields = generatePropsString(
|
|
558
592
|
componentName,
|
|
559
593
|
properties,
|
|
560
594
|
structNameParts,
|
|
595
|
+
generateOptionalObjectProperties,
|
|
561
596
|
);
|
|
562
597
|
properties.forEach(property => {
|
|
563
598
|
const name = property.name;
|
|
@@ -587,7 +622,13 @@ function generateStruct(structs, componentName, nameParts, properties) {
|
|
|
587
622
|
`Properties are expected for ObjectTypeAnnotation (see ${name} in ${componentName})`,
|
|
588
623
|
);
|
|
589
624
|
}
|
|
590
|
-
generateStruct(
|
|
625
|
+
generateStruct(
|
|
626
|
+
structs,
|
|
627
|
+
componentName,
|
|
628
|
+
nameParts.concat([name]),
|
|
629
|
+
props,
|
|
630
|
+
generateOptionalObjectProperties,
|
|
631
|
+
);
|
|
591
632
|
return;
|
|
592
633
|
case 'MixedTypeAnnotation':
|
|
593
634
|
return;
|
|
@@ -617,9 +658,29 @@ function generateStruct(structs, componentName, nameParts, properties) {
|
|
|
617
658
|
case 'DoubleTypeAnnotation':
|
|
618
659
|
case 'FloatTypeAnnotation':
|
|
619
660
|
case 'MixedTypeAnnotation':
|
|
620
|
-
|
|
661
|
+
if (
|
|
662
|
+
property.optional &&
|
|
663
|
+
property.typeAnnotation.default == null &&
|
|
664
|
+
generateOptionalObjectProperties
|
|
665
|
+
) {
|
|
666
|
+
return `if (${name}.has_value()) {
|
|
667
|
+
result["${name}"] = ${name}.value();
|
|
668
|
+
}`;
|
|
669
|
+
} else {
|
|
670
|
+
return `result["${name}"] = ${name};`;
|
|
671
|
+
}
|
|
621
672
|
default:
|
|
622
|
-
|
|
673
|
+
if (
|
|
674
|
+
property.optional &&
|
|
675
|
+
property.typeAnnotation.default == null &&
|
|
676
|
+
generateOptionalObjectProperties
|
|
677
|
+
) {
|
|
678
|
+
return `if (${name}.has_value()) {
|
|
679
|
+
result["${name}"] = ::facebook::react::toDynamic(${name}.value());
|
|
680
|
+
}`;
|
|
681
|
+
} else {
|
|
682
|
+
return `result["${name}"] = ::facebook::react::toDynamic(${name});`;
|
|
683
|
+
}
|
|
623
684
|
}
|
|
624
685
|
})
|
|
625
686
|
.join('\n ');
|
|
@@ -668,6 +729,7 @@ module.exports = {
|
|
|
668
729
|
componentName,
|
|
669
730
|
component.props,
|
|
670
731
|
[],
|
|
732
|
+
component.generateOptionalProperties,
|
|
671
733
|
);
|
|
672
734
|
const extendString = getClassExtendString(component);
|
|
673
735
|
const extendsImports = getExtendsImports(component.extendsProps);
|
|
@@ -529,6 +529,7 @@ function generatePropsString(
|
|
|
529
529
|
componentName: string,
|
|
530
530
|
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
531
531
|
nameParts: $ReadOnlyArray<string>,
|
|
532
|
+
generateOptionalProperties?: boolean = false,
|
|
532
533
|
) {
|
|
533
534
|
return props
|
|
534
535
|
.map(prop => {
|
|
@@ -542,6 +543,14 @@ function generatePropsString(
|
|
|
542
543
|
prop,
|
|
543
544
|
);
|
|
544
545
|
|
|
546
|
+
if (
|
|
547
|
+
prop.optional &&
|
|
548
|
+
prop.typeAnnotation.default == null &&
|
|
549
|
+
generateOptionalProperties
|
|
550
|
+
) {
|
|
551
|
+
return `std::optional<${nativeType}> ${prop.name}${defaultInitializer};`;
|
|
552
|
+
}
|
|
553
|
+
|
|
545
554
|
return `${nativeType} ${prop.name}${defaultInitializer};`;
|
|
546
555
|
})
|
|
547
556
|
.join('\n' + ' ');
|
|
@@ -581,7 +590,12 @@ function generateStructsForComponent(
|
|
|
581
590
|
componentName: string,
|
|
582
591
|
component: ComponentShape,
|
|
583
592
|
): string {
|
|
584
|
-
const structs = generateStructs(
|
|
593
|
+
const structs = generateStructs(
|
|
594
|
+
componentName,
|
|
595
|
+
component.props,
|
|
596
|
+
[],
|
|
597
|
+
component.generateOptionalObjectProperties,
|
|
598
|
+
);
|
|
585
599
|
const structArray = Array.from(structs.values());
|
|
586
600
|
if (structArray.length < 1) {
|
|
587
601
|
return '';
|
|
@@ -593,6 +607,7 @@ function generateStructs(
|
|
|
593
607
|
componentName: string,
|
|
594
608
|
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
595
609
|
nameParts: Array<string>,
|
|
610
|
+
generateOptionalObjectProperties?: boolean = false,
|
|
596
611
|
): StructsMap {
|
|
597
612
|
const structs: StructsMap = new Map();
|
|
598
613
|
properties.forEach(prop => {
|
|
@@ -605,6 +620,7 @@ function generateStructs(
|
|
|
605
620
|
componentName,
|
|
606
621
|
elementProperties,
|
|
607
622
|
nameParts.concat([prop.name]),
|
|
623
|
+
generateOptionalObjectProperties,
|
|
608
624
|
);
|
|
609
625
|
nestedStructs.forEach(function (value, key) {
|
|
610
626
|
structs.set(key, value);
|
|
@@ -615,6 +631,7 @@ function generateStructs(
|
|
|
615
631
|
componentName,
|
|
616
632
|
nameParts.concat([prop.name]),
|
|
617
633
|
typeAnnotation.properties,
|
|
634
|
+
generateOptionalObjectProperties,
|
|
618
635
|
);
|
|
619
636
|
}
|
|
620
637
|
|
|
@@ -629,6 +646,7 @@ function generateStructs(
|
|
|
629
646
|
componentName,
|
|
630
647
|
elementProperties,
|
|
631
648
|
nameParts.concat([prop.name]),
|
|
649
|
+
generateOptionalObjectProperties,
|
|
632
650
|
);
|
|
633
651
|
nestedStructs.forEach(function (value, key) {
|
|
634
652
|
structs.set(key, value);
|
|
@@ -640,6 +658,7 @@ function generateStructs(
|
|
|
640
658
|
componentName,
|
|
641
659
|
nameParts.concat([prop.name]),
|
|
642
660
|
elementProperties,
|
|
661
|
+
generateOptionalObjectProperties,
|
|
643
662
|
);
|
|
644
663
|
|
|
645
664
|
// Generate the conversion function for std:vector<Object>.
|
|
@@ -670,6 +689,7 @@ function generateStructs(
|
|
|
670
689
|
componentName,
|
|
671
690
|
elementProperties,
|
|
672
691
|
nameParts.concat([prop.name]),
|
|
692
|
+
generateOptionalObjectProperties,
|
|
673
693
|
);
|
|
674
694
|
nestedStructs.forEach(function (value, key) {
|
|
675
695
|
structs.set(key, value);
|
|
@@ -681,6 +701,7 @@ function generateStructs(
|
|
|
681
701
|
componentName,
|
|
682
702
|
nameParts.concat([prop.name]),
|
|
683
703
|
elementProperties,
|
|
704
|
+
generateOptionalObjectProperties,
|
|
684
705
|
);
|
|
685
706
|
|
|
686
707
|
// Generate the conversion function for std:vector<Object>.
|
|
@@ -707,6 +728,7 @@ function generateStruct(
|
|
|
707
728
|
componentName: string,
|
|
708
729
|
nameParts: $ReadOnlyArray<string>,
|
|
709
730
|
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
|
|
731
|
+
generateOptionalObjectProperties?: boolean = false,
|
|
710
732
|
): void {
|
|
711
733
|
const structNameParts = nameParts;
|
|
712
734
|
const structName = generateStructName(componentName, structNameParts);
|
|
@@ -714,6 +736,7 @@ function generateStruct(
|
|
|
714
736
|
componentName,
|
|
715
737
|
properties,
|
|
716
738
|
structNameParts,
|
|
739
|
+
generateOptionalObjectProperties,
|
|
717
740
|
);
|
|
718
741
|
|
|
719
742
|
properties.forEach((property: NamedShape<PropTypeAnnotation>) => {
|
|
@@ -744,7 +767,13 @@ function generateStruct(
|
|
|
744
767
|
`Properties are expected for ObjectTypeAnnotation (see ${name} in ${componentName})`,
|
|
745
768
|
);
|
|
746
769
|
}
|
|
747
|
-
generateStruct(
|
|
770
|
+
generateStruct(
|
|
771
|
+
structs,
|
|
772
|
+
componentName,
|
|
773
|
+
nameParts.concat([name]),
|
|
774
|
+
props,
|
|
775
|
+
generateOptionalObjectProperties,
|
|
776
|
+
);
|
|
748
777
|
return;
|
|
749
778
|
case 'MixedTypeAnnotation':
|
|
750
779
|
return;
|
|
@@ -776,9 +805,29 @@ function generateStruct(
|
|
|
776
805
|
case 'DoubleTypeAnnotation':
|
|
777
806
|
case 'FloatTypeAnnotation':
|
|
778
807
|
case 'MixedTypeAnnotation':
|
|
779
|
-
|
|
808
|
+
if (
|
|
809
|
+
property.optional &&
|
|
810
|
+
property.typeAnnotation.default == null &&
|
|
811
|
+
generateOptionalObjectProperties
|
|
812
|
+
) {
|
|
813
|
+
return `if (${name}.has_value()) {
|
|
814
|
+
result["${name}"] = ${name}.value();
|
|
815
|
+
}`;
|
|
816
|
+
} else {
|
|
817
|
+
return `result["${name}"] = ${name};`;
|
|
818
|
+
}
|
|
780
819
|
default:
|
|
781
|
-
|
|
820
|
+
if (
|
|
821
|
+
property.optional &&
|
|
822
|
+
property.typeAnnotation.default == null &&
|
|
823
|
+
generateOptionalObjectProperties
|
|
824
|
+
) {
|
|
825
|
+
return `if (${name}.has_value()) {
|
|
826
|
+
result["${name}"] = ::facebook::react::toDynamic(${name}.value());
|
|
827
|
+
}`;
|
|
828
|
+
} else {
|
|
829
|
+
return `result["${name}"] = ::facebook::react::toDynamic(${name});`;
|
|
830
|
+
}
|
|
782
831
|
}
|
|
783
832
|
})
|
|
784
833
|
.join('\n ');
|
|
@@ -834,6 +883,7 @@ module.exports = {
|
|
|
834
883
|
componentName,
|
|
835
884
|
component.props,
|
|
836
885
|
[],
|
|
886
|
+
component.generateOptionalProperties,
|
|
837
887
|
);
|
|
838
888
|
const extendString = getClassExtendString(component);
|
|
839
889
|
const extendsImports = getExtendsImports(component.extendsProps);
|