You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2016/01/09 00:39:56 UTC

thrift git commit: THRIFT-3314 Dots in file names of includes causes dots in javascript variable names Client: Javascript Patch: Matt Fysh

Repository: thrift
Updated Branches:
  refs/heads/master 4fcc74478 -> 373695a8b


THRIFT-3314 Dots in file names of includes causes dots in javascript variable names
Client: Javascript
Patch: Matt Fysh


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/373695a8
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/373695a8
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/373695a8

Branch: refs/heads/master
Commit: 373695a8b680446a74bd0a0085fcbbbf04f8c8d8
Parents: 4fcc744
Author: Jens Geyer <je...@apache.org>
Authored: Sat Jan 9 00:31:04 2016 +0100
Committer: Jens Geyer <je...@apache.org>
Committed: Sat Jan 9 00:39:17 2016 +0100

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_js_generator.cc | 39 ++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/373695a8/compiler/cpp/src/generate/t_js_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_js_generator.cc b/compiler/cpp/src/generate/t_js_generator.cc
index 7c73add..94d5259 100644
--- a/compiler/cpp/src/generate/t_js_generator.cc
+++ b/compiler/cpp/src/generate/t_js_generator.cc
@@ -23,6 +23,7 @@
 #include <iostream>
 #include <vector>
 #include <list>
+#include <cassert>
 
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -175,6 +176,7 @@ public:
                                  bool include_callback = false);
   std::string argument_list(t_struct* tstruct, bool include_callback = false);
   std::string type_to_enum(t_type* ttype);
+  std::string make_valid_nodeJs_identifier(std::string const& name);
 
   std::string autogen_comment() {
     return std::string("//\n") + "// Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n"
@@ -207,7 +209,7 @@ public:
   std::string js_type_namespace(t_program* p) {
     if (gen_node_) {
       if (p != NULL && p != program_) {
-        return p->get_name() + "_ttypes.";
+        return make_valid_nodeJs_identifier(p->get_name()) + "_ttypes.";
       }
       return "ttypes.";
     }
@@ -387,7 +389,7 @@ string t_js_generator::render_includes() {
   if (gen_node_) {
     const vector<t_program*>& includes = program_->get_includes();
     for (size_t i = 0; i < includes.size(); ++i) {
-      result += "var " + includes[i]->get_name() + "_ttypes = require('./" + includes[i]->get_name()
+      result += "var " + make_valid_nodeJs_identifier(includes[i]->get_name()) + "_ttypes = require('./" + includes[i]->get_name()
                 + "_types');\n";
     }
     if (includes.size() > 0) {
@@ -2184,6 +2186,39 @@ std::string t_js_generator::ts_function_signature(t_function* tfunction, bool in
   return str;
 }
 
+/**
+ * Takes a name and produes a valid NodeJS identifier from it
+ *
+ * @param name The name which shall become a valid NodeJS identifier
+ * @return The modified name with update identifier
+ */
+std::string t_js_generator::make_valid_nodeJs_identifier(std::string const& name) {
+  std::string str = name;
+  if (str.empty()) {
+    return str;
+  }
+
+  // tests rely on this
+  assert(('A' < 'Z') && ('a' < 'z') && ('0' < '9'));
+
+  // if the first letter is a number, we add an additional underscore in front of it
+  char c = str.at(0);
+  if (('0' <= c) && (c <= '9')) {
+    str = "_" + str;
+  }
+
+  // following chars: letter, number or underscore
+  for (size_t i = 0; i < str.size(); ++i) {
+    c = str.at(i);
+    if ((('A' > c) || (c > 'Z')) && (('a' > c) || (c > 'z')) && (('0' > c) || (c > '9'))
+        && ('_' != c) && ('$' != c)) {
+      str.replace(i, 1, "_");
+    }
+  }
+
+  return str;
+}
+
 THRIFT_REGISTER_GENERATOR(js,
                           "Javascript",
                           "    jquery:          Generate jQuery compatible code.\n"