Skip to content

Commit c2668fc

Browse files
ink-sudbaileychess
andauthored
Add ts-no-import-ext flag (#7748)
Co-authored-by: Derek Bailey <[email protected]>
1 parent b5802b5 commit c2668fc

13 files changed

+812
-5
lines changed

include/flatbuffers/idl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ struct IDLOptions {
659659
bool json_nested_flexbuffers;
660660
bool json_nested_legacy_flatbuffers;
661661
bool ts_flat_file;
662+
bool ts_no_import_ext;
662663
bool no_leak_private_annotations;
663664
bool require_json_eof;
664665

@@ -763,6 +764,7 @@ struct IDLOptions {
763764
json_nested_flexbuffers(true),
764765
json_nested_legacy_flatbuffers(false),
765766
ts_flat_file(false),
767+
ts_no_import_ext(false),
766768
no_leak_private_annotations(false),
767769
require_json_eof(true),
768770
mini_reflect(IDLOptions::kNone),

src/flatc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
613613
opts.json_nested_legacy_flatbuffers = true;
614614
} else if (arg == "--ts-flat-files") {
615615
opts.ts_flat_file = true;
616+
} else if (arg == "--ts-no-import-ext") {
617+
opts.ts_no_import_ext = true;
616618
} else if (arg == "--no-leak-private-annotation") {
617619
opts.no_leak_private_annotations = true;
618620
} else if (arg == "--annotate") {

src/idl_gen_ts.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,16 @@ class TsGenerator : public BaseGenerator {
256256
// specified here? Should we always be adding the "./" for a relative
257257
// path or turn it off if --include-prefix is specified, or something
258258
// else?
259+
std::string import_extension = parser_.opts.ts_no_import_ext ? "" : ".js";
259260
std::string include_name =
260-
"./" + flatbuffers::StripExtension(include_file);
261+
"./" + flatbuffers::StripExtension(include_file) + import_extension;
261262
code += "import {";
262263
for (const auto &pair : it.second) {
263264
code += namer_.EscapeKeyword(pair.first) + " as " +
264265
namer_.EscapeKeyword(pair.second) + ", ";
265266
}
266267
code.resize(code.size() - 2);
267-
code += "} from '" + include_name + ".js';\n";
268+
code += "} from '" + include_name + "';\n";
268269
}
269270
code += "\n";
270271
}
@@ -883,10 +884,11 @@ class TsGenerator : public BaseGenerator {
883884
import.object_name = object_name;
884885
import.bare_file_path = bare_file_path;
885886
import.rel_file_path = rel_file_path;
887+
std::string import_extension = parser_.opts.ts_no_import_ext ? "" : ".js";
886888
import.import_statement = "import { " + symbols_expression + " } from '" +
887-
rel_file_path + ".js';";
889+
rel_file_path + import_extension + "';";
888890
import.export_statement = "export { " + symbols_expression + " } from '." +
889-
bare_file_path + ".js';";
891+
bare_file_path + import_extension + "';";
890892
import.dependency = &dependency;
891893
import.dependent = &dependent;
892894

tests/ts/TypeScriptTest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
8484
schema="../optional_scalars.fbs",
8585
)
8686

87+
flatc(
88+
options=["--ts", "--reflect-names", "--gen-name-strings", "--ts-no-import-ext"],
89+
schema="../optional_scalars.fbs",
90+
prefix="no_import_ext",
91+
)
92+
8793
flatc(
8894
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
8995
schema=[
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
export var OptionalByte;
3+
(function (OptionalByte) {
4+
OptionalByte[OptionalByte["None"] = 0] = "None";
5+
OptionalByte[OptionalByte["One"] = 1] = "One";
6+
OptionalByte[OptionalByte["Two"] = 2] = "Two";
7+
})(OptionalByte || (OptionalByte = {}));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
export enum OptionalByte {
4+
None = 0,
5+
One = 1,
6+
Two = 2
7+
}

0 commit comments

Comments
 (0)