Skip to content

Commit c716af8

Browse files
committed
Formatting & readability fixes on idl_gen_ts.cpp
Signed-off-by: Bülent Vural <[email protected]>
1 parent cda288f commit c716af8

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

src/idl_gen_ts.cpp

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -402,27 +402,26 @@ class TsGenerator : public BaseGenerator {
402402
const auto &value = field.value;
403403
if (value.type.enum_def && value.type.base_type != BASE_TYPE_UNION &&
404404
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.
408405
switch (value.type.base_type) {
409406
case BASE_TYPE_ARRAY: {
410407
std::string ret = "[";
411408
for (auto i = 0; i < value.type.fixed_length; ++i) {
412-
ret +=
409+
std::string enum_name =
413410
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 ? ", " : "");
419416
}
420417
ret += "]";
421418
return ret;
422-
break;
423419
}
424420
case BASE_TYPE_LONG:
425421
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.
426425
return "BigInt('" + value.constant + "')";
427426
}
428427
default: {
@@ -481,20 +480,20 @@ class TsGenerator : public BaseGenerator {
481480
case BASE_TYPE_LONG:
482481
case BASE_TYPE_ULONG: return allowNull ? "bigint|null" : "bigint";
483482
case BASE_TYPE_ARRAY: {
483+
std::string name;
484484
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+
}
495494
}
496495

497-
return allowNull ? " (" + name + ")[] | null" : name;
496+
return name + (allowNull ? "|null" : "");
498497
}
499498
default:
500499
if (IsScalar(type.base_type)) {
@@ -568,8 +567,9 @@ class TsGenerator : public BaseGenerator {
568567
// Generate arguments for a struct inside a struct. To ensure names
569568
// don't clash, and to make it obvious these arguments are constructing
570569
// 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);
573573
} else {
574574
auto element_type = field.value.type.element;
575575

@@ -578,25 +578,33 @@ class TsGenerator : public BaseGenerator {
578578
case BASE_TYPE_STRUCT: {
579579
std::string str_last_item_idx =
580580
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";
582583

583-
std::string fname = nameprefix.length() ? nameprefix + "_" + field.name : field.name;
584+
std::string fname = nameprefix.length()
585+
? nameprefix + "_" + field.name
586+
: field.name;
584587

585588
*body += " const item = " + fname + "?.[i];\n\n";
586-
589+
587590
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";
589595
*body += " item.pack(builder);\n";
590596
*body += " continue;\n";
591597
*body += " }\n\n";
592598
}
593599

594-
std::string class_name = GetPrefixedName(*field.value.type.struct_def);
600+
std::string class_name =
601+
GetPrefixedName(*field.value.type.struct_def);
595602
std::string pack_func_create_call =
596603
class_name + ".create" + class_name + "(builder,\n";
597604
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) +
600608
"\n ";
601609
*body += " " + pack_func_create_call;
602610
*body += " );\n }\n\n";
@@ -606,12 +614,15 @@ class TsGenerator : public BaseGenerator {
606614
default: {
607615
std::string str_last_item_idx =
608616
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;
610620

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";
612623
*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));
615626
*body += "(";
616627
*body += element_type == BASE_TYPE_BOOL ? "+" : "";
617628

@@ -626,10 +637,10 @@ class TsGenerator : public BaseGenerator {
626637
}
627638
}
628639
} else {
629-
std::string fname = nameprefix.length() ? nameprefix + "_" + field.name : field.name;
640+
std::string fname =
641+
nameprefix.length() ? nameprefix + "_" + field.name : field.name;
630642

631-
*body += " builder.write" +
632-
GenWriteMethod(field.value.type) + "(";
643+
*body += " builder.write" + GenWriteMethod(field.value.type) + "(";
633644
if (field.value.type.base_type == BASE_TYPE_BOOL) {
634645
*body += "Number(Boolean(" + fname + ")));\n";
635646
continue;
@@ -1015,7 +1026,10 @@ class TsGenerator : public BaseGenerator {
10151026
const auto conversion_function = GenUnionListConvFuncName(enum_def);
10161027

10171028
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";
10191033
ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." +
10201034
namer_.Method(field_name, "TypeLength") + "()" +
10211035
"; "

tests/ts/arrays_test_complex/arrays_test_complex_generated.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static sizeOf():number {
5454
return 2656;
5555
}
5656

57-
static createArrayStruct(builder:flatbuffers.Builder, a_underscore: number, b_underscore: number[]|null, c: number, d: (any|NestedStructT)[] | null, e: number, f: (any|OuterStructT)[] | null, g: bigint[]|null):flatbuffers.Offset {
57+
static createArrayStruct(builder:flatbuffers.Builder, a_underscore: number, b_underscore: number[]|null, c: number, d: (any|NestedStructT)[]|null, e: number, f: (any|OuterStructT)[]|null, g: bigint[]|null):flatbuffers.Offset {
5858
builder.prep(8, 2656);
5959

6060
for (let i = 1; i >= 0; --i) {
@@ -387,7 +387,7 @@ static sizeOf():number {
387387
return 1072;
388388
}
389389

390-
static createNestedStruct(builder:flatbuffers.Builder, a: number[]|null, b: TestEnum, c_underscore: number[]|null, d_outer: (any|OuterStructT)[] | null, e: bigint[]|null):flatbuffers.Offset {
390+
static createNestedStruct(builder:flatbuffers.Builder, a: number[]|null, b: TestEnum, c_underscore: number[]|null, d_outer: (any|OuterStructT)[]|null, e: bigint[]|null):flatbuffers.Offset {
391391
builder.prep(8, 1072);
392392

393393
for (let i = 1; i >= 0; --i) {
@@ -519,7 +519,7 @@ static sizeOf():number {
519519
return 208;
520520
}
521521

522-
static createOuterStruct(builder:flatbuffers.Builder, a: boolean, b: number, c_underscore_a: number, c_underscore_b: number[]|null, c_underscore_c: number, c_underscore_d_underscore: bigint, d: (any|InnerStructT)[] | null, e_a: number, e_b: number[]|null, e_c: number, e_d_underscore: bigint, f: number[]|null):flatbuffers.Offset {
522+
static createOuterStruct(builder:flatbuffers.Builder, a: boolean, b: number, c_underscore_a: number, c_underscore_b: number[]|null, c_underscore_c: number, c_underscore_d_underscore: bigint, d: (any|InnerStructT)[]|null, e_a: number, e_b: number[]|null, e_c: number, e_d_underscore: bigint, f: number[]|null):flatbuffers.Offset {
523523
builder.prep(8, 208);
524524

525525
for (let i = 3; i >= 0; --i) {

0 commit comments

Comments
 (0)