Skip to content

Commit

Permalink
[TS] Make strict compliant and improve typings (#7549)
Browse files Browse the repository at this point in the history
* [TS] Make strict compliant and improve typings

* clang-format

* Code gen harmonize

Co-authored-by: Derek Bailey <derekbailey@google.com>
  • Loading branch information
bjornharrtell and dbaileychess committed Sep 29, 2022
1 parent 374f8fb commit d243b90
Show file tree
Hide file tree
Showing 41 changed files with 279 additions and 268 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
},
"scripts": {
"test": "npm run compile && cd tests/ts && python3 ./TypeScriptTest.py",
"lint": "eslint ts",
"compile": "tsc && tsc -p tsconfig.mjs.json && rollup -c",
"prepublishOnly": "npm install --only=dev && npm run compile"
},
Expand Down
41 changes: 24 additions & 17 deletions src/idl_gen_ts.cpp
Expand Up @@ -759,10 +759,10 @@ class TsGenerator : public BaseGenerator {
import.object_name = object_name;
import.bare_file_path = bare_file_path;
import.rel_file_path = rel_file_path;
import.import_statement =
"import { " + symbols_expression + " } from '" + rel_file_path + ".js';";
import.export_statement =
"export { " + symbols_expression + " } from '." + bare_file_path + ".js';";
import.import_statement = "import { " + symbols_expression + " } from '" +
rel_file_path + ".js';";
import.export_statement = "export { " + symbols_expression + " } from '." +
bare_file_path + ".js';";
import.dependency = &dependency;
import.dependent = &dependent;

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

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

ret = "(() => {\n";
ret += " let ret = [];\n";
ret += " const ret = [];\n";
ret += " for(let targetEnumIndex = 0; targetEnumIndex < this." +
namer_.Method(field_name, "TypeLength") + "()" +
"; "
"++targetEnumIndex) {\n";
ret += " let targetEnum = this." +
ret += " const targetEnum = this." +
namer_.Method(field_name, "Type") + "(targetEnumIndex);\n";
ret += " if(targetEnum === null || " + enum_type +
"[targetEnum!] === 'NONE') { "
"continue; }\n\n";
ret += " let temp = " + conversion_function + "(targetEnum, " +
ret += " const temp = " + conversion_function + "(targetEnum, " +
field_binded_method + ", targetEnumIndex);\n";
ret += " if(temp === null) { continue; }\n";
ret += union_has_string ? " if(typeof temp === 'string') { "
Expand Down Expand Up @@ -1102,11 +1102,13 @@ class TsGenerator : public BaseGenerator {
switch (vectortype.base_type) {
case BASE_TYPE_STRUCT: {
const auto &sd = *field.value.type.struct_def;
field_type += GetTypeName(sd, /*object_api=*/true);
;
const auto field_type_name =
GetTypeName(sd, /*object_api=*/true);
field_type += field_type_name;
field_type += ")[]";

field_val = GenBBAccess() + ".createObjList(" +
field_val = GenBBAccess() + ".createObjList<" + vectortypename +
", " + field_type_name + ">(" +
field_binded_method + ", this." +
namer_.Method(field, "Length") + "())";

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

case BASE_TYPE_STRING: {
field_type += "string)[]";
field_val = GenBBAccess() + ".createScalarList(" +
field_val = GenBBAccess() + ".createScalarList<string>(" +
field_binded_method + ", this." +
namer_.Field(field, "Length") + "())";
field_offset_decl =
Expand Down Expand Up @@ -1162,9 +1164,9 @@ class TsGenerator : public BaseGenerator {
field_type += vectortypename;
}
field_type += ")[]";
field_val = GenBBAccess() + ".createScalarList(" +
field_binded_method + ", this." +
namer_.Method(field, "Length") + "())";
field_val = GenBBAccess() + ".createScalarList<" +
vectortypename + ">(" + field_binded_method +
", this." + namer_.Method(field, "Length") + "())";

field_offset_decl =
AddImport(imports, struct_def, struct_def).name + "." +
Expand Down Expand Up @@ -1260,7 +1262,7 @@ class TsGenerator : public BaseGenerator {
obj_api_class = "\n";
obj_api_class += "export class ";
obj_api_class += GetTypeName(struct_def, /*object_api=*/true);
obj_api_class += " {\n";
obj_api_class += " implements flatbuffers.IGeneratedObject {\n";
obj_api_class += constructor_func;
obj_api_class += pack_func_prototype + pack_func_offset_decl +
pack_func_create_call + "\n}";
Expand Down Expand Up @@ -1298,12 +1300,17 @@ class TsGenerator : public BaseGenerator {
}

const std::string object_name = GetTypeName(struct_def);
const std::string object_api_name = GetTypeName(struct_def, true);

// Emit constructor
GenDocComment(struct_def.doc_comment, code_ptr);
code += "export class ";
code += object_name;
code += " {\n";
if (parser.opts.generate_object_based_api)
code += " implements flatbuffers.IUnpackableObject<" + object_api_name +
"> {\n";
else
code += " {\n";
code += " bb: flatbuffers.ByteBuffer|null = null;\n";
code += " bb_pos = 0;\n";

Expand Down
9 changes: 5 additions & 4 deletions tests/ts/TypeScriptTest.py
Expand Up @@ -61,7 +61,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)

print("Invoking flatc...")
flatc(
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema="../monster_test.fbs",
include="../include_test",
)
Expand All @@ -74,18 +74,18 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
)

flatc(
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema="../union_vector/union_vector.fbs",
prefix="union_vector",
)

flatc(
options=["--ts", "--gen-name-strings"],
options=["--ts", "--reflect-names", "--gen-name-strings"],
schema="../optional_scalars.fbs",
)

flatc(
options=["--ts", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
options=["--ts", "--reflect-names", "--gen-name-strings", "--gen-mutable", "--gen-object-api"],
schema=[
"typescript_keywords.fbs",
"test_dir/typescript_include.fbs",
Expand All @@ -98,6 +98,7 @@ def flatc(options, schema, prefix=None, include=None, data=None, cwd=tests_path)
flatc(
options=[
"--ts",
"--reflect-names",
"--gen-name-strings",
"--gen-mutable",
"--gen-object-api",
Expand Down
4 changes: 2 additions & 2 deletions tests/ts/my-game/example/ability.ts
Expand Up @@ -4,7 +4,7 @@ import * as flatbuffers from 'flatbuffers';



export class Ability {
export class Ability implements flatbuffers.IUnpackableObject<AbilityT> {
bb: flatbuffers.ByteBuffer|null = null;
bb_pos = 0;
__init(i:number, bb:flatbuffers.ByteBuffer):Ability {
Expand Down Expand Up @@ -61,7 +61,7 @@ unpackTo(_o: AbilityT): void {
}
}

export class AbilityT {
export class AbilityT implements flatbuffers.IGeneratedObject {
constructor(
public id: number = 0,
public distance: number = 0
Expand Down
12 changes: 6 additions & 6 deletions tests/ts/my-game/example/monster.js
Expand Up @@ -889,19 +889,19 @@ export class Monster {
}
unpack() {
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(), (() => {
let temp = unionToAny(this.testType(), this.test.bind(this));
const temp = unionToAny(this.testType(), this.test.bind(this));
if (temp === null) {
return null;
}
return temp.unpack();
})(), 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(), (() => {
let temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
const temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
if (temp === null) {
return null;
}
return temp.unpack();
})(), this.anyAmbiguousType(), (() => {
let temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
const temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
if (temp === null) {
return null;
}
Expand All @@ -917,7 +917,7 @@ export class Monster {
_o.color = this.color();
_o.testType = this.testType();
_o.test = (() => {
let temp = unionToAny(this.testType(), this.test.bind(this));
const temp = unionToAny(this.testType(), this.test.bind(this));
if (temp === null) {
return null;
}
Expand Down Expand Up @@ -959,15 +959,15 @@ export class Monster {
_o.vectorOfNonOwningReferences = this.bb.createScalarList(this.vectorOfNonOwningReferences.bind(this), this.vectorOfNonOwningReferencesLength());
_o.anyUniqueType = this.anyUniqueType();
_o.anyUnique = (() => {
let temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
const temp = unionToAnyUniqueAliases(this.anyUniqueType(), this.anyUnique.bind(this));
if (temp === null) {
return null;
}
return temp.unpack();
})();
_o.anyAmbiguousType = this.anyAmbiguousType();
_o.anyAmbiguous = (() => {
let temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
const temp = unionToAnyAmbiguousAliases(this.anyAmbiguousType(), this.anyAmbiguous.bind(this));
if (temp === null) {
return null;
}
Expand Down

0 comments on commit d243b90

Please sign in to comment.