Format TS generator file

This commit is contained in:
Austin Chick
2026-02-15 19:52:39 -06:00
parent 37196be52a
commit 0f0b24aee9

View File

@@ -1654,8 +1654,7 @@ class TsGenerator : public BaseGenerator {
// to preserve backwards compatibility, we allow the first field to be a // to preserve backwards compatibility, we allow the first field to be a
// struct // struct
return struct_def.fields.vec.size() < 2 || return struct_def.fields.vec.size() < 2 ||
std::all_of( std::all_of(std::begin(struct_def.fields.vec) + 1,
std::begin(struct_def.fields.vec) + 1,
std::end(struct_def.fields.vec), std::end(struct_def.fields.vec),
[](const FieldDef* f) -> bool { [](const FieldDef* f) -> bool {
FLATBUFFERS_ASSERT(f != nullptr); FLATBUFFERS_ASSERT(f != nullptr);
@@ -1693,15 +1692,15 @@ class TsGenerator : public BaseGenerator {
// Generate the __init method that sets the field in a pre-existing // Generate the __init method that sets the field in a pre-existing
// accessor object. This is to allow object reuse. // accessor object. This is to allow object reuse.
code += code += " __init(i:number, bb:flatbuffers.ByteBuffer):" + object_name +
" __init(i:number, bb:flatbuffers.ByteBuffer):" + object_name + " {\n"; " {\n";
code += " this.bb_pos = i;\n"; code += " this.bb_pos = i;\n";
code += " this.bb = bb;\n"; code += " this.bb = bb;\n";
code += " return this;\n"; code += " return this;\n";
code += "}\n\n"; code += "}\n\n";
// Generate special accessors for the table that when used as the root of a // Generate special accessors for the table that when used as the root of
// FlatBuffer // a FlatBuffer
GenerateRootAccessor(struct_def, code_ptr, code, object_name, false); GenerateRootAccessor(struct_def, code_ptr, code, object_name, false);
GenerateRootAccessor(struct_def, code_ptr, code, object_name, true); GenerateRootAccessor(struct_def, code_ptr, code, object_name, true);
@@ -1741,9 +1740,9 @@ class TsGenerator : public BaseGenerator {
std::string prefix = namer_.Method(field) + "("; std::string prefix = namer_.Method(field) + "(";
if (is_string) { if (is_string) {
code += prefix + "):string|" + null_keyword_ + "\n"; code += prefix + "):string|" + null_keyword_ + "\n";
code += code += prefix + "optionalEncoding:flatbuffers.Encoding" + "):" +
prefix + "optionalEncoding:flatbuffers.Encoding" + "):" + GenTypeName(imports, struct_def, field.value.type, false,
GenTypeName(imports, struct_def, field.value.type, false, true) + true) +
"\n"; "\n";
code += prefix + "optionalEncoding?:any"; code += prefix + "optionalEncoding?:any";
} else { } else {
@@ -1796,12 +1795,13 @@ class TsGenerator : public BaseGenerator {
if (struct_def.fixed) { if (struct_def.fixed) {
code += " return (obj || " + GenerateNewExpression(type); code += " return (obj || " + GenerateNewExpression(type);
code += ").__init(this.bb_pos"; code += ").__init(this.bb_pos";
code += code += MaybeAdd(field.value.offset) + ", " + GenBBAccess() +
MaybeAdd(field.value.offset) + ", " + GenBBAccess() + ");\n"; ");\n";
} else { } else {
code += offset_prefix + "(obj || " + GenerateNewExpression(type) + code += offset_prefix + "(obj || " +
").__init("; GenerateNewExpression(type) + ").__init(";
code += field.value.type.struct_def->fixed code +=
field.value.type.struct_def->fixed
? "this.bb_pos + offset" ? "this.bb_pos + offset"
: GenBBAccess() + ".__indirect(this.bb_pos + offset)"; : GenBBAccess() + ".__indirect(this.bb_pos + offset)";
code += ", " + GenBBAccess() + ") : " + null_keyword_ + ";\n"; code += ", " + GenBBAccess() + ") : " + null_keyword_ + ";\n";
@@ -1873,7 +1873,8 @@ class TsGenerator : public BaseGenerator {
} else if (IsString(vectortype)) { } else if (IsString(vectortype)) {
index += ", optionalEncoding"; index += ", optionalEncoding";
} }
code += offset_prefix + GenGetter(vectortype, "(" + index + ")"); code +=
offset_prefix + GenGetter(vectortype, "(" + index + ")");
} }
switch (field.value.type.base_type) { switch (field.value.type.base_type) {
@@ -1971,7 +1972,8 @@ class TsGenerator : public BaseGenerator {
} else if (IsString(vectortype)) { } else if (IsString(vectortype)) {
index += ", optionalEncoding"; index += ", optionalEncoding";
} }
code += offset_prefix + GenGetter(vectortype, "(" + index + ")"); code +=
offset_prefix + GenGetter(vectortype, "(" + index + ")");
} }
code += " : "; code += " : ";
if (field.value.type.element == BASE_TYPE_BOOL) { if (field.value.type.element == BASE_TYPE_BOOL) {
@@ -2003,7 +2005,8 @@ class TsGenerator : public BaseGenerator {
" " " "
"{\n"; "{\n";
code += offset_prefix + code +=
offset_prefix +
GenGetter(field.value.type, "(obj, this.bb_pos + offset)") + GenGetter(field.value.type, "(obj, this.bb_pos + offset)") +
" : " + null_keyword_ + ";\n"; " : " + null_keyword_ + ";\n";
break; break;
@@ -2015,8 +2018,8 @@ class TsGenerator : public BaseGenerator {
code += "}\n\n"; code += "}\n\n";
// Adds the mutable scalar value to the output // Adds the mutable scalar value to the output
if (IsScalar(field.value.type.base_type) && parser.opts.mutable_buffer && if (IsScalar(field.value.type.base_type) &&
!IsUnion(field.value.type)) { parser.opts.mutable_buffer && !IsUnion(field.value.type)) {
std::string type = std::string type =
GenTypeName(imports, struct_def, field.value.type, true); GenTypeName(imports, struct_def, field.value.type, true);
@@ -2031,8 +2034,8 @@ class TsGenerator : public BaseGenerator {
NumToString(field.value.offset) + ", "; NumToString(field.value.offset) + ", ";
} else { } else {
code += " const offset = " + GenBBAccess() + code += " const offset = " + GenBBAccess() +
".__offset(this.bb_pos, " + NumToString(field.value.offset) + ".__offset(this.bb_pos, " +
");\n\n"; NumToString(field.value.offset) + ");\n\n";
code += " if (offset === 0) {\n"; code += " if (offset === 0) {\n";
code += " return false;\n"; code += " return false;\n";
code += " }\n\n"; code += " }\n\n";
@@ -2083,7 +2086,8 @@ class TsGenerator : public BaseGenerator {
// Emit the fully qualified name // Emit the fully qualified name
if (parser_.opts.generate_name_strings) { if (parser_.opts.generate_name_strings) {
const std::string fullyQualifiedName = const std::string fullyQualifiedName =
struct_def.defined_namespace->GetFullyQualifiedName(struct_def.name); struct_def.defined_namespace->GetFullyQualifiedName(
struct_def.name);
GenDocComment(code_ptr); GenDocComment(code_ptr);
code += "static getFullyQualifiedName(): \""; code += "static getFullyQualifiedName(): \"";
@@ -2265,7 +2269,8 @@ class TsGenerator : public BaseGenerator {
code += " if (" + arg_name + " !== " + null_keyword_ + ")\n "; code += " if (" + arg_name + " !== " + null_keyword_ + ")\n ";
} }
code += " " + methodPrefix + "." + namer_.Method("add", field) + "("; code +=
" " + methodPrefix + "." + namer_.Method("add", field) + "(";
code += "builder, " + arg_name + ");\n"; code += "builder, " + arg_name + ");\n";
} }
@@ -2327,7 +2332,8 @@ class TsGenerator : public BaseGenerator {
}; // namespace ts }; // namespace ts
} // namespace ts } // namespace ts
static bool GenerateTS(const Parser& parser, const std::string& path, static bool
GenerateTS(const Parser& parser, const std::string& path,
const std::string& file_name) { const std::string& file_name) {
ts::TsGenerator generator(parser, path, file_name); ts::TsGenerator generator(parser, path, file_name);
return generator.generate(); return generator.generate();