@@ -590,7 +590,12 @@ function generateStructsForComponent(
590590 componentName : string ,
591591 component : ComponentShape ,
592592) : string {
593- const structs = generateStructs ( componentName , component . props , [ ] ) ;
593+ const structs = generateStructs (
594+ componentName ,
595+ component . props ,
596+ [ ] ,
597+ component . generateOptionalObjectProperties ,
598+ ) ;
594599 const structArray = Array . from ( structs . values ( ) ) ;
595600 if ( structArray . length < 1 ) {
596601 return '';
@@ -602,6 +607,7 @@ function generateStructs(
602607 componentName : string ,
603608 properties : $ReadOnlyArray < NamedShape < PropTypeAnnotation >> ,
604609 nameParts : Array < string > ,
610+ generateOptionalObjectProperties ?: boolean = false ,
605611) : StructsMap {
606612 const structs : StructsMap = new Map ( ) ;
607613 properties . forEach ( prop => {
@@ -614,6 +620,7 @@ function generateStructs(
614620 componentName ,
615621 elementProperties ,
616622 nameParts . concat ( [ prop . name ] ) ,
623+ generateOptionalObjectProperties ,
617624 ) ;
618625 nestedStructs . forEach ( function ( value , key ) {
619626 structs . set ( key , value ) ;
@@ -624,6 +631,7 @@ function generateStructs(
624631 componentName ,
625632 nameParts . concat ( [ prop . name ] ) ,
626633 typeAnnotation . properties ,
634+ generateOptionalObjectProperties ,
627635 ) ;
628636 }
629637
@@ -638,6 +646,7 @@ function generateStructs(
638646 componentName ,
639647 elementProperties ,
640648 nameParts . concat ( [ prop . name ] ) ,
649+ generateOptionalObjectProperties ,
641650 ) ;
642651 nestedStructs . forEach ( function ( value , key ) {
643652 structs . set ( key , value ) ;
@@ -649,6 +658,7 @@ function generateStructs(
649658 componentName ,
650659 nameParts . concat ( [ prop . name ] ) ,
651660 elementProperties ,
661+ generateOptionalObjectProperties ,
652662 ) ;
653663
654664 // Generate the conversion function for std:vector<Object>.
@@ -679,6 +689,7 @@ function generateStructs(
679689 componentName ,
680690 elementProperties ,
681691 nameParts . concat ( [ prop . name ] ) ,
692+ generateOptionalObjectProperties ,
682693 ) ;
683694 nestedStructs . forEach ( function ( value , key ) {
684695 structs . set ( key , value ) ;
@@ -690,6 +701,7 @@ function generateStructs(
690701 componentName ,
691702 nameParts . concat ( [ prop . name ] ) ,
692703 elementProperties ,
704+ generateOptionalObjectProperties ,
693705 ) ;
694706
695707 // Generate the conversion function for std:vector<Object>.
@@ -716,13 +728,15 @@ function generateStruct(
716728 componentName : string ,
717729 nameParts : $ReadOnlyArray < string > ,
718730 properties : $ReadOnlyArray < NamedShape < PropTypeAnnotation >> ,
731+ generateOptionalObjectProperties ? : boolean = false ,
719732) : void {
720733 const structNameParts = nameParts ;
721734 const structName = generateStructName ( componentName , structNameParts ) ;
722735 const fields = generatePropsString (
723736 componentName ,
724737 properties ,
725738 structNameParts ,
739+ generateOptionalObjectProperties ,
726740 ) ;
727741
728742 properties . forEach ( ( property : NamedShape < PropTypeAnnotation > ) => {
@@ -753,7 +767,13 @@ function generateStruct(
753767 `Properties are expected for ObjectTypeAnnotation (see ${ name } in ${ componentName } )` ,
754768 ) ;
755769 }
756- generateStruct ( structs , componentName , nameParts . concat ( [ name ] ) , props ) ;
770+ generateStruct (
771+ structs ,
772+ componentName ,
773+ nameParts . concat ( [ name ] ) ,
774+ props ,
775+ generateOptionalObjectProperties ,
776+ ) ;
757777 return ;
758778 case 'MixedTypeAnnotation' :
759779 return ;
@@ -785,9 +805,29 @@ function generateStruct(
785805 case 'DoubleTypeAnnotation' :
786806 case 'FloatTypeAnnotation' :
787807 case 'MixedTypeAnnotation' :
788- return `result["${ name } "] = ${ name } ;` ;
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+ }
789819 default :
790- return `result["${ name } "] = ::facebook::react::toDynamic(${ name } );` ;
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+ }
791831 }
792832 } )
793833 . join ( '\n ' ) ;
0 commit comments