Skip to content

Commit 97c8335

Browse files
jmillanbjornharrtelldbaileychess
committed
TS/JS: Export object based classes on entry (#7822)
* TS/JS: Export object based classes on entry Along with the non object ones, for consistency. This is a regression introduced recently. Before: `export { UpdateSettingsRequest } from './worker/update-settings-request.js';` Now: `export { UpdateSettingsRequest, UpdateSettingsRequestT } from './worker/update-settings-request.js';` * only export object based classes for structs Enums are not elegible. --------- Co-authored-by: Björn Harrtell <bjornharrtell@users.noreply.github.com> Co-authored-by: Derek Bailey <derekbailey@google.com>
1 parent 0188b46 commit 97c8335

29 files changed

+187
-113
lines changed

src/idl_gen_ts.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ class TsGenerator : public BaseGenerator {
255255

256256
for (const auto &it : ns_defs_) {
257257
code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";
258-
259258
// export all definitions in ns entry point module
260259
int export_counter = 0;
261260
for (const auto &def : it.second.definitions) {
@@ -281,7 +280,14 @@ class TsGenerator : public BaseGenerator {
281280
base_name_rel += base_file_name;
282281
auto ts_file_path_rel = base_name_rel + ".ts";
283282
auto type_name = def.first;
284-
code += "export { " + type_name + " } from '";
283+
auto fully_qualified_type_name =
284+
it.second.ns->GetFullyQualifiedName(type_name);
285+
auto is_struct = parser_.structs_.Lookup(fully_qualified_type_name);
286+
code += "export { " + type_name;
287+
if (parser_.opts.generate_object_based_api && is_struct) {
288+
code += ", " + type_name + parser_.opts.object_suffix;
289+
}
290+
code += " } from '";
285291
std::string import_extension =
286292
parser_.opts.ts_no_import_ext ? "" : ".js";
287293
code += base_name_rel + import_extension + "';\n";

tests/ts/arrays_test_complex/arrays_test_complex_generated.cjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
1818
return to;
1919
};
2020
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21+
// If the importer is in node compatibility mode or this is not an ESM
22+
// file that has been converted to a CommonJS file using a Babel-
23+
// compatible transform (i.e. "__esModule" has not been set), then set
24+
// "default" to the CommonJS "module.exports" for node compatibility.
2125
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
2226
mod
2327
));
@@ -27,10 +31,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
2731
var example_exports = {};
2832
__export(example_exports, {
2933
ArrayStruct: () => ArrayStruct,
34+
ArrayStructT: () => ArrayStructT,
3035
ArrayTable: () => ArrayTable,
36+
ArrayTableT: () => ArrayTableT,
3137
InnerStruct: () => InnerStruct,
38+
InnerStructT: () => InnerStructT,
3239
NestedStruct: () => NestedStruct,
40+
NestedStructT: () => NestedStructT,
3341
OuterStruct: () => OuterStruct,
42+
OuterStructT: () => OuterStructT,
3443
TestEnum: () => TestEnum
3544
});
3645
module.exports = __toCommonJS(example_exports);
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { ArrayStruct } from './example/array-struct.js';
2-
export { ArrayTable } from './example/array-table.js';
3-
export { InnerStruct } from './example/inner-struct.js';
4-
export { NestedStruct } from './example/nested-struct.js';
5-
export { OuterStruct } from './example/outer-struct.js';
1+
export { ArrayStruct, ArrayStructT } from './example/array-struct.js';
2+
export { ArrayTable, ArrayTableT } from './example/array-table.js';
3+
export { InnerStruct, InnerStructT } from './example/inner-struct.js';
4+
export { NestedStruct, NestedStructT } from './example/nested-struct.js';
5+
export { OuterStruct, OuterStructT } from './example/outer-struct.js';
66
export { TestEnum } from './example/test-enum.js';
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// automatically generated by the FlatBuffers compiler, do not modify
2-
export { ArrayStruct } from './example/array-struct.js';
3-
export { ArrayTable } from './example/array-table.js';
4-
export { InnerStruct } from './example/inner-struct.js';
5-
export { NestedStruct } from './example/nested-struct.js';
6-
export { OuterStruct } from './example/outer-struct.js';
2+
export { ArrayStruct, ArrayStructT } from './example/array-struct.js';
3+
export { ArrayTable, ArrayTableT } from './example/array-table.js';
4+
export { InnerStruct, InnerStructT } from './example/inner-struct.js';
5+
export { NestedStruct, NestedStructT } from './example/nested-struct.js';
6+
export { OuterStruct, OuterStructT } from './example/outer-struct.js';
77
export { TestEnum } from './example/test-enum.js';
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// automatically generated by the FlatBuffers compiler, do not modify
22

3-
export { ArrayStruct } from './example/array-struct.js';
4-
export { ArrayTable } from './example/array-table.js';
5-
export { InnerStruct } from './example/inner-struct.js';
6-
export { NestedStruct } from './example/nested-struct.js';
7-
export { OuterStruct } from './example/outer-struct.js';
3+
export { ArrayStruct, ArrayStructT } from './example/array-struct.js';
4+
export { ArrayTable, ArrayTableT } from './example/array-table.js';
5+
export { InnerStruct, InnerStructT } from './example/inner-struct.js';
6+
export { NestedStruct, NestedStructT } from './example/nested-struct.js';
7+
export { OuterStruct, OuterStructT } from './example/outer-struct.js';
88
export { TestEnum } from './example/test-enum.js';

tests/ts/monster_test.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { TableA } from './table-a.js';
1+
export { TableA, TableAT } from './table-a.js';
22
export * as MyGame from './my-game.js';

tests/ts/monster_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// automatically generated by the FlatBuffers compiler, do not modify
2-
export { TableA } from './table-a.js';
2+
export { TableA, TableAT } from './table-a.js';
33
export * as MyGame from './my-game.js';

tests/ts/monster_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// automatically generated by the FlatBuffers compiler, do not modify
22

3-
export { TableA } from './table-a.js';
3+
export { TableA, TableAT } from './table-a.js';
44
export * as MyGame from './my-game.js';

tests/ts/monster_test_generated.cjs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
1818
return to;
1919
};
2020
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21+
// If the importer is in node compatibility mode or this is not an ESM
22+
// file that has been converted to a CommonJS file using a Babel-
23+
// compatible transform (i.e. "__esModule" has not been set), then set
24+
// "default" to the CommonJS "module.exports" for node compatibility.
2125
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
2226
mod
2327
));
@@ -27,7 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
2731
var monster_test_exports = {};
2832
__export(monster_test_exports, {
2933
MyGame: () => my_game_exports,
30-
TableA: () => TableA
34+
TableA: () => TableA,
35+
TableAT: () => TableAT
3136
});
3237
module.exports = __toCommonJS(monster_test_exports);
3338

