[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:
James Kuszmaul
2022-06-14 17:51:12 -05:00
committed by GitHub
parent 0cc1aeb8ca
commit 42acdb63c3
2 changed files with 53 additions and 18 deletions

View File

@@ -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;

View File

@@ -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"],
}, },