Namer applied to Typescript generator (#7488)

* Namer applied to Typescript generator

* fixes

* More fixes

Co-authored-by: Casper Neo <cneo@google.com>
Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Casper
2022-08-29 19:44:35 -04:00
committed by GitHub
parent ce382d6dd3
commit bf5d23230a
2 changed files with 174 additions and 204 deletions

View File

@@ -23,6 +23,7 @@
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h" #include "flatbuffers/idl.h"
#include "flatbuffers/util.h" #include "flatbuffers/util.h"
#include "idl_namer.h"
namespace flatbuffers { namespace flatbuffers {
namespace { namespace {
@@ -37,13 +38,49 @@ struct ImportDefinition {
const Definition *dependency = nullptr; const Definition *dependency = nullptr;
}; };
Namer::Config TypeScriptDefaultConfig() {
return { /*types=*/Case::kKeep,
/*constants=*/Case::kUnknown,
/*methods=*/Case::kLowerCamel,
/*functions=*/Case::kLowerCamel,
/*fields=*/Case::kLowerCamel,
/*variables=*/Case::kLowerCamel,
/*variants=*/Case::kKeep,
/*enum_variant_seperator=*/"::",
/*escape_keywords=*/Namer::Config::Escape::AfterConvertingCase,
/*namespaces=*/Case::kKeep,
/*namespace_seperator=*/"_",
/*object_prefix=*/"",
/*object_suffix=*/"T",
/*keyword_prefix=*/"",
/*keyword_suffix=*/"_",
/*filenames=*/Case::kDasher,
/*directories=*/Case::kDasher,
/*output_path=*/"",
/*filename_suffix=*/"_generated",
/*filename_extension=*/".ts" };
}
std::set<std::string> TypescriptKeywords() {
// List of keywords retrieved from here:
// https://github.com/microsoft/TypeScript/issues/2536
return {
"arguments", "break", "case", "catch", "class", "const",
"continue", "debugger", "default", "delete", "do", "else",
"enum", "export", "extends", "false", "finally", "for",
"function", "if", "import", "in", "instanceof", "new",
"null", "Object", "return", "super", "switch", "this",
"throw", "true", "try", "typeof", "var", "void",
"while", "with", "as", "implements", "interface", "let",
"package", "private", "protected", "public", "static", "yield",
};
}
enum AnnotationType { kParam = 0, kType = 1, kReturns = 2 }; enum AnnotationType { kParam = 0, kType = 1, kReturns = 2 };
template<typename T> template<typename T> struct SupportsObjectAPI : std::false_type {};
struct SupportsObjectAPI : std::false_type {};
template<> template<> struct SupportsObjectAPI<StructDef> : std::true_type {};
struct SupportsObjectAPI<StructDef> : std::true_type {};
} // namespace } // namespace
@@ -56,67 +93,10 @@ class TsGenerator : public BaseGenerator {
TsGenerator(const Parser &parser, const std::string &path, TsGenerator(const Parser &parser, const std::string &path,
const std::string &file_name) const std::string &file_name)
: BaseGenerator(parser, path, file_name, "", "_", "ts") { : BaseGenerator(parser, path, file_name, "", "_", "ts"),
// clang-format off namer_(WithFlagOptions(TypeScriptDefaultConfig(), parser.opts, path),
TypescriptKeywords()) {}
// List of keywords retrieved from here:
// https://github.com/microsoft/TypeScript/issues/2536
// One per line to ease comparisons to that list are easier
static const char *const keywords[] = {
"arguments",
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"enum",
"export",
"extends",
"false",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"null",
"Object",
"return",
"super",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"var",
"void",
"while",
"with",
"as",
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
nullptr,
// clang-format on
};
for (auto kw = keywords; *kw; kw++) keywords_.insert(*kw);
}
bool generate() { bool generate() {
generateEnums(); generateEnums();
generateStructs(); generateStructs();
@@ -132,29 +112,27 @@ class TsGenerator : public BaseGenerator {
std::string GetTypeName(const EnumDef &def, const bool = false, std::string GetTypeName(const EnumDef &def, const bool = false,
const bool force_ns_wrap = false) { const bool force_ns_wrap = false) {
std::string base_name = def.name;
if (IncludeNamespace() || force_ns_wrap) { if (IncludeNamespace() || force_ns_wrap) {
base_name = WrapInNameSpace(def.defined_namespace, base_name); return namer_.NamespacedType(def);
} }
return namer_.Type(def);
return EscapeKeyword(base_name);
} }
std::string GetTypeName(const StructDef &def, const bool object_api = false, std::string GetTypeName(const StructDef &def, const bool object_api = false,
const bool force_ns_wrap = false) { const bool force_ns_wrap = false) {
std::string base_name = def.name;
if (object_api && parser_.opts.generate_object_based_api) { if (object_api && parser_.opts.generate_object_based_api) {
base_name = if (IncludeNamespace() || force_ns_wrap) {
parser_.opts.object_prefix + base_name + parser_.opts.object_suffix; return namer_.NamespacedObjectType(def);
} else {
return namer_.ObjectType(def);
}
} else {
if (IncludeNamespace() || force_ns_wrap) {
return namer_.NamespacedType(def);
} else {
return namer_.Type(def);
}
} }
if (IncludeNamespace() || force_ns_wrap) {
base_name = WrapInNameSpace(def.defined_namespace, base_name);
}
return EscapeKeyword(base_name);
} }
// Save out the generated code for a single class while adding // Save out the generated code for a single class while adding
@@ -189,20 +167,16 @@ class TsGenerator : public BaseGenerator {
flat_file_definitions_.insert(&definition); flat_file_definitions_.insert(&definition);
return true; return true;
} else { } else {
auto basename = auto dirs = namer_.Directories(*definition.defined_namespace);
NamespaceDir(*definition.defined_namespace, true) + EnsureDirExists(dirs);
ConvertCase(definition.name, Case::kDasher, Case::kUpperCamel); auto basename = dirs + namer_.File(definition, SkipFile::Suffix);
return SaveFile((basename + ".ts").c_str(), code, false); return SaveFile(basename.c_str(), code, false);
} }
} }
private: private:
std::unordered_set<std::string> keywords_; IdlNamer namer_;
std::string EscapeKeyword(const std::string &name) const {
return keywords_.find(name) == keywords_.end() ? name : name + "_";
}
import_set imports_all_; import_set imports_all_;
@@ -285,8 +259,8 @@ class TsGenerator : public BaseGenerator {
"./" + flatbuffers::StripExtension(include_file); "./" + flatbuffers::StripExtension(include_file);
code += "import {"; code += "import {";
for (const auto &pair : it.second) { for (const auto &pair : it.second) {
code += EscapeKeyword(pair.first) + " as " + code += namer_.EscapeKeyword(pair.first) + " as " +
EscapeKeyword(pair.second) + ", "; namer_.EscapeKeyword(pair.second) + ", ";
} }
code.resize(code.size() - 2); code.resize(code.size() - 2);
code += "} from '" + include_name + "';\n"; code += "} from '" + include_name + "';\n";
@@ -349,15 +323,13 @@ class TsGenerator : public BaseGenerator {
GenDocComment(ev.doc_comment, code_ptr, " "); GenDocComment(ev.doc_comment, code_ptr, " ");
} }
const std::string escaped_name = EscapeKeyword(ev.name);
// Generate mapping between EnumName: EnumValue(int) // Generate mapping between EnumName: EnumValue(int)
if (reverse) { if (reverse) {
code += " '" + enum_def.ToString(ev) + "'"; code += " '" + enum_def.ToString(ev) + "'";
code += " = "; code += " = ";
code += "'" + escaped_name + "'"; code += "'" + namer_.Variant(ev) + "'";
} else { } else {
code += " " + escaped_name; code += " " + namer_.Variant(ev);
code += " = "; code += " = ";
// Unfortunately, because typescript does not support bigint enums, // Unfortunately, because typescript does not support bigint enums,
// for 64-bit enums, we instead map the enum names to strings. // for 64-bit enums, we instead map the enum names to strings.
@@ -414,8 +386,8 @@ class TsGenerator : public BaseGenerator {
return GenBBAccess() + ".__union_with_string" + arguments; return GenBBAccess() + ".__union_with_string" + arguments;
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType(), arguments); case BASE_TYPE_VECTOR: return GenGetter(type.VectorType(), arguments);
default: { default: {
auto getter = GenBBAccess() + ".read" + auto getter = GenBBAccess() + "." +
ConvertCase(GenType(type), Case::kUpperCamel) + arguments; namer_.Method("read_" + GenType(type)) + arguments;
if (type.base_type == BASE_TYPE_BOOL) { getter = "!!" + getter; } if (type.base_type == BASE_TYPE_BOOL) { getter = "!!" + getter; }
return getter; return getter;
} }
@@ -443,7 +415,7 @@ class TsGenerator : public BaseGenerator {
return AddImport(imports, *value.type.enum_def, return AddImport(imports, *value.type.enum_def,
*value.type.enum_def) *value.type.enum_def)
.name + .name +
"." + EscapeKeyword(val->name); "." + namer_.Variant(*val);
} else { } else {
return value.constant; return value.constant;
} }
@@ -504,7 +476,7 @@ class TsGenerator : public BaseGenerator {
} }
// Returns the method name for use with add/put calls. // Returns the method name for use with add/put calls.
static std::string GenWriteMethod(const Type &type) { std::string GenWriteMethod(const Type &type) {
// Forward to signed versions since unsigned versions don't exist // Forward to signed versions since unsigned versions don't exist
switch (type.base_type) { switch (type.base_type) {
case BASE_TYPE_UTYPE: case BASE_TYPE_UTYPE:
@@ -515,9 +487,8 @@ class TsGenerator : public BaseGenerator {
default: break; default: break;
} }
return IsScalar(type.base_type) return IsScalar(type.base_type) ? namer_.Type(GenType(type))
? ConvertCase(GenType(type), Case::kUpperCamel) : (IsStruct(type) ? "Struct" : "Offset");
: (IsStruct(type) ? "Struct" : "Offset");
} }
template<typename T> static std::string MaybeAdd(T value) { template<typename T> static std::string MaybeAdd(T value) {
@@ -547,8 +518,8 @@ class TsGenerator : public BaseGenerator {
} }
} }
static void GenStructBody(const StructDef &struct_def, std::string *body, void GenStructBody(const StructDef &struct_def, std::string *body,
const std::string &nameprefix) { const std::string &nameprefix) {
*body += " builder.prep("; *body += " builder.prep(";
*body += NumToString(struct_def.minalign) + ", "; *body += NumToString(struct_def.minalign) + ", ";
*body += NumToString(struct_def.bytesize) + ");\n"; *body += NumToString(struct_def.bytesize) + ");\n";
@@ -574,7 +545,7 @@ class TsGenerator : public BaseGenerator {
} }
std::string GenerateNewExpression(const std::string &object_name) { std::string GenerateNewExpression(const std::string &object_name) {
return "new " + EscapeKeyword(object_name) + "()"; return "new " + namer_.Type(object_name) + "()";
} }
void GenerateRootAccessor(StructDef &struct_def, std::string *code_ptr, void GenerateRootAccessor(StructDef &struct_def, std::string *code_ptr,
@@ -774,14 +745,11 @@ class TsGenerator : public BaseGenerator {
} }
if (dep_comps.size() == 0) { rel_file_path += "."; } if (dep_comps.size() == 0) { rel_file_path += "."; }
const auto &depc_comps = dependency.defined_namespace->components;
for (auto it = depc_comps.begin(); it != depc_comps.end(); it++) {
bare_file_path +=
kPathSeparator + ConvertCase(*it, Case::kDasher, Case::kUpperCamel);
}
bare_file_path += bare_file_path +=
kPathSeparator + kPathSeparator +
ConvertCase(dependency.name, Case::kDasher, Case::kUpperCamel); namer_.Directories(dependency.defined_namespace->components,
SkipDir::OutputPath) +
namer_.File(dependency, SkipFile::SuffixAndExtension);
rel_file_path += bare_file_path; rel_file_path += bare_file_path;
ImportDefinition import; ImportDefinition import;
@@ -845,11 +813,11 @@ class TsGenerator : public BaseGenerator {
} }
std::string GenUnionConvFuncName(const EnumDef &enum_def) { std::string GenUnionConvFuncName(const EnumDef &enum_def) {
return "unionTo" + enum_def.name; return namer_.Function("unionTo", enum_def);
} }
std::string GenUnionListConvFuncName(const EnumDef &enum_def) { std::string GenUnionListConvFuncName(const EnumDef &enum_def) {
return "unionListTo" + enum_def.name; return namer_.Function("unionListTo", enum_def);
} }
std::string GenUnionConvFunc(const Type &union_type, import_set &imports) { std::string GenUnionConvFunc(const Type &union_type, import_set &imports) {
@@ -876,7 +844,7 @@ class TsGenerator : public BaseGenerator {
const auto &ev = **it; const auto &ev = **it;
if (ev.IsZero()) { continue; } if (ev.IsZero()) { continue; }
ret += " case '" + ev.name + "': "; ret += " case '" + namer_.Variant(ev) + "': ";
if (IsString(ev.union_type)) { if (IsString(ev.union_type)) {
ret += "return " + accessor_str + "'') as string;"; ret += "return " + accessor_str + "'') as string;";
@@ -931,11 +899,11 @@ class TsGenerator : public BaseGenerator {
if (!is_array) { if (!is_array) {
const auto conversion_function = GenUnionConvFuncName(enum_def); const auto conversion_function = GenUnionConvFuncName(enum_def);
const auto target_enum = "this." + field_name + "Type()";
ret = "(() => {\n"; ret = "(() => {\n";
ret += " let temp = " + conversion_function + "(" + target_enum + ret += " let temp = " + conversion_function + "(this." +
", " + field_binded_method + ");\n"; namer_.Method(field_name, "Type") + "(), " +
field_binded_method + ");\n";
ret += " if(temp === null) { return null; }\n"; ret += " if(temp === null) { return null; }\n";
ret += union_has_string ret += union_has_string
? " if(typeof temp === 'string') { return temp; }\n" ? " if(typeof temp === 'string') { return temp; }\n"
@@ -944,17 +912,15 @@ class TsGenerator : public BaseGenerator {
ret += " })()"; ret += " })()";
} else { } else {
const auto conversion_function = GenUnionListConvFuncName(enum_def); const auto conversion_function = GenUnionListConvFuncName(enum_def);
const auto target_enum_accesor = "this." + field_name + "Type";
const auto target_enum_length = target_enum_accesor + "Length()";
ret = "(() => {\n"; ret = "(() => {\n";
ret += " let ret = [];\n"; ret += " let ret = [];\n";
ret += " for(let targetEnumIndex = 0; targetEnumIndex < " + ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." +
target_enum_length + namer_.Method(field_name, "TypeLength") + "()" +
"; " "; "
"++targetEnumIndex) {\n"; "++targetEnumIndex) {\n";
ret += " let targetEnum = " + target_enum_accesor + ret += " let targetEnum = this." +
"(targetEnumIndex);\n"; namer_.Method(field_name, "Type") + "(targetEnumIndex);\n";
ret += " if(targetEnum === null || " + enum_type + ret += " if(targetEnum === null || " + enum_type +
"[targetEnum!] === 'NONE') { " "[targetEnum!] === 'NONE') { "
"continue; }\n\n"; "continue; }\n\n";
@@ -993,11 +959,9 @@ class TsGenerator : public BaseGenerator {
it != struct_def.fields.vec.end(); ++it) { it != struct_def.fields.vec.end(); ++it) {
auto &field = **it; auto &field = **it;
auto curr_member_accessor = auto curr_member_accessor = prefix + "." + namer_.Method(field);
prefix + "." + ConvertCase(field.name, Case::kLowerCamel);
if (prefix != "this") { if (prefix != "this") {
curr_member_accessor = curr_member_accessor = prefix + "?." + namer_.Method(field);
prefix + "?." + ConvertCase(field.name, Case::kLowerCamel);
} }
if (IsStruct(field.value.type)) { if (IsStruct(field.value.type)) {
ret += GenStructMemberValueTS(*field.value.type.struct_def, ret += GenStructMemberValueTS(*field.value.type.struct_def,
@@ -1066,10 +1030,10 @@ class TsGenerator : public BaseGenerator {
auto &field = **it; auto &field = **it;
if (field.deprecated) continue; if (field.deprecated) continue;
const auto field_name = ConvertCase(field.name, Case::kLowerCamel); const auto field_method = namer_.Method(field);
const auto field_name_escaped = EscapeKeyword(field_name); const auto field_field = namer_.Field(field);
const std::string field_binded_method = const std::string field_binded_method =
"this." + field_name + ".bind(this)"; "this." + field_method + ".bind(this)";
std::string field_val; std::string field_val;
std::string field_type; std::string field_type;
@@ -1089,14 +1053,14 @@ class TsGenerator : public BaseGenerator {
field_type += GenTypeName(imports, field, field.value.type, false, field_type += GenTypeName(imports, field, field.value.type, false,
has_null_default); has_null_default);
field_val = "this." + field_name + "()"; field_val = "this." + namer_.Method(field) + "()";
if (field.value.type.base_type != BASE_TYPE_STRING) { if (field.value.type.base_type != BASE_TYPE_STRING) {
field_offset_val = "this." + field_name_escaped; field_offset_val = "this." + namer_.Field(field);
} else { } else {
field_offset_decl = GenNullCheckConditional( field_offset_decl = GenNullCheckConditional(
"this." + field_name_escaped, "this." + namer_.Field(field),
"builder.createString(this." + field_name_escaped + "!)", "0"); "builder.createString(this." + field_field + "!)", "0");
} }
} }
@@ -1108,12 +1072,13 @@ class TsGenerator : public BaseGenerator {
const auto &sd = *field.value.type.struct_def; const auto &sd = *field.value.type.struct_def;
field_type += AddImport(imports, struct_def, sd).object_name; field_type += AddImport(imports, struct_def, sd).object_name;
const std::string field_accessor = "this." + field_name + "()"; const std::string field_accessor =
"this." + namer_.Method(field) + "()";
field_val = GenNullCheckConditional(field_accessor, field_val = GenNullCheckConditional(field_accessor,
field_accessor + "!.unpack()"); field_accessor + "!.unpack()");
auto packing = GenNullCheckConditional( auto packing = GenNullCheckConditional(
"this." + field_name_escaped, "this." + field_field,
"this." + field_name_escaped + "!.pack(builder)", "0"); "this." + field_field + "!.pack(builder)", "0");
if (sd.fixed) { if (sd.fixed) {
field_offset_val = std::move(packing); field_offset_val = std::move(packing);
@@ -1140,22 +1105,20 @@ class TsGenerator : public BaseGenerator {
field_type += ")[]"; field_type += ")[]";
field_val = GenBBAccess() + ".createObjList(" + field_val = GenBBAccess() + ".createObjList(" +
field_binded_method + ", this." + field_name + field_binded_method + ", this." +
"Length())"; namer_.Method(field, "Length") + "())";
if (sd.fixed) { if (sd.fixed) {
field_offset_decl = field_offset_decl =
"builder.createStructOffsetList(this." + "builder.createStructOffsetList(this." + field_field +
field_name_escaped + ", " + ", " + AddImport(imports, struct_def, struct_def).name +
AddImport(imports, struct_def, struct_def).name + "." + namer_.Method("start", field, "Vector") + ")";
".start" + ConvertCase(field_name, Case::kUpperCamel) +
"Vector)";
} else { } else {
field_offset_decl = field_offset_decl =
AddImport(imports, struct_def, struct_def).name + AddImport(imports, struct_def, struct_def).name + "." +
".create" + ConvertCase(field_name, Case::kUpperCamel) + namer_.Method("create", field, "Vector") +
"Vector(builder, builder.createObjectOffsetList(" + "(builder, builder.createObjectOffsetList(" + "this." +
"this." + field_name_escaped + "))"; field_field + "))";
} }
break; break;
@@ -1164,13 +1127,13 @@ class TsGenerator : public BaseGenerator {
case BASE_TYPE_STRING: { case BASE_TYPE_STRING: {
field_type += "string)[]"; field_type += "string)[]";
field_val = GenBBAccess() + ".createScalarList(" + field_val = GenBBAccess() + ".createScalarList(" +
field_binded_method + ", this." + field_name + field_binded_method + ", this." +
"Length())"; namer_.Field(field, "Length") + "())";
field_offset_decl = field_offset_decl =
AddImport(imports, struct_def, struct_def).name + AddImport(imports, struct_def, struct_def).name + "." +
".create" + ConvertCase(field_name, Case::kUpperCamel) + namer_.Method("create", field, "Vector") +
"Vector(builder, builder.createObjectOffsetList(" + "(builder, builder.createObjectOffsetList(" + "this." +
"this." + field_name_escaped + "))"; namer_.Field(field) + "))";
break; break;
} }
@@ -1178,14 +1141,14 @@ class TsGenerator : public BaseGenerator {
field_type += GenObjApiUnionTypeTS( field_type += GenObjApiUnionTypeTS(
imports, struct_def, parser.opts, *(vectortype.enum_def)); imports, struct_def, parser.opts, *(vectortype.enum_def));
field_type += ")[]"; field_type += ")[]";
field_val = GenUnionValTS(imports, struct_def, field_name, field_val = GenUnionValTS(imports, struct_def, field_method,
vectortype, true); vectortype, true);
field_offset_decl = field_offset_decl =
AddImport(imports, struct_def, struct_def).name + AddImport(imports, struct_def, struct_def).name + "." +
".create" + ConvertCase(field_name, Case::kUpperCamel) + namer_.Method("create", field, "Vector") +
"Vector(builder, builder.createObjectOffsetList(" + "(builder, builder.createObjectOffsetList(" + "this." +
"this." + field_name_escaped + "))"; namer_.Field(field) + "))";
break; break;
} }
@@ -1198,13 +1161,13 @@ class TsGenerator : public BaseGenerator {
} }
field_type += ")[]"; field_type += ")[]";
field_val = GenBBAccess() + ".createScalarList(" + field_val = GenBBAccess() + ".createScalarList(" +
field_binded_method + ", this." + field_name + field_binded_method + ", this." +
"Length())"; namer_.Method(field, "Length") + "())";
field_offset_decl = field_offset_decl =
AddImport(imports, struct_def, struct_def).name + AddImport(imports, struct_def, struct_def).name + "." +
".create" + ConvertCase(field_name, Case::kUpperCamel) + namer_.Method("create", field, "Vector") +
"Vector(builder, this." + field_name_escaped + ")"; "(builder, this." + field_field + ")";
break; break;
} }
@@ -1217,10 +1180,10 @@ class TsGenerator : public BaseGenerator {
field_type += GenObjApiUnionTypeTS(imports, struct_def, parser.opts, field_type += GenObjApiUnionTypeTS(imports, struct_def, parser.opts,
*(field.value.type.enum_def)); *(field.value.type.enum_def));
field_val = GenUnionValTS(imports, struct_def, field_name, field_val = GenUnionValTS(imports, struct_def, field_method,
field.value.type); field.value.type);
field_offset_decl = field_offset_decl =
"builder.createObjectOffset(this." + field_name_escaped + ")"; "builder.createObjectOffset(this." + field_field + ")";
break; break;
} }
@@ -1233,16 +1196,16 @@ class TsGenerator : public BaseGenerator {
if (!field_offset_decl.empty()) { if (!field_offset_decl.empty()) {
field_offset_decl = field_offset_decl =
" const " + field_name_escaped + " = " + field_offset_decl + ";"; " const " + field_field + " = " + field_offset_decl + ";";
} }
if (field_offset_val.empty()) { field_offset_val = field_name_escaped; } if (field_offset_val.empty()) { field_offset_val = field_field; }
unpack_func += " " + field_val; unpack_func += " " + field_val;
unpack_to_func += " _o." + field_name_escaped + " = " + field_val + ";"; unpack_to_func += " _o." + field_field + " = " + field_val + ";";
// FIXME: if field_type and field_name_escaped are identical, then // FIXME: if field_type and field_field are identical, then
// this generates invalid typescript. // this generates invalid typescript.
constructor_func += " public " + field_name_escaped + ": " + field_type + constructor_func += " public " + field_field + ": " + field_type +
" = " + field_default_val; " = " + field_default_val;
if (!struct_def.fixed) { if (!struct_def.fixed) {
@@ -1257,9 +1220,9 @@ class TsGenerator : public BaseGenerator {
pack_func_create_call += pack_func_create_call +=
" if (" + field_offset_val + " !== null)\n "; " if (" + field_offset_val + " !== null)\n ";
} }
pack_func_create_call += " " + struct_name + ".add" + pack_func_create_call += " " + struct_name + "." +
ConvertCase(field.name, Case::kUpperCamel) + namer_.Method("add", field) + "(builder, " +
"(builder, " + field_offset_val + ");\n"; field_offset_val + ");\n";
} }
} }
@@ -1382,7 +1345,7 @@ class TsGenerator : public BaseGenerator {
const auto has_null_default = is_string || HasNullDefault(field); const auto has_null_default = is_string || HasNullDefault(field);
GenDocComment(field.doc_comment, code_ptr); GenDocComment(field.doc_comment, code_ptr);
std::string prefix = ConvertCase(field.name, Case::kLowerCamel) + "("; std::string prefix = namer_.Method(field) + "(";
if (is_string) { if (is_string) {
code += prefix + "):string|null\n"; code += prefix + "):string|null\n";
code += code +=
@@ -1429,7 +1392,7 @@ class TsGenerator : public BaseGenerator {
AddImport(imports, struct_def, *field.value.type.struct_def) AddImport(imports, struct_def, *field.value.type.struct_def)
.name; .name;
GenDocComment(field.doc_comment, code_ptr); GenDocComment(field.doc_comment, code_ptr);
code += ConvertCase(field.name, Case::kLowerCamel); code += namer_.Method(field);
code += "(obj?:" + type + "):" + type + "|null {\n"; code += "(obj?:" + type + "):" + type + "|null {\n";
if (struct_def.fixed) { if (struct_def.fixed) {
@@ -1469,7 +1432,7 @@ class TsGenerator : public BaseGenerator {
default: ret_type = vectortypename; default: ret_type = vectortypename;
} }
GenDocComment(field.doc_comment, code_ptr); GenDocComment(field.doc_comment, code_ptr);
std::string prefix = ConvertCase(field.name, Case::kLowerCamel); std::string prefix = namer_.Method(field);
// TODO: make it work without any // TODO: make it work without any
// if (is_union) { prefix += "<T extends flatbuffers.Table>"; } // if (is_union) { prefix += "<T extends flatbuffers.Table>"; }
if (is_union) { prefix += ""; } if (is_union) { prefix += ""; }
@@ -1529,7 +1492,7 @@ class TsGenerator : public BaseGenerator {
case BASE_TYPE_UNION: { case BASE_TYPE_UNION: {
GenDocComment(field.doc_comment, code_ptr); GenDocComment(field.doc_comment, code_ptr);
code += ConvertCase(field.name, Case::kLowerCamel); code += namer_.Method(field);
const auto &union_enum = *(field.value.type.enum_def); const auto &union_enum = *(field.value.type.enum_def);
const auto union_type = GenUnionGenericTypeTS(union_enum); const auto union_type = GenUnionGenericTypeTS(union_enum);
@@ -1554,12 +1517,15 @@ class TsGenerator : public BaseGenerator {
std::string type = std::string type =
GenTypeName(imports, struct_def, field.value.type, true); GenTypeName(imports, struct_def, field.value.type, true);
code += "mutate_" + field.name + "(value:" + type + "):boolean {\n"; code += namer_.LegacyTsMutateMethod(field) + "(value:" + type +
"):boolean {\n";
const std::string write_method =
"." + namer_.Method("write", GenType(field.value.type));
if (struct_def.fixed) { if (struct_def.fixed) {
code += " " + GenBBAccess() + ".write" + code += " " + GenBBAccess() + write_method + "(this.bb_pos + " +
ConvertCase(GenType(field.value.type), Case::kUpperCamel) + NumToString(field.value.offset) + ", ";
"(this.bb_pos + " + 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, " + NumToString(field.value.offset) +
@@ -1569,9 +1535,8 @@ class TsGenerator : public BaseGenerator {
code += " }\n\n"; code += " }\n\n";
// special case for bools, which are treated as uint8 // special case for bools, which are treated as uint8
code += " " + GenBBAccess() + ".write" + code +=
ConvertCase(GenType(field.value.type), Case::kUpperCamel) + " " + GenBBAccess() + write_method + "(this.bb_pos + offset, ";
"(this.bb_pos + offset, ";
if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; } if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
} }
@@ -1584,8 +1549,8 @@ class TsGenerator : public BaseGenerator {
if (IsVector(field.value.type)) { if (IsVector(field.value.type)) {
// Emit a length helper // Emit a length helper
GenDocComment(code_ptr); GenDocComment(code_ptr);
code += ConvertCase(field.name, Case::kLowerCamel); code += namer_.Method(field, "Length");
code += "Length():number {\n" + offset_prefix; code += "():number {\n" + offset_prefix;
code += code +=
GenBBAccess() + ".__vector_len(this.bb_pos + offset) : 0;\n}\n\n"; GenBBAccess() + ".__vector_len(this.bb_pos + offset) : 0;\n}\n\n";
@@ -1595,9 +1560,9 @@ class TsGenerator : public BaseGenerator {
if (IsScalar(vectorType.base_type) && !IsLong(vectorType.base_type)) { if (IsScalar(vectorType.base_type) && !IsLong(vectorType.base_type)) {
GenDocComment(code_ptr); GenDocComment(code_ptr);
code += ConvertCase(field.name, Case::kLowerCamel); code += namer_.Method(field, "Array");
code += "Array():" + GenType(vectorType) + "Array|null {\n" + code +=
offset_prefix; "():" + GenType(vectorType) + "Array|null {\n" + offset_prefix;
code += "new " + GenType(vectorType) + "Array(" + GenBBAccess() + code += "new " + GenType(vectorType) + "Array(" + GenBBAccess() +
".bytes().buffer, " + GenBBAccess() + ".bytes().buffer, " + GenBBAccess() +
@@ -1656,7 +1621,7 @@ class TsGenerator : public BaseGenerator {
// Generate the field insertion method // Generate the field insertion method
GenDocComment(code_ptr); GenDocComment(code_ptr);
code += "static add" + ConvertCase(field.name, Case::kUpperCamel); code += "static " + namer_.Method("add", field);
code += "(builder:flatbuffers.Builder, " + argname + ":" + code += "(builder:flatbuffers.Builder, " + argname + ":" +
GetArgType(imports, struct_def, field, false) + ") {\n"; GetArgType(imports, struct_def, field, false) + ") {\n";
code += " builder.addField" + GenWriteMethod(field.value.type) + "("; code += " builder.addField" + GenWriteMethod(field.value.type) + "(";
@@ -1687,8 +1652,8 @@ class TsGenerator : public BaseGenerator {
GenDocComment(code_ptr); GenDocComment(code_ptr);
const std::string sig_begin = const std::string sig_begin =
"static create" + ConvertCase(field.name, Case::kUpperCamel) + "static " + namer_.Method("create", field, "Vector") +
"Vector(builder:flatbuffers.Builder, data:"; "(builder:flatbuffers.Builder, data:";
const std::string sig_end = "):flatbuffers.Offset"; const std::string sig_end = "):flatbuffers.Offset";
std::string type = std::string type =
GenTypeName(imports, struct_def, vector_type, true) + "[]"; GenTypeName(imports, struct_def, vector_type, true) + "[]";
@@ -1725,8 +1690,9 @@ class TsGenerator : public BaseGenerator {
// after // after
GenDocComment(code_ptr); GenDocComment(code_ptr);
code += "static start" + ConvertCase(field.name, Case::kUpperCamel); code += "static ";
code += "Vector(builder:flatbuffers.Builder, numElems:number) {\n"; code += namer_.Method("start", field, "Vector");
code += "(builder:flatbuffers.Builder, numElems:number) {\n";
code += " builder.startVector(" + NumToString(elem_size); code += " builder.startVector(" + NumToString(elem_size);
code += ", numElems, " + NumToString(alignment) + ");\n"; code += ", numElems, " + NumToString(alignment) + ");\n";
code += "}\n\n"; code += "}\n\n";
@@ -1784,8 +1750,7 @@ class TsGenerator : public BaseGenerator {
code += " if (" + arg_name + " !== null)\n "; code += " if (" + arg_name + " !== null)\n ";
} }
code += " " + methodPrefix + ".add" + code += " " + methodPrefix + "." + namer_.Method("add", field) + "(";
ConvertCase(field.name, Case::kUpperCamel) + "(";
code += "builder, " + arg_name + ");\n"; code += "builder, " + arg_name + ");\n";
} }
@@ -1803,8 +1768,8 @@ class TsGenerator : public BaseGenerator {
code += "}\n"; code += "}\n";
code += "\n"; code += "\n";
code += "static deserialize(buffer: Uint8Array):" + EscapeKeyword(name) + code += "static deserialize(buffer: Uint8Array):" +
" {\n"; namer_.EscapeKeyword(name) + " {\n";
code += " return " + AddImport(imports, struct_def, struct_def).name + code += " return " + AddImport(imports, struct_def, struct_def).name +
".getRootAs" + name + "(new flatbuffers.ByteBuffer(buffer))\n"; ".getRootAs" + name + "(new flatbuffers.ByteBuffer(buffer))\n";
code += "}\n"; code += "}\n";
@@ -1833,12 +1798,8 @@ class TsGenerator : public BaseGenerator {
} }
std::string GetArgName(const FieldDef &field) { std::string GetArgName(const FieldDef &field) {
auto argname = ConvertCase(field.name, Case::kLowerCamel); auto argname = namer_.Variable(field);
if (!IsScalar(field.value.type.base_type)) { if (!IsScalar(field.value.type.base_type)) { argname += "Offset"; }
argname += "Offset";
} else {
argname = EscapeKeyword(argname);
}
return argname; return argname;
} }

View File

@@ -33,6 +33,9 @@ class IdlNamer : public Namer {
std::string Type(const EnumDef &d) const { return Type(d.name); } std::string Type(const EnumDef &d) const { return Type(d.name); }
std::string Function(const Definition &s) const { return Function(s.name); } std::string Function(const Definition &s) const { return Function(s.name); }
std::string Function(const std::string& prefix, const Definition &s) const {
return Function(prefix + s.name);
}
std::string Field(const FieldDef &s) const { return Field(s.name); } std::string Field(const FieldDef &s) const { return Field(s.name); }
std::string Field(const FieldDef &d, const std::string &s) const { std::string Field(const FieldDef &d, const std::string &s) const {
@@ -131,6 +134,12 @@ class IdlNamer : public Namer {
suffix; suffix;
} }
// This is a mix of snake case and keep casing, when Ts should be using
// lower camel case.
std::string LegacyTsMutateMethod(const FieldDef& d) {
return "mutate_" + d.name;
}
private: private:
std::string NamespacedString(const struct Namespace *ns, std::string NamespacedString(const struct Namespace *ns,
const std::string &str) const { const std::string &str) const {