Permalink
Browse files

Other: Added ES6 syntax flag to pbjs, see #667

  • Loading branch information...
dcodeIO committed Jan 30, 2017
1 parent 4397607 commit 6a0920b2c32e7963741693f5a773b89f4b262688
@@ -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
@@ -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")`).
@@ -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,
@@ -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) {
@@ -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.",
@@ -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
};
@@ -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));
@@ -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)
@@ -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)
@@ -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) {
@@ -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];
@@ -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;
@@ -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("");
@@ -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 ? "" : ","));
@@ -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 + ";");
@@ -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 = [];
@@ -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;
@@ -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 = [];
@@ -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;
@@ -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 = [];
@@ -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;
@@ -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 = [];
@@ -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 = [];
@@ -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 = [];
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;

0 comments on commit 6a0920b

Please sign in to comment.