Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JS Generator Identifier when name is camel case #9775

Closed
wants to merge 11 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 76 additions & 5 deletions src/google/protobuf/compiler/js/js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,64 @@ std::vector<std::string> ParseUpperCamel(const std::string& input) {
return words;
}

std::vector<std::string> ParseLowerCamel(const std::string& input) {
std::vector<std::string> words;
std::string running = "";
for (int i = 0; i < input.size(); i++) {
if (i != 0 && input[i] >= 'A' && input[i] <= 'Z' && !running.empty()) {
words.push_back(running);
running.clear();
}
running += ToLowerASCII(input[i]);
}
if (!running.empty()) {
words.push_back(running);
}
return words;
}

bool HasUpper(const std::string& input) {
for (int i = 0; i < input.size(); i++) {
if (input[i] >= 'A' && input[i] <= 'Z') {
return true;
}
}

return false;
}

bool IsUpperCamel(const std::string& input) {
if (input.empty()) {
return false;
}

if (input.find('_') != std::string::npos) {
return false;
}

if (input[0] < 'A' || input[0] > 'Z') {
return false;
}

return HasUpper(input);
}

bool IsLowerCamel(const std::string& input) {
if (input.empty()) {
return false;
}

if (input.find('_') != std::string::npos) {
return false;
}

if (input[0] >= 'A' && input[0] <= 'Z') {
return false;
}

return HasUpper(input);
}

std::string ToLowerCamel(const std::vector<std::string>& words) {
std::string result;
for (int i = 0; i < words.size(); i++) {
Expand Down Expand Up @@ -462,14 +520,27 @@ std::string JSIdent(const GeneratorOptions& options,
const FieldDescriptor* field, bool is_upper_camel,
bool is_map, bool drop_list) {
std::string result;

if (field->type() == FieldDescriptor::TYPE_GROUP) {
result = is_upper_camel
? ToUpperCamel(ParseUpperCamel(field->message_type()->name()))
: ToLowerCamel(ParseUpperCamel(field->message_type()->name()));
result = is_upper_camel
? ToUpperCamel(ParseUpperCamel(field->message_type()->name()))
: ToLowerCamel(ParseUpperCamel(field->message_type()->name()));
} else {
result = is_upper_camel ? ToUpperCamel(ParseLowerUnderscore(field->name()))
: ToLowerCamel(ParseLowerUnderscore(field->name()));
if (is_upper_camel && IsUpperCamel(field->name())) {
result = field->name();
} else if (is_upper_camel && IsLowerCamel(field->name())) {
result = ToUpperCamel(ParseLowerCamel(field->name()));
} else if (!is_upper_camel && IsLowerCamel(field->name())) {
result = field->name();
} else if (!is_upper_camel && IsUpperCamel(field->name())) {
result = ToLowerCamel(ParseUpperCamel(field->name()));
} else if (is_upper_camel) {
result = ToUpperCamel(ParseLowerUnderscore(field->name()));
} else {
result = ToLowerCamel(ParseLowerUnderscore(field->name()));
}
}

if (is_map || field->is_map()) {
// JSPB-style or proto3-style map.
result += "Map";
Expand Down