THRIFT-4738: Fix typescript declaration generation
diff --git a/compiler/cpp/src/thrift/generate/t_js_generator.cc b/compiler/cpp/src/thrift/generate/t_js_generator.cc
index fc23e87..f8f1bee 100644
--- a/compiler/cpp/src/thrift/generate/t_js_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_js_generator.cc
@@ -206,6 +206,7 @@
std::string ts_includes();
std::string ts_service_includes();
std::string render_includes();
+ std::string render_ts_includes();
std::string declare_field(t_field* tfield, bool init = false, bool obj = false);
std::string function_signature(t_function* tfunction,
std::string prefix = "",
@@ -287,7 +288,7 @@
* TypeScript Definition File helper functions
*/
- string ts_function_signature(t_function* tfunction, bool include_callback, bool optional_callback);
+ string ts_function_signature(t_function* tfunction, bool include_callback);
string ts_get_type(t_type* type);
/**
@@ -511,6 +512,27 @@
}
/**
+ * Renders all the imports necessary for including another Thrift program
+ */
+string t_js_generator::render_ts_includes() {
+ string result;
+
+ if (!gen_node_) {
+ return result;
+ }
+ const vector<t_program*>& includes = program_->get_includes();
+ for (size_t i = 0; i < includes.size(); ++i) {
+ result += "import " + make_valid_nodeJs_identifier(includes[i]->get_name()) + "_ttypes = require('./" + includes[i]->get_name()
+ + "_types');\n";
+ }
+ if (includes.size() > 0) {
+ result += "\n";
+ }
+
+ return result;
+}
+
+/**
* Close up (or down) some filez.
*/
void t_js_generator::close_generator() {
@@ -1117,7 +1139,7 @@
f_service_ts_ << "/// <reference path=\"" << tservice->get_extends()->get_name()
<< ".d.ts\" />" << endl;
}
- f_service_ts_ << autogen_comment() << endl << ts_includes() << endl << endl;
+ f_service_ts_ << autogen_comment() << endl << ts_includes() << endl << render_ts_includes() << endl;
if (gen_node_) {
f_service_ts_ << "import ttypes = require('./" + program_->get_name() + "_types');" << endl;
// Generate type aliases
@@ -1219,9 +1241,9 @@
}
f_service_ts_ << "{" << endl;
indent_up();
- f_service_ts_ << ts_indent() << "private _handler: Object;" << endl << endl;
- f_service_ts_ << ts_indent() << "constructor(handler: Object);" << endl;
- f_service_ts_ << ts_indent() << "process(input: Thrift.TJSONProtocol, output: Thrift.TJSONProtocol): void;" << endl;
+ f_service_ts_ << ts_indent() << "private _handler: object;" << endl << endl;
+ f_service_ts_ << ts_indent() << "constructor(handler: object);" << endl;
+ f_service_ts_ << ts_indent() << "process(input: thrift.TProtocol, output: thrift.TProtocol): void;" << endl;
indent_down();
}
} else {
@@ -1325,7 +1347,7 @@
}
if (gen_ts_) {
indent_up();
- f_service_ts_ << ts_indent() << "process_" << tfunction->get_name() << "(seqid: number, input: Thrift.TJSONProtocol, output: Thrift.TJSONProtocol): void;" << endl;
+ f_service_ts_ << ts_indent() << "process_" << tfunction->get_name() << "(seqid: number, input: thrift.TProtocol, output: thrift.TProtocol): void;" << endl;
indent_down();
}
@@ -1645,10 +1667,11 @@
indent(f_service_) << "this._seqid = 0;" << endl;
indent(f_service_) << "this._reqs = {};" << endl;
if (gen_ts_) {
- f_service_ts_ << ts_indent() << "private input: Thrift.TJSONProtocol;" << endl << ts_indent()
- << "private output: Thrift.TJSONProtocol;" << endl << ts_indent() << "private seqid: number;"
- << endl << endl << ts_indent()
- << "constructor(input: Thrift.TJSONProtocol, output?: Thrift.TJSONProtocol);"
+ f_service_ts_ << ts_indent() << "private output: thrift.TTransport;" << endl
+ << ts_indent() << "private pClass: thrift.TProtocol;" << endl
+ << ts_indent() << "private _seqid: number;" << endl
+ << endl
+ << ts_indent() << "constructor(output: thrift.TTransport, pClass: { new(trans: thrift.TTransport): thrift.TProtocol });"
<< endl;
}
} else {
@@ -1719,13 +1742,13 @@
if (gen_ts_) {
// function definition without callback
- f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, false, false) << endl;
+ f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, false) << endl;
if (!gen_es6_) {
// overload with callback
- f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, true, false) << endl;
+ f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, true) << endl;
} else {
// overload with callback
- f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, true, true) << endl;
+ f_service_ts_ << ts_print_doc(*f_iter) << ts_indent() << ts_function_signature(*f_iter, true) << endl;
}
}
@@ -2669,7 +2692,7 @@
* @param bool in-/exclude the callback argument
* @return String of rendered function definition
*/
-std::string t_js_generator::ts_function_signature(t_function* tfunction, bool include_callback, bool optional_callback) {
+std::string t_js_generator::ts_function_signature(t_function* tfunction, bool include_callback) {
string str;
const vector<t_field*>& fields = tfunction->get_arglist()->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -2685,7 +2708,6 @@
}
if (include_callback) {
- string callback_optional_string = optional_callback ? "?" : "";
if (gen_node_) {
t_struct* exceptions = tfunction->get_xceptions();
string exception_types;
@@ -2694,19 +2716,19 @@
for (vector<t_field*>::const_iterator it = members.begin(); it != members.end(); ++it) {
t_type* t = get_true_type((*it)->get_type());
if (it == members.begin()) {
- exception_types = t->get_name();
+ exception_types = js_type_namespace(t->get_program()) + t->get_name();
} else {
- exception_types += " | " + t->get_name();
+ exception_types += " | " + js_type_namespace(t->get_program()) + t->get_name();
}
}
}
if (exception_types == "") {
- str += "callback" + callback_optional_string + ": (error: void, response: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
+ str += "callback?: (error: void, response: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
} else {
- str += "callback" + callback_optional_string + ": (error: " + exception_types + ", response: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
+ str += "callback?: (error: " + exception_types + ", response: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
}
} else {
- str += "callback" + callback_optional_string + ": (data: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
+ str += "callback?: (data: " + ts_get_type(tfunction->get_returntype()) + ")=>void): ";
}
if (gen_jquery_) {