Skip to content

Commit 00eec24

Browse files
[TS] Fix relative paths for exports (#8517)
Fixes an issue where exports were using incorrect relative paths for >=3 namespace levels. This is fixed by making the starting range of the namespace components relative to the amount of components. Co-authored-by: Björn Harrtell <[email protected]>
1 parent b8db3a9 commit 00eec24

File tree

13 files changed

+255
-11
lines changed

13 files changed

+255
-11
lines changed

src/idl_gen_ts.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,9 @@ class TsGenerator : public BaseGenerator {
263263
for (const auto &def : it.second.definitions) {
264264
std::vector<std::string> rel_components;
265265
// build path for root level vs child level
266-
if (it.second.ns->components.size() > 1)
267-
std::copy(it.second.ns->components.begin() + 1,
268-
it.second.ns->components.end(),
269-
std::back_inserter(rel_components));
270-
else
271-
std::copy(it.second.ns->components.begin(),
272-
it.second.ns->components.end(),
273-
std::back_inserter(rel_components));
266+
if (it.second.ns->components.size() > 0) {
267+
rel_components.push_back(it.second.ns->components.back());
268+
}
274269
auto base_file_name =
275270
namer_.File(*(def.second), SkipFile::SuffixAndExtension);
276271
auto base_name =
@@ -303,8 +298,12 @@ class TsGenerator : public BaseGenerator {
303298
if (it2.second.ns->components.size() != child_ns_level) continue;
304299
auto ts_file_path = it2.second.path + ".ts";
305300
code += "export * as " + it2.second.symbolic_name + " from './";
306-
std::string rel_path = it2.second.path;
307-
code += rel_path + ".js';\n";
301+
int count = it2.second.ns->components.size() > 1 ? 2 : 1;
302+
std::vector<std::string> rel_path;
303+
std::copy(it2.second.ns->components.end() - count,
304+
it2.second.ns->components.end(),
305+
std::back_inserter(rel_path));
306+
code += namer_.Directories(rel_path, SkipDir::OutputPathAndTrailingPathSeparator) + ".js';\n";
308307
export_counter++;
309308
}
310309

tests/long_namespace.fbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace com.company.test;
2+
3+
table Person {
4+
name:string;
5+
age:short;
6+
}
7+
root_type Person;

tests/longer_namespace.fbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace longer_namespace.a.b.c;
2+
3+
table Person {
4+
name:string;
5+
age:short;
6+
}
7+
root_type Person;

tests/ts/TypeScriptTest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def esbuild(input, output):
122122
schema="../union_underlying_type_test.fbs"
123123
)
124124

125+
flatc(options=["--ts"], schema="../long_namespace.fbs")
126+
flatc(options=["--ts"], schema="../longer_namespace.fbs")
127+
125128
print("Running TypeScript Compiler...")
126129
check_call(["tsc"])
127130
print("Running TypeScript Compiler in old node resolution mode for no_import_ext...")

tests/ts/com/company/test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
4+
5+
export { Person } from './test/person.js';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
4+
5+
import * as flatbuffers from 'flatbuffers';
6+
7+
8+
9+
export class Person {
10+
bb: flatbuffers.ByteBuffer|null = null;
11+
bb_pos = 0;
12+
__init(i:number, bb:flatbuffers.ByteBuffer):Person {
13+
this.bb_pos = i;
14+
this.bb = bb;
15+
return this;
16+
}
17+
18+
static getRootAsPerson(bb:flatbuffers.ByteBuffer, obj?:Person):Person {
19+
return (obj || new Person()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
20+
}
21+
22+
static getSizePrefixedRootAsPerson(bb:flatbuffers.ByteBuffer, obj?:Person):Person {
23+
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
24+
return (obj || new Person()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
25+
}
26+
27+
name():string|null
28+
name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
29+
name(optionalEncoding?:any):string|Uint8Array|null {
30+
const offset = this.bb!.__offset(this.bb_pos, 4);
31+
return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
32+
}
33+
34+
age():number {
35+
const offset = this.bb!.__offset(this.bb_pos, 6);
36+
return offset ? this.bb!.readInt16(this.bb_pos + offset) : 0;
37+
}
38+
39+
static startPerson(builder:flatbuffers.Builder) {
40+
builder.startObject(2);
41+
}
42+
43+
static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) {
44+
builder.addFieldOffset(0, nameOffset, 0);
45+
}
46+
47+
static addAge(builder:flatbuffers.Builder, age:number) {
48+
builder.addFieldInt16(1, age, 0);
49+
}
50+
51+
static endPerson(builder:flatbuffers.Builder):flatbuffers.Offset {
52+
const offset = builder.endObject();
53+
return offset;
54+
}
55+
56+
static finishPersonBuffer(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {
57+
builder.finish(offset);
58+
}
59+
60+
static finishSizePrefixedPersonBuffer(builder:flatbuffers.Builder, offset:flatbuffers.Offset) {
61+
builder.finish(offset, undefined, true);
62+
}
63+
64+
static createPerson(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset, age:number):flatbuffers.Offset {
65+
Person.startPerson(builder);
66+
Person.addName(builder, nameOffset);
67+
Person.addAge(builder, age);
68+
return Person.endPerson(builder);
69+
}
70+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { Person } from './c/person.js';

tests/ts/longer-namespace/a/b/c.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
3+
export { Person } from './c/person.js';

tests/ts/longer-namespace/a/b/c.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// automatically generated by the FlatBuffers compiler, do not modify
2+
3+
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
4+
5+
export { Person } from './c/person.js';
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as flatbuffers from 'flatbuffers';
2+
export declare class Person {
3+
bb: flatbuffers.ByteBuffer | null;
4+
bb_pos: number;
5+
__init(i: number, bb: flatbuffers.ByteBuffer): Person;
6+
static getRootAsPerson(bb: flatbuffers.ByteBuffer, obj?: Person): Person;
7+
static getSizePrefixedRootAsPerson(bb: flatbuffers.ByteBuffer, obj?: Person): Person;
8+
name(): string | null;
9+
name(optionalEncoding: flatbuffers.Encoding): string | Uint8Array | null;
10+
age(): number;
11+
static startPerson(builder: flatbuffers.Builder): void;
12+
static addName(builder: flatbuffers.Builder, nameOffset: flatbuffers.Offset): void;
13+
static addAge(builder: flatbuffers.Builder, age: number): void;
14+
static endPerson(builder: flatbuffers.Builder): flatbuffers.Offset;
15+
static finishPersonBuffer(builder: flatbuffers.Builder, offset: flatbuffers.Offset): void;
16+
static finishSizePrefixedPersonBuffer(builder: flatbuffers.Builder, offset: flatbuffers.Offset): void;
17+
static createPerson(builder: flatbuffers.Builder, nameOffset: flatbuffers.Offset, age: number): flatbuffers.Offset;
18+
}

0 commit comments

Comments
 (0)