Skip to content

Commit

Permalink
TS/JS: Export object based classes on entry (#7822)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Mar 15, 2023
1 parent 0188b46 commit 97c8335
Show file tree
Hide file tree
Showing 29 changed files with 187 additions and 113 deletions.
10 changes: 8 additions & 2 deletions src/idl_gen_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ class TsGenerator : public BaseGenerator {

for (const auto &it : ns_defs_) {
code = "// " + std::string(FlatBuffersGeneratedWarning()) + "\n\n";

// export all definitions in ns entry point module
int export_counter = 0;
for (const auto &def : it.second.definitions) {
Expand All @@ -281,7 +280,14 @@ class TsGenerator : public BaseGenerator {
base_name_rel += base_file_name;
auto ts_file_path_rel = base_name_rel + ".ts";
auto type_name = def.first;
code += "export { " + type_name + " } from '";
auto fully_qualified_type_name =
it.second.ns->GetFullyQualifiedName(type_name);
auto is_struct = parser_.structs_.Lookup(fully_qualified_type_name);
code += "export { " + type_name;
if (parser_.opts.generate_object_based_api && is_struct) {
code += ", " + type_name + parser_.opts.object_suffix;
}
code += " } from '";
std::string import_extension =
parser_.opts.ts_no_import_ext ? "" : ".js";
code += base_name_rel + import_extension + "';\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
Expand All @@ -27,10 +31,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
var example_exports = {};
__export(example_exports, {
ArrayStruct: () => ArrayStruct,
ArrayStructT: () => ArrayStructT,
ArrayTable: () => ArrayTable,
ArrayTableT: () => ArrayTableT,
InnerStruct: () => InnerStruct,
InnerStructT: () => InnerStructT,
NestedStruct: () => NestedStruct,
NestedStructT: () => NestedStructT,
OuterStruct: () => OuterStruct,
OuterStructT: () => OuterStructT,
TestEnum: () => TestEnum
});
module.exports = __toCommonJS(example_exports);
Expand Down
10 changes: 5 additions & 5 deletions tests/ts/arrays_test_complex/my-game/example.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { ArrayStruct } from './example/array-struct.js';
export { ArrayTable } from './example/array-table.js';
export { InnerStruct } from './example/inner-struct.js';
export { NestedStruct } from './example/nested-struct.js';
export { OuterStruct } from './example/outer-struct.js';
export { ArrayStruct, ArrayStructT } from './example/array-struct.js';
export { ArrayTable, ArrayTableT } from './example/array-table.js';
export { InnerStruct, InnerStructT } from './example/inner-struct.js';
export { NestedStruct, NestedStructT } from './example/nested-struct.js';
export { OuterStruct, OuterStructT } from './example/outer-struct.js';
export { TestEnum } from './example/test-enum.js';
10 changes: 5 additions & 5 deletions tests/ts/arrays_test_complex/my-game/example.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { ArrayStruct } from './example/array-struct.js';
export { ArrayTable } from './example/array-table.js';
export { InnerStruct } from './example/inner-struct.js';
export { NestedStruct } from './example/nested-struct.js';
export { OuterStruct } from './example/outer-struct.js';
export { ArrayStruct, ArrayStructT } from './example/array-struct.js';
export { ArrayTable, ArrayTableT } from './example/array-table.js';
export { InnerStruct, InnerStructT } from './example/inner-struct.js';
export { NestedStruct, NestedStructT } from './example/nested-struct.js';
export { OuterStruct, OuterStructT } from './example/outer-struct.js';
export { TestEnum } from './example/test-enum.js';
10 changes: 5 additions & 5 deletions tests/ts/arrays_test_complex/my-game/example.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { ArrayStruct } from './example/array-struct.js';
export { ArrayTable } from './example/array-table.js';
export { InnerStruct } from './example/inner-struct.js';
export { NestedStruct } from './example/nested-struct.js';
export { OuterStruct } from './example/outer-struct.js';
export { ArrayStruct, ArrayStructT } from './example/array-struct.js';
export { ArrayTable, ArrayTableT } from './example/array-table.js';
export { InnerStruct, InnerStructT } from './example/inner-struct.js';
export { NestedStruct, NestedStructT } from './example/nested-struct.js';
export { OuterStruct, OuterStructT } from './example/outer-struct.js';
export { TestEnum } from './example/test-enum.js';
2 changes: 1 addition & 1 deletion tests/ts/monster_test.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { TableA } from './table-a.js';
export { TableA, TableAT } from './table-a.js';
export * as MyGame from './my-game.js';
2 changes: 1 addition & 1 deletion tests/ts/monster_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { TableA } from './table-a.js';
export { TableA, TableAT } from './table-a.js';
export * as MyGame from './my-game.js';
2 changes: 1 addition & 1 deletion tests/ts/monster_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { TableA } from './table-a.js';
export { TableA, TableAT } from './table-a.js';
export * as MyGame from './my-game.js';
31 changes: 27 additions & 4 deletions tests/ts/monster_test_generated.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
Expand All @@ -27,7 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
var monster_test_exports = {};
__export(monster_test_exports, {
MyGame: () => my_game_exports,
TableA: () => TableA
TableA: () => TableA,
TableAT: () => TableAT
});
module.exports = __toCommonJS(monster_test_exports);

Expand Down Expand Up @@ -167,6 +172,7 @@ __export(my_game_exports, {
Example: () => example_exports,
Example2: () => example2_exports,
InParentNamespace: () => InParentNamespace,
InParentNamespaceT: () => InParentNamespaceT,
OtherNameSpace: () => other_name_space_exports
});

Expand Down Expand Up @@ -227,21 +233,31 @@ var InParentNamespaceT = class {
var example_exports = {};
__export(example_exports, {
Ability: () => Ability,
AbilityT: () => AbilityT,
Any: () => Any,
AnyAmbiguousAliases: () => AnyAmbiguousAliases,
AnyUniqueAliases: () => AnyUniqueAliases,
Color: () => Color,
LongEnum: () => LongEnum,
Monster: () => Monster2,
MonsterT: () => MonsterT2,
Race: () => Race,
Referrable: () => Referrable,
ReferrableT: () => ReferrableT,
Stat: () => Stat,
StatT: () => StatT,
StructOfStructs: () => StructOfStructs,
StructOfStructsOfStructs: () => StructOfStructsOfStructs,
StructOfStructsOfStructsT: () => StructOfStructsOfStructsT,
StructOfStructsT: () => StructOfStructsT,
Test: () => Test,
TestSimpleTableWithEnum: () => TestSimpleTableWithEnum,
TestSimpleTableWithEnumT: () => TestSimpleTableWithEnumT,
TestT: () => TestT,
TypeAliases: () => TypeAliases,
Vec3: () => Vec3
TypeAliasesT: () => TypeAliasesT,
Vec3: () => Vec3,
Vec3T: () => Vec3T
});

// my-game/example/ability.js
Expand Down Expand Up @@ -916,6 +932,10 @@ var Monster2 = class {
const offset = this.bb.__offset(this.bb_pos, 24);
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
}
/**
* an example documentation comment: this will end up in the generated code
* multiline too
*/
testarrayoftables(index, obj) {
const offset = this.bb.__offset(this.bb_pos, 26);
return offset ? (obj || new Monster2()).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos + offset) + index * 4), this.bb) : null;
Expand Down Expand Up @@ -2502,15 +2522,18 @@ var TypeAliasesT = class {
// my-game/example2.js
var example2_exports = {};
__export(example2_exports, {
Monster: () => Monster
Monster: () => Monster,
MonsterT: () => MonsterT
});

// my-game/other-name-space.js
var other_name_space_exports = {};
__export(other_name_space_exports, {
FromInclude: () => FromInclude,
TableB: () => TableB,
Unused: () => Unused
TableBT: () => TableBT,
Unused: () => Unused,
UnusedT: () => UnusedT
});

// my-game/other-name-space/from-include.js
Expand Down
2 changes: 1 addition & 1 deletion tests/ts/my-game.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { InParentNamespace } from './my-game/in-parent-namespace.js';
export { InParentNamespace, InParentNamespaceT } from './my-game/in-parent-namespace.js';
export * as Example from './my-game/example.js';
export * as Example2 from './my-game/example2.js';
export * as OtherNameSpace from './my-game/other-name-space.js';
2 changes: 1 addition & 1 deletion tests/ts/my-game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { InParentNamespace } from './my-game/in-parent-namespace.js';
export { InParentNamespace, InParentNamespaceT } from './my-game/in-parent-namespace.js';
export * as Example from './my-game/example.js';
export * as Example2 from './my-game/example2.js';
export * as OtherNameSpace from './my-game/other-name-space.js';
2 changes: 1 addition & 1 deletion tests/ts/my-game.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { InParentNamespace } from './my-game/in-parent-namespace.js';
export { InParentNamespace, InParentNamespaceT } from './my-game/in-parent-namespace.js';
export * as Example from './my-game/example.js';
export * as Example2 from './my-game/example2.js';
export * as OtherNameSpace from './my-game/other-name-space.js';
20 changes: 10 additions & 10 deletions tests/ts/my-game/example.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export { Ability } from './example/ability.js';
export { Ability, AbilityT } from './example/ability.js';
export { Any } from './example/any.js';
export { AnyAmbiguousAliases } from './example/any-ambiguous-aliases.js';
export { AnyUniqueAliases } from './example/any-unique-aliases.js';
export { Color } from './example/color.js';
export { LongEnum } from './example/long-enum.js';
export { Monster } from './example/monster.js';
export { Monster, MonsterT } from './example/monster.js';
export { Race } from './example/race.js';
export { Referrable } from './example/referrable.js';
export { Stat } from './example/stat.js';
export { StructOfStructs } from './example/struct-of-structs.js';
export { StructOfStructsOfStructs } from './example/struct-of-structs-of-structs.js';
export { Test } from './example/test.js';
export { TestSimpleTableWithEnum } from './example/test-simple-table-with-enum.js';
export { TypeAliases } from './example/type-aliases.js';
export { Vec3 } from './example/vec3.js';
export { Referrable, ReferrableT } from './example/referrable.js';
export { Stat, StatT } from './example/stat.js';
export { StructOfStructs, StructOfStructsT } from './example/struct-of-structs.js';
export { StructOfStructsOfStructs, StructOfStructsOfStructsT } from './example/struct-of-structs-of-structs.js';
export { Test, TestT } from './example/test.js';
export { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './example/test-simple-table-with-enum.js';
export { TypeAliases, TypeAliasesT } from './example/type-aliases.js';
export { Vec3, Vec3T } from './example/vec3.js';
20 changes: 10 additions & 10 deletions tests/ts/my-game/example.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { Ability } from './example/ability.js';
export { Ability, AbilityT } from './example/ability.js';
export { Any } from './example/any.js';
export { AnyAmbiguousAliases } from './example/any-ambiguous-aliases.js';
export { AnyUniqueAliases } from './example/any-unique-aliases.js';
export { Color } from './example/color.js';
export { LongEnum } from './example/long-enum.js';
export { Monster } from './example/monster.js';
export { Monster, MonsterT } from './example/monster.js';
export { Race } from './example/race.js';
export { Referrable } from './example/referrable.js';
export { Stat } from './example/stat.js';
export { StructOfStructs } from './example/struct-of-structs.js';
export { StructOfStructsOfStructs } from './example/struct-of-structs-of-structs.js';
export { Test } from './example/test.js';
export { TestSimpleTableWithEnum } from './example/test-simple-table-with-enum.js';
export { TypeAliases } from './example/type-aliases.js';
export { Vec3 } from './example/vec3.js';
export { Referrable, ReferrableT } from './example/referrable.js';
export { Stat, StatT } from './example/stat.js';
export { StructOfStructs, StructOfStructsT } from './example/struct-of-structs.js';
export { StructOfStructsOfStructs, StructOfStructsOfStructsT } from './example/struct-of-structs-of-structs.js';
export { Test, TestT } from './example/test.js';
export { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './example/test-simple-table-with-enum.js';
export { TypeAliases, TypeAliasesT } from './example/type-aliases.js';
export { Vec3, Vec3T } from './example/vec3.js';
20 changes: 10 additions & 10 deletions tests/ts/my-game/example.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { Ability } from './example/ability.js';
export { Ability, AbilityT } from './example/ability.js';
export { Any } from './example/any.js';
export { AnyAmbiguousAliases } from './example/any-ambiguous-aliases.js';
export { AnyUniqueAliases } from './example/any-unique-aliases.js';
export { Color } from './example/color.js';
export { LongEnum } from './example/long-enum.js';
export { Monster } from './example/monster.js';
export { Monster, MonsterT } from './example/monster.js';
export { Race } from './example/race.js';
export { Referrable } from './example/referrable.js';
export { Stat } from './example/stat.js';
export { StructOfStructs } from './example/struct-of-structs.js';
export { StructOfStructsOfStructs } from './example/struct-of-structs-of-structs.js';
export { Test } from './example/test.js';
export { TestSimpleTableWithEnum } from './example/test-simple-table-with-enum.js';
export { TypeAliases } from './example/type-aliases.js';
export { Vec3 } from './example/vec3.js';
export { Referrable, ReferrableT } from './example/referrable.js';
export { Stat, StatT } from './example/stat.js';
export { StructOfStructs, StructOfStructsT } from './example/struct-of-structs.js';
export { StructOfStructsOfStructs, StructOfStructsOfStructsT } from './example/struct-of-structs-of-structs.js';
export { Test, TestT } from './example/test.js';
export { TestSimpleTableWithEnum, TestSimpleTableWithEnumT } from './example/test-simple-table-with-enum.js';
export { TypeAliases, TypeAliasesT } from './example/type-aliases.js';
export { Vec3, Vec3T } from './example/vec3.js';
2 changes: 1 addition & 1 deletion tests/ts/my-game/example2.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { Monster } from './example2/monster.js';
export { Monster, MonsterT } from './example2/monster.js';
2 changes: 1 addition & 1 deletion tests/ts/my-game/example2.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { Monster } from './example2/monster.js';
export { Monster, MonsterT } from './example2/monster.js';
2 changes: 1 addition & 1 deletion tests/ts/my-game/example2.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { Monster } from './example2/monster.js';
export { Monster, MonsterT } from './example2/monster.js';
4 changes: 2 additions & 2 deletions tests/ts/my-game/other-name-space.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { FromInclude } from './other-name-space/from-include.js';
export { TableB } from './other-name-space/table-b.js';
export { Unused } from './other-name-space/unused.js';
export { TableB, TableBT } from './other-name-space/table-b.js';
export { Unused, UnusedT } from './other-name-space/unused.js';
4 changes: 2 additions & 2 deletions tests/ts/my-game/other-name-space.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { FromInclude } from './other-name-space/from-include.js';
export { TableB } from './other-name-space/table-b.js';
export { Unused } from './other-name-space/unused.js';
export { TableB, TableBT } from './other-name-space/table-b.js';
export { Unused, UnusedT } from './other-name-space/unused.js';
4 changes: 2 additions & 2 deletions tests/ts/my-game/other-name-space.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify

export { FromInclude } from './other-name-space/from-include.js';
export { TableB } from './other-name-space/table-b.js';
export { Unused } from './other-name-space/unused.js';
export { TableB, TableBT } from './other-name-space/table-b.js';
export { Unused, UnusedT } from './other-name-space/unused.js';
18 changes: 9 additions & 9 deletions tests/ts/reflection.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export { AdvancedFeatures } from './reflection/advanced-features.js';
export { BaseType } from './reflection/base-type.js';
export { Enum } from './reflection/enum.js';
export { EnumVal } from './reflection/enum-val.js';
export { Field } from './reflection/field.js';
export { KeyValue } from './reflection/key-value.js';
export { Enum, EnumT } from './reflection/enum.js';
export { EnumVal, EnumValT } from './reflection/enum-val.js';
export { Field, FieldT } from './reflection/field.js';
export { KeyValue, KeyValueT } from './reflection/key-value.js';
export { Object_ } from './reflection/object.js';
export { RPCCall } from './reflection/rpccall.js';
export { Schema } from './reflection/schema.js';
export { SchemaFile } from './reflection/schema-file.js';
export { Service } from './reflection/service.js';
export { Type } from './reflection/type.js';
export { RPCCall, RPCCallT } from './reflection/rpccall.js';
export { Schema, SchemaT } from './reflection/schema.js';
export { SchemaFile, SchemaFileT } from './reflection/schema-file.js';
export { Service, ServiceT } from './reflection/service.js';
export { Type, TypeT } from './reflection/type.js';
18 changes: 9 additions & 9 deletions tests/ts/reflection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { AdvancedFeatures } from './reflection/advanced-features.js';
export { BaseType } from './reflection/base-type.js';
export { Enum } from './reflection/enum.js';
export { EnumVal } from './reflection/enum-val.js';
export { Field } from './reflection/field.js';
export { KeyValue } from './reflection/key-value.js';
export { Enum, EnumT } from './reflection/enum.js';
export { EnumVal, EnumValT } from './reflection/enum-val.js';
export { Field, FieldT } from './reflection/field.js';
export { KeyValue, KeyValueT } from './reflection/key-value.js';
export { Object_ } from './reflection/object.js';
export { RPCCall } from './reflection/rpccall.js';
export { Schema } from './reflection/schema.js';
export { SchemaFile } from './reflection/schema-file.js';
export { Service } from './reflection/service.js';
export { Type } from './reflection/type.js';
export { RPCCall, RPCCallT } from './reflection/rpccall.js';
export { Schema, SchemaT } from './reflection/schema.js';
export { SchemaFile, SchemaFileT } from './reflection/schema-file.js';
export { Service, ServiceT } from './reflection/service.js';
export { Type, TypeT } from './reflection/type.js';
Loading

0 comments on commit 97c8335

Please sign in to comment.