Skip to content

Commit

Permalink
Other: Added ES6 syntax flag to pbjs, see #667
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 30, 2017
1 parent 4397607 commit 6a0920b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 60 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Examples

### Using .proto files

It's possible to load existing .proto files using the full library, which parses and compiles the definitions to ready to use reflection-based runtime message classes:
It's possible to load existing .proto files using the full library, which parses and compiles the definitions to ready to use (reflection-based) message classes:

```protobuf
// awesome.proto
Expand Down Expand Up @@ -441,7 +441,7 @@ $> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
$> pbts -o compiled.d.ts compiled.js
```

Additionally, TypeScript definitions of static modules are compatible with reflection, as long as the following conditions are met:
Additionally, TypeScript definitions of static modules are compatible with their reflection-based counterparts (i.e. as exported by JSON modules), as long as the following conditions are met:

1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
2. Types, services and enums must start with an uppercase letter to become available as properties of the reflected types as well (i.e. to be able to use `MyMessage.MyEnum` instead of `root.lookup("MyMessage.MyEnum")`).
Expand Down
13 changes: 10 additions & 3 deletions cli/pbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.main = function(args, callback) {
lint : "l"
},
string: [ "target", "out", "path", "wrap", "root", "lint" ],
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments" ],
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6" ],
default: {
target : "json",
create : true,
Expand All @@ -42,13 +42,14 @@ exports.main = function(args, callback) {
delimited : true,
beautify : true,
comments : true,
es6 : null,
lint : lintDefault
}
});

var target = targets[argv.target],
files = argv._,
paths = typeof argv.path === 'string' ? [ argv.path ] : argv.path || [];
paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];