@@ -167,6 +172,7 @@ __export(my_game_exports, {
167172
Example: () => example_exports,
168173
Example2: () => example2_exports,
169174
InParentNamespace: () => InParentNamespace,
175+
InParentNamespaceT: () => InParentNamespaceT,
170176
OtherNameSpace: () => other_name_space_exports
171177
});
172178

@@ -227,21 +233,31 @@ var InParentNamespaceT = class {
227233
var example_exports = {};
228234
__export(example_exports, {
229235
Ability: () => Ability,
236+
AbilityT: () => AbilityT,
230237
Any: () => Any,
231238
AnyAmbiguousAliases: () => AnyAmbiguousAliases,
232239
AnyUniqueAliases: () => AnyUniqueAliases,
233240
Color: () => Color,
234241
LongEnum: () => LongEnum,
235242
Monster: () => Monster2,
243+
MonsterT: () => MonsterT2,
236244
Race: () => Race,
237245
Referrable: () => Referrable,
246+
ReferrableT: () => ReferrableT,
238247
Stat: () => Stat,
248+
StatT: () => StatT,
239249
StructOfStructs: () => StructOfStructs,
240250
StructOfStructsOfStructs: () => StructOfStructsOfStructs,
251+
StructOfStructsOfStructsT: () => StructOfStructsOfStructsT,
252+
StructOfStructsT: () => StructOfStructsT,
241253
Test: () => Test,
242254
TestSimpleTableWithEnum: () => TestSimpleTableWithEnum,
255+
TestSimpleTableWithEnumT: () => TestSimpleTableWithEnumT,
256+
TestT: () => TestT,
243257
TypeAliases: () => TypeAliases,
244-
Vec3: () => Vec3
258+
TypeAliasesT: () => TypeAliasesT,
259+
Vec3: () => Vec3,
260+
Vec3T: () => Vec3T
245261
});
246262

247263
// my-game/example/ability.js
@@ -916,6 +932,10 @@ var Monster2 = class {
916932
const offset = this.bb.__offset(this.bb_pos, 24);
917933
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
918934
}
935+
/**
936+
* an example documentation comment: this will end up in the generated code
937+
* multiline too
938+
*/
919939
testarrayoftables(index, obj) {
920940
const offset = this.bb.__offset(this.bb_pos, 26);
921941
return offset ? (obj || new Monster2()).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos + offset) + index * 4), this.bb) : null;
@@ -2502,15 +2522,18 @@ var TypeAliasesT = class {
25022522
// my-game/example2.js
25032523
var example2_exports = {};
25042524
__export(example2_exports, {
2505-
Monster: () => Monster
2525+
Monster: () => Monster,
2526+
MonsterT: () => MonsterT
25062527
});
25072528

25082529
// my-game/other-name-space.js
25092530
var other_name_space_exports = {};
25102531
__export(other_name_space_exports, {
25112532
FromInclude: () => FromInclude,
25122533
TableB: () => TableB,
2513-
Unused: () => Unused
2534+
TableBT: () => TableBT,
2535+
Unused: () => Unused,
2536+
UnusedT: () => UnusedT
25142537
});
25152538

25162539
// my-game/other-name-space/from-include.js

tests/ts/my-game.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { InParentNamespace } from './my-game/in-parent-namespace.js';
1+
export { InParentNamespace, InParentNamespaceT } from './my-game/in-parent-namespace.js';
22
export * as Example from './my-game/example.js';
33
export * as Example2 from './my-game/example2.js';
44
export * as OtherNameSpace from './my-game/other-name-space.js';

0 commit comments

Comments
 (0)