diff --git a/README.md b/README.md index 85411e642..4b0703df9 100644 --- a/README.md +++ b/README.md @@ -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")`). diff --git a/cli/pbjs.js b/cli/pbjs.js index 56b337685..9e1c01ca0 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -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,7 +78,7 @@ 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.", "", @@ -85,6 +86,8 @@ exports.main = function(args, callback) { "", " " + 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 }; diff --git a/cli/targets/json-module.js b/cli/targets/json-module.js index e707b5068..3b1c93779 100644 --- a/cli/targets/json-module.js +++ b/cli/targets/json-module.js @@ -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)); diff --git a/cli/targets/static.js b/cli/targets/static.js index 5230ab397..3488d7d80 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -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 + ";"); diff --git a/tests/data/comments.js b/tests/data/comments.js index 64e9175ed..361e0b86c 100644 --- a/tests/data/comments.js +++ b/tests/data/comments.js @@ -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; diff --git a/tests/data/convert.js b/tests/data/convert.js index e8a777871..1d5340a25 100644 --- a/tests/data/convert.js +++ b/tests/data/convert.js @@ -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; diff --git a/tests/data/mapbox/vector_tile.js b/tests/data/mapbox/vector_tile.js index 3e8634700..cd5191d1d 100644 --- a/tests/data/mapbox/vector_tile.js +++ b/tests/data/mapbox/vector_tile.js @@ -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; diff --git a/tests/data/package.js b/tests/data/package.js index 559e20851..5797fd625 100644 --- a/tests/data/package.js +++ b/tests/data/package.js @@ -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 = []; diff --git a/tests/data/rpc.js b/tests/data/rpc.js index ccb79902f..192fb88c0 100644 --- a/tests/data/rpc.js +++ b/tests/data/rpc.js @@ -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 = []; diff --git a/tests/data/test.js b/tests/data/test.js index 1b3e84f58..5a3376a3e 100644 --- a/tests/data/test.js +++ b/tests/data/test.js @@ -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;