Skip to content

Commit

Permalink
added minimal support for es6 import_style
Browse files Browse the repository at this point in the history
  • Loading branch information
safanaj committed Nov 4, 2022
1 parent 3ff6090 commit e07a01b
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions generator/js_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ std::string MaybeCrossFileRef(const GeneratorOptions& options,
const FileDescriptor* from_file,
const Descriptor* to_message) {
if ((options.import_style == GeneratorOptions::kImportCommonJs ||
options.import_style == GeneratorOptions::kImportCommonJsStrict) &&
options.import_style == GeneratorOptions::kImportCommonJsStrict ||
options.import_style == GeneratorOptions::kImportEs6) &&
from_file != to_message->file()) {
// Cross-file ref in CommonJS needs to use the module alias instead of
// the global name.
Expand Down Expand Up @@ -3618,8 +3619,14 @@ void Generator::GenerateFile(const GeneratorOptions& options,

// Generate "require" statements.
if ((options.import_style == GeneratorOptions::kImportCommonJs ||
options.import_style == GeneratorOptions::kImportCommonJsStrict)) {
printer->Print("var jspb = require('google-protobuf');\n");
options.import_style == GeneratorOptions::kImportCommonJsStrict ||
options.import_style == GeneratorOptions::kImportEs6)) {

if (options.import_style == GeneratorOptions::kImportEs6) {
printer->Print("import * as jspb from 'google-protobuf';\n");
} else {
printer->Print("var jspb = require('google-protobuf');\n");
}
printer->Print("var goog = jspb;\n");

// Do not use global scope in strict mode
Expand Down Expand Up @@ -3648,13 +3655,22 @@ void Generator::GenerateFile(const GeneratorOptions& options,
" Function('return this')();\n\n");
}

for (int i = 0; i < file->dependency_count(); i++) {
const std::string& name = file->dependency(i)->name();
printer->Print(
"var $alias$ = require('$file$');\n"
"goog.object.extend(proto, $alias$);\n",
"alias", ModuleAlias(name), "file",
GetRootPath(file->name(), name) + GetJSFilename(options, name));
if (options.import_style == GeneratorOptions::kImportEs6) {
for (int i = 0; i < file->dependency_count(); i++) {
const std::string& name = file->dependency(i)->name();
printer->Print("import * as $alias$ from '$file$';\n"
"goog.object.extend(proto, $alias$);\n",
"alias", ModuleAlias(name), "file",
GetRootPath(file->name(), name) + GetJSFilename(options, name));
}
} else {
for (int i = 0; i < file->dependency_count(); i++) {
const std::string& name = file->dependency(i)->name();
printer->Print("var $alias$ = require('$file$');\n"
"goog.object.extend(proto, $alias$);\n",
"alias", ModuleAlias(name), "file",
GetRootPath(file->name(), name) + GetJSFilename(options, name));
}
}
}

Expand Down Expand Up @@ -3698,6 +3714,20 @@ void Generator::GenerateFile(const GeneratorOptions& options,
} else if (options.import_style == GeneratorOptions::kImportCommonJsStrict) {
printer->Print("goog.object.extend(exports, proto);\n", "package",
GetNamespace(options, file));
} else if (options.import_style == GeneratorOptions::kImportEs6) {
std::string package = GetNamespace(options, file);
for (std::set<std::string>::iterator it = provided.begin();
it != provided.end(); ++it) {
std::string fullname = *it;
std::string name = fullname.substr(package.length());

std::string::iterator iend = std::remove(name.begin(), name.end(), '.');
name.resize(name.length()-(name.end()-iend));
name.shrink_to_fit();

printer->Print("export const $name$ = $fullname$;\n",
"name", name, "fullname", fullname);
}
}

// Emit well-known type methods.
Expand Down

0 comments on commit e07a01b

Please sign in to comment.