@@ -402,27 +402,26 @@ class TsGenerator : public BaseGenerator {
402
402
const auto &value = field.value ;
403
403
if (value.type .enum_def && value.type .base_type != BASE_TYPE_UNION &&
404
404
value.type .base_type != BASE_TYPE_VECTOR) {
405
- // If the value is an enum with a 64-bit base type, we have to just
406
- // return the bigint value directly since typescript does not support
407
- // enums with bigint backing types.
408
405
switch (value.type .base_type ) {
409
406
case BASE_TYPE_ARRAY: {
410
407
std::string ret = " [" ;
411
408
for (auto i = 0 ; i < value.type .fixed_length ; ++i) {
412
- ret + =
409
+ std::string enum_name =
413
410
AddImport (imports, *value.type .enum_def , *value.type .enum_def )
414
- .name +
415
- " . " +
416
- namer_. Variant (
417
- *value. type . enum_def -> FindByValue (value. constant ));
418
- if (i < value.type .fixed_length - 1 ) { ret += " , " ; }
411
+ .name ;
412
+ std::string enum_value = namer_. Variant (
413
+ *value. type . enum_def -> FindByValue (value. constant ));
414
+ ret += enum_name + " . " + enum_value +
415
+ (i < value.type .fixed_length - 1 ? " , " : " " );
419
416
}
420
417
ret += " ]" ;
421
418
return ret;
422
- break ;
423
419
}
424
420
case BASE_TYPE_LONG:
425
421
case BASE_TYPE_ULONG: {
422
+ // If the value is an enum with a 64-bit base type, we have to just
423
+ // return the bigint value directly since typescript does not support
424
+ // enums with bigint backing types.
426
425
return " BigInt('" + value.constant + " ')" ;
427
426
}
428
427
default : {
@@ -481,20 +480,20 @@ class TsGenerator : public BaseGenerator {
481
480
case BASE_TYPE_LONG:
482
481
case BASE_TYPE_ULONG: return allowNull ? " bigint|null" : " bigint" ;
483
482
case BASE_TYPE_ARRAY: {
483
+ std::string name;
484
484
if (type.element == BASE_TYPE_LONG || type.element == BASE_TYPE_ULONG) {
485
- return allowNull ? " bigint[]|null" : " bigint[]" ;
486
- }
487
- if (type.element != BASE_TYPE_STRUCT) {
488
- return allowNull ? " number[]|null" : " number[]" ;
489
- }
490
-
491
- std::string name = " any" ;
492
-
493
- if (parser_.opts .generate_object_based_api ) {
494
- name += " |" + GetTypeName (*type.struct_def , /* object_api =*/ true );
485
+ name = " bigint[]" ;
486
+ } else if (type.element != BASE_TYPE_STRUCT) {
487
+ name = " number[]" ;
488
+ } else {
489
+ name = " any[]" ;
490
+ if (parser_.opts .generate_object_based_api ) {
491
+ name = " (any|" +
492
+ GetTypeName (*type.struct_def , /* object_api =*/ true ) + " )[]" ;
493
+ }
495
494
}
496
495
497
- return allowNull ? " ( " + name + " )[] | null" : name ;
496
+ return name + (allowNull ? " | null" : " " ) ;
498
497
}
499
498
default :
500
499
if (IsScalar (type.base_type )) {
@@ -568,8 +567,9 @@ class TsGenerator : public BaseGenerator {
568
567
// Generate arguments for a struct inside a struct. To ensure names
569
568
// don't clash, and to make it obvious these arguments are constructing
570
569
// a nested struct, prefix the name with the field name.
571
- GenStructBody (*field.value .type .struct_def , body,
572
- nameprefix.length () ? nameprefix + " _" + field.name : field.name );
570
+ GenStructBody (
571
+ *field.value .type .struct_def , body,
572
+ nameprefix.length () ? nameprefix + " _" + field.name : field.name );
573
573
} else {
574
574
auto element_type = field.value .type .element ;
575
575
@@ -578,25 +578,33 @@ class TsGenerator : public BaseGenerator {
578
578
case BASE_TYPE_STRUCT: {
579
579
std::string str_last_item_idx =
580
580
NumToString (field.value .type .fixed_length - 1 );
581
- *body += " \n for (let i = " + str_last_item_idx + " ; i >= 0; --i" + " ) {\n " ;
581
+ *body += " \n for (let i = " + str_last_item_idx +
582
+ " ; i >= 0; --i" + " ) {\n " ;
582
583
583
- std::string fname = nameprefix.length () ? nameprefix + " _" + field.name : field.name ;
584
+ std::string fname = nameprefix.length ()
585
+ ? nameprefix + " _" + field.name
586
+ : field.name ;
584
587
585
588
*body += " const item = " + fname + " ?.[i];\n\n " ;
586
-
589
+
587
590
if (parser_.opts .generate_object_based_api ) {
588
- *body += " if (item instanceof " + GetTypeName (*field.value .type .struct_def , /* object_api =*/ true ) + " ) {\n " ;
591
+ *body += " if (item instanceof " +
592
+ GetTypeName (*field.value .type .struct_def ,
593
+ /* object_api =*/ true ) +
594
+ " ) {\n " ;
589
595
*body += " item.pack(builder);\n " ;
590
596
*body += " continue;\n " ;
591
597
*body += " }\n\n " ;
592
598
}
593
599
594
- std::string class_name = GetPrefixedName (*field.value .type .struct_def );
600
+ std::string class_name =
601
+ GetPrefixedName (*field.value .type .struct_def );
595
602
std::string pack_func_create_call =
596
603
class_name + " .create" + class_name + " (builder,\n " ;
597
604
pack_func_create_call +=
598
- " " + GenStructMemberValueTS (*field.value .type .struct_def ,
599
- " item" , " ,\n " , false ) +
605
+ " " +
606
+ GenStructMemberValueTS (*field.value .type .struct_def , " item" ,
607
+ " ,\n " , false ) +
600
608
" \n " ;
601
609
*body += " " + pack_func_create_call;
602
610
*body += " );\n }\n\n " ;
@@ -606,12 +614,15 @@ class TsGenerator : public BaseGenerator {
606
614
default : {
607
615
std::string str_last_item_idx =
608
616
NumToString (field.value .type .fixed_length - 1 );
609
- std::string fname = nameprefix.length () ? nameprefix + " _" + field.name : field.name ;
617
+ std::string fname = nameprefix.length ()
618
+ ? nameprefix + " _" + field.name
619
+ : field.name ;
610
620
611
- *body += " \n for (let i = " + str_last_item_idx + " ; i >= 0; --i) {\n " ;
621
+ *body += " \n for (let i = " + str_last_item_idx +
622
+ " ; i >= 0; --i) {\n " ;
612
623
*body += " builder.write" ;
613
- *body +=
614
- GenWriteMethod ( static_cast <flatbuffers::Type>(field.value .type .element ));
624
+ *body += GenWriteMethod (
625
+ static_cast <flatbuffers::Type>(field.value .type .element ));
615
626
*body += " (" ;
616
627
*body += element_type == BASE_TYPE_BOOL ? " +" : " " ;
617
628
@@ -626,10 +637,10 @@ class TsGenerator : public BaseGenerator {
626
637
}
627
638
}
628
639
} else {
629
- std::string fname = nameprefix.length () ? nameprefix + " _" + field.name : field.name ;
640
+ std::string fname =
641
+ nameprefix.length () ? nameprefix + " _" + field.name : field.name ;
630
642
631
- *body += " builder.write" +
632
- GenWriteMethod (field.value .type ) + " (" ;
643
+ *body += " builder.write" + GenWriteMethod (field.value .type ) + " (" ;
633
644
if (field.value .type .base_type == BASE_TYPE_BOOL) {
634
645
*body += " Number(Boolean(" + fname + " )));\n " ;
635
646
continue ;
@@ -1015,7 +1026,10 @@ class TsGenerator : public BaseGenerator {
1015
1026
const auto conversion_function = GenUnionListConvFuncName (enum_def);
1016
1027
1017
1028
ret = " (() => {\n " ;
1018
- ret += " const ret: (" + GenObjApiUnionTypeTS (imports, *union_type.struct_def , parser_.opts , *union_type.enum_def ) + " )[] = [];\n " ;
1029
+ ret += " const ret: (" +
1030
+ GenObjApiUnionTypeTS (imports, *union_type.struct_def ,
1031
+ parser_.opts , *union_type.enum_def ) +
1032
+ " )[] = [];\n " ;
1019
1033
ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." +
1020
1034
namer_.Method (field_name, " TypeLength" ) + " ()" +
1021
1035
" ; "
0 commit comments