Skip to content

[TS] Add option for removing typescript import flag using --ts-no-import-ext #7748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ struct IDLOptions {
bool json_nested_flexbuffers;
bool json_nested_legacy_flatbuffers;
bool ts_flat_file;
bool ts_no_import_ext;
bool no_leak_private_annotations;
bool require_json_eof;

Expand Down Expand Up @@ -763,6 +764,7 @@ struct IDLOptions {
json_nested_flexbuffers(true),
json_nested_legacy_flatbuffers(false),
ts_flat_file(false),
ts_no_import_ext(false),
no_leak_private_annotations(false),
require_json_eof(true),
mini_reflect(IDLOptions::kNone),
Expand Down
2 changes: 2 additions & 0 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
opts.json_nested_legacy_flatbuffers = true;
} else if (arg == "--ts-flat-files") {
opts.ts_flat_file = true;
} else if (arg == "--ts-no-import-ext") {
opts.ts_no_import_ext = true;
} else if (arg == "--no-leak-private-annotation") {
opts.no_leak_private_annotations = true;
} else if (arg == "--annotate") {
Expand Down
10 changes: 6 additions & 4 deletions src/idl_gen_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,16 @@ class TsGenerator : public BaseGenerator {
// specified here? Should we always be adding the "./" for a relative
// path or turn it off if --include-prefix is specified, or something
// else?
std::string import_extension = parser_.opts.ts_no_import_ext ? "" : ".js";
std::string include_name =
"./" + flatbuffers::StripExtension(include_file);
"./" + flatbuffers::StripExtension(include_file) + import_extension;
code += "import {";
for (const auto &pair : it.second) {
code += namer_.EscapeKeyword(pair.first) + " as " +
namer_.EscapeKeyword(pair.second) + ", ";
}
code.resize(code.size() - 2);
code += "} from '" + include_name + ".js';\n";
code += "} from '" + include_name + "';\n";
}
code += "\n";
}
Expand Down Expand Up @@ -883,10 +884,11 @@ class TsGenerator : public BaseGenerator {
import.object_name = object_name;
import.bare_file_path = bare_file_path;
import.rel_file_path = rel_file_path;
std::string import_extension = parser_.opts.ts_no_import_ext ? "" : ".js";
import.import_statement = "import { " + symbols_expression + " } from '" +
rel_file_path + ".js';";
rel_file_path + import_extension + "';";
import.export_statement = "export { " + symbols_expression + " } from '." +
bare_file_path + ".js';";
bare_file_path + import_extension + "';";
import.dependency = &dependency;
import.dependent = &dependent;

Expand Down
6 changes: 6 additions & 0 deletions tests/ts/TypeScriptTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
schema="../optional_scalars.fbs",
)

flatc(
options=["--ts", "--reflect-names", "--gen-name-strings", "--ts-no-import-ext"],
schema="../optional_scalars.fbs",
prefix="no_import_ext",
)

flatc(
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema=[
Expand Down
7 changes: 7 additions & 0 deletions tests/ts/no_import_ext/optional-scalars/optional-byte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
export var OptionalByte;
(function (OptionalByte) {
OptionalByte[OptionalByte["None"] = 0] = "None";
OptionalByte[OptionalByte["One"] = 1] = "One";
OptionalByte[OptionalByte["Two"] = 2] = "Two";
})(OptionalByte || (OptionalByte = {}));
7 changes: 7 additions & 0 deletions tests/ts/no_import_ext/optional-scalars/optional-byte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify

export enum OptionalByte {
None = 0,
One = 1,
Two = 2
}
Loading