mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-03 03:42:26 +00:00
[TS] Don't generate self-imports with --ts-flat-file (#7340)
The logic to manage generating typescript in a single file was generating self imports and unused local names, which triggered some linters. This resolves #7191
This commit is contained in:
@@ -177,6 +177,10 @@ class TsGenerator : public BaseGenerator {
|
|||||||
// This maps from import names to types to import.
|
// This maps from import names to types to import.
|
||||||
std::map<std::string, std::map<std::string, std::string>>
|
std::map<std::string, std::map<std::string, std::string>>
|
||||||
flat_file_import_declarations_;
|
flat_file_import_declarations_;
|
||||||
|
// For flat file codegen, tracks whether we need to import the flatbuffers
|
||||||
|
// library itself (not necessary for files that solely consist of enum
|
||||||
|
// definitions).
|
||||||
|
bool import_flatbuffers_lib_ = false;
|
||||||
|
|
||||||
// Generate code for all enums.
|
// Generate code for all enums.
|
||||||
void generateEnums() {
|
void generateEnums() {
|
||||||
@@ -200,6 +204,7 @@ class TsGenerator : public BaseGenerator {
|
|||||||
import_set bare_imports;
|
import_set bare_imports;
|
||||||
import_set imports;
|
import_set imports;
|
||||||
AddImport(bare_imports, "* as flatbuffers", "flatbuffers");
|
AddImport(bare_imports, "* as flatbuffers", "flatbuffers");
|
||||||
|
import_flatbuffers_lib_ = true;
|
||||||
auto &struct_def = **it;
|
auto &struct_def = **it;
|
||||||
std::string declcode;
|
std::string declcode;
|
||||||
GenStruct(parser_, struct_def, &declcode, imports);
|
GenStruct(parser_, struct_def, &declcode, imports);
|
||||||
@@ -212,7 +217,9 @@ class TsGenerator : public BaseGenerator {
|
|||||||
void generateEntry() {
|
void generateEntry() {
|
||||||
std::string code;
|
std::string code;
|
||||||
if (parser_.opts.ts_flat_file) {
|
if (parser_.opts.ts_flat_file) {
|
||||||
code += "import * as flatbuffers from 'flatbuffers';\n";
|
if (import_flatbuffers_lib_) {
|
||||||
|
code += "import * as flatbuffers from 'flatbuffers';\n";
|
||||||
|
}
|
||||||
for (const auto &it : flat_file_import_declarations_) {
|
for (const auto &it : flat_file_import_declarations_) {
|
||||||
// Note that we do end up generating an import for ourselves, which
|
// Note that we do end up generating an import for ourselves, which
|
||||||
// should generally be harmless.
|
// should generally be harmless.
|
||||||
@@ -635,15 +642,29 @@ class TsGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (parser_.opts.ts_flat_file) {
|
if (parser_.opts.ts_flat_file) {
|
||||||
std::string file = dependency.declaration_file == nullptr
|
// In flat-file generation, do not attempt to import things from ourselves
|
||||||
? dependency.file
|
// *and* do not wrap namespaces (note that this does override the logic
|
||||||
: dependency.declaration_file->substr(2);
|
// above, but since we force all non-self-imports to use namespace-based
|
||||||
file = RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
|
// names in flat file generation, it's fine).
|
||||||
dependency.file).substr(2);
|
if (dependent.file == dependency.file) {
|
||||||
long_import_name = ns + import_name;
|
// Should already be caught elsewhere, but if we ever try to get flat
|
||||||
flat_file_import_declarations_[file][import_name] = long_import_name;
|
// file generation and --gen-all working concurrently, then we'll need
|
||||||
if (parser_.opts.generate_object_based_api) {
|
// to update this import logic.
|
||||||
flat_file_import_declarations_[file][import_name + "T"] = long_import_name + "T";
|
FLATBUFFERS_ASSERT(!parser_.opts.generate_all);
|
||||||
|
long_import_name = import_name;
|
||||||
|
} else {
|
||||||
|
long_import_name = ns + import_name;
|
||||||
|
std::string file = dependency.declaration_file == nullptr
|
||||||
|
? dependency.file
|
||||||
|
: dependency.declaration_file->substr(2);
|
||||||
|
file = RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
|
||||||
|
dependency.file)
|
||||||
|
.substr(2);
|
||||||
|
flat_file_import_declarations_[file][import_name] = long_import_name;
|
||||||
|
if (parser_.opts.generate_object_based_api) {
|
||||||
|
flat_file_import_declarations_[file][import_name + "T"] =
|
||||||
|
long_import_name + "T";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -715,13 +736,26 @@ class TsGenerator : public BaseGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (parser_.opts.ts_flat_file) {
|
if (parser_.opts.ts_flat_file) {
|
||||||
std::string file = dependency.declaration_file == nullptr
|
// In flat-file generation, do not attempt to import things from ourselves
|
||||||
? dependency.file
|
// *and* do not wrap namespaces (note that this does override the logic
|
||||||
: dependency.declaration_file->substr(2);
|
// above, but since we force all non-self-imports to use namespace-based
|
||||||
file = RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
|
// names in flat file generation, it's fine).
|
||||||
dependency.file).substr(2);
|
if (dependent.file == dependency.file) {
|
||||||
long_import_name = ns + import_name;
|
// Should already be caught elsewhere, but if we ever try to get flat
|
||||||
flat_file_import_declarations_[file][import_name] = long_import_name;
|
// file generation and --gen-all working concurrently, then we'll need
|
||||||
|
// to update this import logic.
|
||||||
|
FLATBUFFERS_ASSERT(!parser_.opts.generate_all);
|
||||||
|
long_import_name = import_name;
|
||||||
|
} else {
|
||||||
|
long_import_name = ns + import_name;
|
||||||
|
std::string file = dependency.declaration_file == nullptr
|
||||||
|
? dependency.file
|
||||||
|
: dependency.declaration_file->substr(2);
|
||||||
|
file = RelativeToRootPath(StripFileName(AbsolutePath(dependent.file)),
|
||||||
|
dependency.file)
|
||||||
|
.substr(2);
|
||||||
|
flat_file_import_declarations_[file][import_name] = long_import_name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string import_statement;
|
std::string import_statement;
|
||||||
@@ -971,7 +1005,7 @@ class TsGenerator : public BaseGenerator {
|
|||||||
} else {
|
} else {
|
||||||
if (nullCheck) {
|
if (nullCheck) {
|
||||||
std::string nullValue = "0";
|
std::string nullValue = "0";
|
||||||
if (field.value.type.base_type == BASE_TYPE_BOOL) {
|
if (field.value.type.base_type == BASE_TYPE_BOOL) {
|
||||||
nullValue = "false";
|
nullValue = "false";
|
||||||
}
|
}
|
||||||
ret += "(" + curr_member_accessor + " ?? " + nullValue + ")";
|
ret += "(" + curr_member_accessor + " ?? " + nullValue + ")";
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ def flatbuffer_ts_library(
|
|||||||
],
|
],
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
|
"noUnusedLocals": True,
|
||||||
"strict": True,
|
"strict": True,
|
||||||
"types": ["node"],
|
"types": ["node"],
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user