Skip to content

Commit d243b90

Browse files
[TS] Make strict compliant and improve typings (#7549)
* [TS] Make strict compliant and improve typings * clang-format * Code gen harmonize Co-authored-by: Derek Bailey <[email protected]>
1 parent 374f8fb commit d243b90

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+279
-268
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"scripts": {
1919
"test": "npm run compile && cd tests/ts && python3 ./TypeScriptTest.py",
20+
"lint": "eslint ts",
2021
"compile": "tsc && tsc -p tsconfig.mjs.json && rollup -c",
2122
"prepublishOnly": "npm install --only=dev && npm run compile"
2223
},

src/idl_gen_ts.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,10 @@ class TsGenerator : public BaseGenerator {
759759
import.object_name = object_name;
760760
import.bare_file_path = bare_file_path;
761761
import.rel_file_path = rel_file_path;
762-
import.import_statement =
763-
"import { " + symbols_expression + " } from '" + rel_file_path + ".js';";
764-
import.export_statement =
765-
"export { " + symbols_expression + " } from '." + bare_file_path + ".js';";
762+
import.import_statement = "import { " + symbols_expression + " } from '" +
763+
rel_file_path + ".js';";
764+
import.export_statement = "export { " + symbols_expression + " } from '." +
765+
bare_file_path + ".js';";
766766
import.dependency = &dependency;
767767
import.dependent = &dependent;
768768

@@ -903,7 +903,7 @@ class TsGenerator : public BaseGenerator {
903903
const auto conversion_function = GenUnionConvFuncName(enum_def);
904904

905905
ret = "(() => {\n";
906-
ret += " let temp = " + conversion_function + "(this." +
906+
ret += " const temp = " + conversion_function + "(this." +
907907
namer_.Method(field_name, "Type") + "(), " +
908908
field_binded_method + ");\n";
909909
ret += " if(temp === null) { return null; }\n";
@@ -916,17 +916,17 @@ class TsGenerator : public BaseGenerator {
916916
const auto conversion_function = GenUnionListConvFuncName(enum_def);
917917

918918
ret = "(() => {\n";
919-
ret += " let ret = [];\n";
919+
ret += " const ret = [];\n";
920920
ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." +
921921
namer_.Method(field_name, "TypeLength") + "()" +
922922
"; "
923923
"++targetEnumIndex) {\n";
924-
ret += " let targetEnum = this." +
924+
ret += " const targetEnum = this." +
925925
namer_.Method(field_name, "Type") + "(targetEnumIndex);\n";
926926
ret += " if(targetEnum === null || " + enum_type +
927927
"[targetEnum!] === 'NONE') { "
928928
"continue; }\n\n";
929-
ret += " let temp = " + conversion_function + "(targetEnum, " +
929+
ret += " const temp = " + conversion_function + "(targetEnum, " +
930930
field_binded_method + ", targetEnumIndex);\n";
931931
ret += " if(temp === null) { continue; }\n";
932932
ret += union_has_string ? " if(typeof temp === 'string') { "
@@ -1102,11 +1102,13 @@ class TsGenerator : public BaseGenerator {
11021102
switch (vectortype.base_type) {
11031103
case BASE_TYPE_STRUCT: {
11041104
const auto &sd = *field.value.type.struct_def;
1105-
field_type += GetTypeName(sd, /*object_api=*/true);
1106-
;
1105+
const auto field_type_name =
1106+
GetTypeName(sd, /*object_api=*/true);
1107+
field_type += field_type_name;
11071108
field_type += ")[]";
11081109

1109-
field_val = GenBBAccess() + ".createObjList(" +
1110+
field_val = GenBBAccess() + ".createObjList<" + vectortypename +
1111+
", " + field_type_name + ">(" +
11101112
field_binded_method + ", this." +
11111113
namer_.Method(field, "Length") + "())";
11121114

@@ -1128,7 +1130,7 @@ class TsGenerator : public BaseGenerator {
11281130

11291131
case BASE_TYPE_STRING: {
11301132
field_type += "string)[]";
1131-
field_val = GenBBAccess() + ".createScalarList(" +
1133+
field_val = GenBBAccess() + ".createScalarList<string>(" +
11321134
field_binded_method + ", this." +
11331135
namer_.Field(field, "Length") + "())";
11341136
field_offset_decl =
@@ -1162,9 +1164,9 @@ class TsGenerator : public BaseGenerator {
11621164
field_type += vectortypename;
11631165
}
11641166
field_type += ")[]";
1165-
field_val = GenBBAccess() + ".createScalarList(" +
1166-
field_binded_method + ", this." +
1167-
namer_.Method(field, "Length") + "())";
1167+
field_val = GenBBAccess() + ".createScalarList<" +
1168+
vectortypename + ">(" + field_binded_method +
1169+
", this." + namer_.Method(field, "Length") + "())";
11681170

11691171
field_offset_decl =
11701172
AddImport(imports, struct_def, struct_def).name + "." +
@@ -1260,7 +1262,7 @@ class TsGenerator : public BaseGenerator {
12601262
obj_api_class = "\n";
12611263
obj_api_class += "export class ";
12621264
obj_api_class += GetTypeName(struct_def, /*object_api=*/true);
1263-
obj_api_class += " {\n";
1265+
obj_api_class += " implements flatbuffers.IGeneratedObject {\n";
12641266
obj_api_class += constructor_func;
12651267
obj_api_class += pack_func_prototype + pack_func_offset_decl +
12661268
pack_func_create_call + "\n}";
@@ -1298,12 +1300,17 @@ class TsGenerator : public BaseGenerator {
12981300
}
12991301

13001302
const std::string object_name = GetTypeName(struct_def);
1303+
const std::string object_api_name = GetTypeName(struct_def, true);
13011304

13021305
// Emit constructor
13031306
GenDocComment(struct_def.doc_comment, code_ptr);
13041307
code += "export class ";
13051308
code += object_name;
1306-
code += " {\n";
1309+
if (parser.opts.generate_object_based_api)
1310+
code += " implements flatbuffers.IUnpackableObject<" + object_api_name +
1311+
"> {\n";
1312+
else
1313+
code += " {\n";
13071314
code += " bb: flatbuffers.ByteBuffer|null = null;\n";
13081315
code += " bb_pos = 0;\n";
13091316

tests/ts/TypeScriptTest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
6161

6262
print("Invoking flatc...")
6363
flatc(
64-
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
64+
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
6565
schema="../monster_test.fbs",
6666
include="../include_test",
6767
)
@@ -74,18 +74,18 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
7474
)
7575

7676
flatc(
77-
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
77+
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
7878
schema="../union_vector/union_vector.fbs",
7979
prefix="union_vector",
8080
)
8181

8282
flatc(
83-
options=["--ts", "--gen-name-strings"],
83+
options=["--ts", "--reflect-names", "--gen-name-strings"],
8484
schema="../optional_scalars.fbs",
8585
)
8686

8787
flatc(
88-
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
88+
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
8989
schema=[
9090
"typescript_keywords.fbs",
9191
"test_dir/typescript_include.fbs",
@@ -98,6 +98,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
9898
flatc(
9999
options=[
100100
"--ts",
101+
"--reflect-names",
101102
"--gen-name-strings",
102103
"--gen-mutable",
103104
"--gen-object-api",

tests/ts/my-game/example/ability.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as flatbuffers from 'flatbuffers';
44

55

66

7-
export class Ability {
7+
export class Ability implements flatbuffers.IUnpackableObject<AbilityT> {
88
bb: flatbuffers.ByteBuffer|null = null;
99
bb_pos = 0;
1010
__init(i:number, bb:flatbuffers.ByteBuffer):Ability {
@@ -61,7 +61,7 @@ unpackTo(_o: AbilityT): void {
6161
}
6262
}
6363

64-
export class AbilityT {
64+
export class AbilityT implements flatbuffers.IGeneratedObject {
6565
constructor(
6666
public id: number = 0,
6767
public distance: number = 0

tests/ts/my-game/example/monster.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,19 +889,19 @@ export class Monster {
889889
}
890890
unpack() {
891891
return new MonsterT((this.pos() !== null ? this.pos().unpack() : null), this.mana(), this.hp(), this.name(), this.bb.createScalarList(this.inventory.bind(this), this.inventoryLength()), this.color(), this.testType(), (() => {
892-
let temp = unionToAny(this.testType(), this.test.bind(this));
892+
const temp = unionToAny(this.testType(), this.test.bind(this));
893893
if (temp === null) {
894894
return null;
895895
}
896896
return temp.unpack();
897897
})(), this.bb.createObjList(this.test4.bind(this), this.test4Length()), this.bb.createScalarList(this.testarrayofstring.bind(this), this.testarrayofstringLength()), this.bb.createObjList(this.testarrayoftables.bind(this), this.testarrayoftablesLength()), (this.enemy() !== null ? this.enemy().unpack() : null), this.bb.createScalarList(this.testnestedflatbuffer.bind(this), this.testnestedflatbufferLength()), (this.testempty() !== null ? this.testempty().unpack() : null), this.testbool(), this.testhashs32Fnv1(), this.testhashu32Fnv1(), this.testhashs64Fnv1(), this.testhashu64Fnv1(), this.testhashs32Fnv1a(), this.testhashu32Fnv1a(), this.testhashs64Fnv1a(), this.testhashu64Fnv1a(), this.bb.createScalarList(this.testarrayofbools.bind(this), this.testarrayofboolsLength()), this.testf(), this.testf2(), this.testf3(), this.bb.createScalarList(this.testarrayofstring2.bind(this), this.testarrayofstring2Length()), this.bb.createObjList(this.testarrayofsortedstruct.bind(this), this.testarrayofsortedstructLength()), this.bb.createScalarList(this.flex.bind(this), this.flexLength()), this.bb.createObjList(this.test5.bind(this), this.test5Length()), this.bb.createScalarList(this.vectorOfLongs.bind(this), this.vectorOfLongsLength()), this.bb.createScalarList(this.vectorOfDoubles.bind(this), this.vectorOfDoublesLength()), (this.parentNamespaceTest() !== null ? this.parentNamespaceTest().unpack() : null), this.bb.createObjList(this.vectorOfReferrables.bind(this), this.vectorOfReferrablesLength()), this.singleWeakReference(), this.bb.createScalarList(this.vectorOfWeakReferences.bind(this), this.vectorOfWeakReferencesLength()), this.bb.createObjList(this.vectorOfStrongReferrables.bind(this), this.vectorOfStrongReferrablesLength()), this.coOwningReference(), this.bb.createScalarList(this.vectorOfCoOwningReferences.bind(this), this.vectorOfCoOwningReferencesLength()), this.nonOwningReference(), this.bb.createScalarList(this.vectorOfNonOwningReferences.bind(this), this.vectorOfNonOwningReferencesLength()), this.anyUniqueType(), (() => {
898-
let temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
898+
const temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
899899
if (temp === null) {
900900
return null;
901901
}
902902
return temp.unpack();
903903
})(), this.anyAmbiguousType(), (() => {
904-
let temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
904+
const temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
905905
if (temp === null) {
906906
return null;
907907
}
@@ -917,7 +917,7 @@ export class Monster {
917917
_o.color = this.color();
918918
_o.testType = this.testType();
919919
_o.test = (() => {
920-
let temp = unionToAny(this.testType(), this.test.bind(this));
920+
const temp = unionToAny(this.testType(), this.test.bind(this));
921921
if (temp === null) {
922922
return null;
923923
}
@@ -959,15 +959,15 @@ export class Monster {
959959
_o.vectorOfNonOwningReferences = this.bb.createScalarList(this.vectorOfNonOwningReferences.bind(this), this.vectorOfNonOwningReferencesLength());
960960
_o.anyUniqueType = this.anyUniqueType();
961961
_o.anyUnique = (() => {
962-
let temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
962+
const temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
963963
if (temp === null) {
964964
return null;
965965
}
966966
return temp.unpack();
967967
})();
968968
_o.anyAmbiguousType = this.anyAmbiguousType();
969969
_o.anyAmbiguous = (() => {
970-
let temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
970+
const temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
971971
if (temp === null) {
972972
return null;
973973
}

0 commit comments

Comments
 (0)