if (!files.length) {
var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
Expand Down Expand Up @@ -77,14 +78,16 @@ exports.main = function(args, callback) {
" default Default wrapper supporting both CommonJS and AMD",
" commonjs CommonJS wrapper",
" amd AMD wrapper",
" es6 ES6 wrapper",
" es6 ES6 wrapper (implies --es6)",
"",
" -r, --root Specifies an alternative protobuf.roots name.",
"",
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
"",
" " + lintDefault,
"",
" --es6 Enables ES6 syntax (const/let instead of var)",
"",
chalk.bold.gray(" Proto sources only:"),
"",
" --keep-case Keeps field casing instead of converting to camel case.",
Expand Down Expand Up @@ -134,6 +137,10 @@ exports.main = function(args, callback) {
return filepath;
};

// Use es6 syntax if not explicitly specified on the command line and the es6 wrapper is used
if (argv.wrap === "es6" && argv.es6 === null)
argv.es6 = true;

var parseOptions = {
"keepCase": argv["keep-case"] || false
};
Expand Down
2 changes: 1 addition & 1 deletion cli/targets/json-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function json_module(root, options, callback) {
try {
var rootProp = util.safeProp(options.root || "default");
var output = [
"var $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
(options.es6 ? "const" : "var") + " $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
];
if (root.options) {
var optionsJson = util.jsonSafeProp(JSON.stringify(root.options, null, 2));
Expand Down
26 changes: 14 additions & 12 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ function static_target(root, options, callback) {
try {
if (config.comments)
push("// Common aliases");
push("var $Reader = $protobuf.Reader,");
push(" $Writer = $protobuf.Writer,");
push(" $util = $protobuf.util;");
push((config.es6 ? "const" : "var") + " $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;");
push("");
if (config.comments)
push("// Lazily resolved type references");
push("var $lazyTypes = [];");
push((config.es6 ? "const" : "var") + " $lazyTypes = [];");
push("");
if (config.comments) {
if (root.comment)
Expand All @@ -40,7 +38,7 @@ function static_target(root, options, callback) {
push("// Exported root namespace");
}
var rootProp = cliUtil.safeProp(config.root || "default");
push("var $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
push((config.es6 ? "const" : "var") + " $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
buildNamespace(null, root);
push("");
if (config.comments)
Expand Down Expand Up @@ -113,7 +111,7 @@ function buildNamespace(ref, ns) {
"@exports " + ns.fullName.substring(1),
"@namespace"
]);
push("var " + name(ns.name) + " = {};");
push((config.es6 ? "const" : "var") + " " + name(ns.name) + " = {};");
}

ns.nestedArray.forEach(function(nested) {
Expand Down Expand Up @@ -172,6 +170,11 @@ function beautifyCode(code) {
"type": "Identifier",
"name": shortVars[node.name]
};
// replace var with let if es6
if (config.es6 && node.type === "VariableDeclaration" && node.kind === "var") {
node.kind = "let";
return undefined;
}
// remove braces around block statements with a single child
if (node.type === "BlockStatement" && reduceableBlockStatements[parent.type] && node.body.length === 1)
return node.body[0];
Expand Down Expand Up @@ -281,7 +284,7 @@ function buildType(ref, type) {
++indent;
push("if (properties)");
++indent;
push("for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)");
push("for (" + (config.es6 ? "let" : "var") + " keys = Object.keys(properties), i = 0; i < keys.length; ++i)");
++indent;
push("this[keys[i]] = properties[keys[i]];");
--indent;
Expand Down Expand Up @@ -327,14 +330,14 @@ function buildType(ref, type) {
});

// virtual oneof fields
var firstOneOf = true;;
var firstOneOf = true;
type.oneofsArray.forEach(function(oneof) {
if (firstOneOf) {
firstOneOf = false;
push("");
if (config.comments)
push("// OneOf field names bound to virtual getters and setters");
push("var $oneOfFields;");
push((config.es6 ? "let" : "var") + " $oneOfFields;");
}
oneof.resolve();
push("");
Expand Down Expand Up @@ -364,7 +367,7 @@ function buildType(ref, type) {
push("");
if (config.comments)
push("// Lazily resolved type references");
push("var $types = {");
push((config.es6 ? "const" : "var") + " $types = {");
++indent;
types.forEach(function(line, i) {
push(line + (i === types.length - 1 ? "" : ","));
Expand Down Expand Up @@ -609,8 +612,7 @@ function buildEnum(ref, enm) {
pushComment(comment);
push(name(ref) + "." + name(enm.name) + " = (function() {");
++indent;
push("var valuesById = {},");
push(" values = Object.create(valuesById);");
push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
Object.keys(enm.values).forEach(function(key) {
var val = enm.values[key];
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");
Expand Down
7 changes: 2 additions & 5 deletions tests/data/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
var $protobuf = require("../../minimal");

// Common aliases
var $Reader = $protobuf.Reader,
$Writer = $protobuf.Writer,
$util = $protobuf.util;
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];
Expand Down Expand Up @@ -362,8 +360,7 @@ $root.Test2 = (function() {
* @property {number} THREE=3 Value with a comment.
*/
$root.Test3 = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "ONE"] = 1;
values[valuesById[2] = "TWO"] = 2;
values[valuesById[3] = "THREE"] = 3;
Expand Down
7 changes: 2 additions & 5 deletions tests/data/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
var $protobuf = require("../../minimal");

// Common aliases
var $Reader = $protobuf.Reader,
$Writer = $protobuf.Writer,
$util = $protobuf.util;
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];
Expand Down Expand Up @@ -505,8 +503,7 @@ $root.Message = (function() {
* @property {number} TWO=2 TWO value
*/
Message.SomeEnum = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "ONE"] = 1;
values[valuesById[2] = "TWO"] = 2;
return values;
Expand Down
7 changes: 2 additions & 5 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
var $protobuf = require("../../../minimal");

// Common aliases
var $Reader = $protobuf.Reader,
$Writer = $protobuf.Writer,
$util = $protobuf.util;
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];
Expand Down Expand Up @@ -216,8 +214,7 @@ $root.vector_tile = (function() {
* @property {number} POLYGON=3 POLYGON value
*/
Tile.GeomType = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "UNKNOWN"] = 0;
values[valuesById[1] = "POINT"] = 1;
values[valuesById[2] = "LINESTRING"] = 2;
Expand Down
4 changes: 1 addition & 3 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
var $protobuf = require("../../minimal");

// Common aliases
var $Reader = $protobuf.Reader,
$Writer = $protobuf.Writer,
$util = $protobuf.util;
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];
Expand Down
4 changes: 1 addition & 3 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
var $protobuf = require("../../minimal");

// Common aliases
var $Reader = $protobuf.Reader,
$Writer = $protobuf.Writer,
$util = $protobuf.util;
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];
Expand Down
31 changes: 10 additions & 21 deletions tests/data/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
var $protobuf = require("../../minimal");

// Common aliases
var $Reader = $protobuf.Reader,
$Writer = $protobuf.Writer,
$util = $protobuf.util;
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;

// Lazily resolved type references
var $lazyTypes = [];
Expand Down Expand Up @@ -177,8 +175,7 @@ $root.jspb = (function() {
* @property {number} BAR=2 BAR value
*/
test.OuterEnum = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "FOO"] = 1;
values[valuesById[2] = "BAR"] = 2;
return values;
Expand Down Expand Up @@ -3135,8 +3132,7 @@ $root.jspb = (function() {
* @property {number} E2=77 E2 value
*/
DefaultValues.Enum = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[13] = "E1"] = 13;
values[valuesById[77] = "E2"] = 77;
return values;
Expand Down Expand Up @@ -6497,8 +6493,7 @@ $root.jspb = (function() {
* @property {number} MAP_VALUE_BAZ_NOBINARY=2 MAP_VALUE_BAZ_NOBINARY value
*/
test.MapValueEnumNoBinary = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "MAP_VALUE_FOO_NOBINARY"] = 0;
values[valuesById[1] = "MAP_VALUE_BAR_NOBINARY"] = 1;
values[valuesById[2] = "MAP_VALUE_BAZ_NOBINARY"] = 2;
Expand Down Expand Up @@ -9177,8 +9172,7 @@ $root.google = (function() {
* @property {number} TYPE_SINT64=18 TYPE_SINT64 value
*/
FieldDescriptorProto.Type = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "TYPE_DOUBLE"] = 1;
values[valuesById[2] = "TYPE_FLOAT"] = 2;
values[valuesById[3] = "TYPE_INT64"] = 3;
Expand Down Expand Up @@ -9210,8 +9204,7 @@ $root.google = (function() {
* @property {number} LABEL_REPEATED=3 LABEL_REPEATED value
*/
FieldDescriptorProto.Label = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "LABEL_OPTIONAL"] = 1;
values[valuesById[2] = "LABEL_REQUIRED"] = 2;
values[valuesById[3] = "LABEL_REPEATED"] = 3;
Expand Down Expand Up @@ -10827,8 +10820,7 @@ $root.google = (function() {
* @property {number} LITE_RUNTIME=3 LITE_RUNTIME value
*/
FileOptions.OptimizeMode = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[1] = "SPEED"] = 1;
values[valuesById[2] = "CODE_SIZE"] = 2;
values[valuesById[3] = "LITE_RUNTIME"] = 3;
Expand Down Expand Up @@ -11447,8 +11439,7 @@ $root.google = (function() {
* @property {number} STRING_PIECE=2 STRING_PIECE value
*/
FieldOptions.CType = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "STRING"] = 0;
values[valuesById[1] = "CORD"] = 1;
values[valuesById[2] = "STRING_PIECE"] = 2;
Expand All @@ -11465,8 +11456,7 @@ $root.google = (function() {
* @property {number} JS_NUMBER=2 JS_NUMBER value
*/
FieldOptions.JSType = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "JS_NORMAL"] = 0;
values[valuesById[1] = "JS_STRING"] = 1;
values[valuesById[2] = "JS_NUMBER"] = 2;
Expand Down Expand Up @@ -12567,8 +12557,7 @@ $root.google = (function() {
* @property {number} IDEMPOTENT=2 IDEMPOTENT value
*/
MethodOptions.IdempotencyLevel = (function() {
var valuesById = {},
values = Object.create(valuesById);
var valuesById = {}, values = Object.create(valuesById);
values[valuesById[0] = "IDEMPOTENCY_UNKNOWN"] = 0;
values[valuesById[1] = "NO_SIDE_EFFECTS"] = 1;
values[valuesById[2] = "IDEMPOTENT"] = 2;
Expand Down

0 comments on commit 6a0920b

Please sign in to comment.