Skip to content

Commit 6a0920b

Browse files
committed
Other: Added ES6 syntax flag to pbjs, see #667
1 parent 4397607 commit 6a0920b

File tree

10 files changed

+45
-60
lines changed

10 files changed

+45
-60
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Examples
8585

8686
### Using .proto files
8787

88-
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:
88+
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:
8989

9090
```protobuf
9191
// awesome.proto
@@ -441,7 +441,7 @@ $> pbjs -t static-module -w commonjs -o compiled.js file1.proto file2.proto
441441
$> pbts -o compiled.d.ts compiled.js
442442
```
443443

444-
Additionally, TypeScript definitions of static modules are compatible with reflection, as long as the following conditions are met:
444+
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:
445445

446446
1. Instead of using `new SomeMessage(...)`, always use `SomeMessage.create(...)` because reflection objects do not provide a constructor.
447447
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")`).

cli/pbjs.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exports.main = function(args, callback) {
3131
lint : "l"
3232
},
3333
string: [ "target", "out", "path", "wrap", "root", "lint" ],
34-
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments" ],
34+
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6" ],
3535
default: {
3636
target : "json",
3737
create : true,
@@ -42,13 +42,14 @@ exports.main = function(args, callback) {
4242
delimited : true,
4343
beautify : true,
4444
comments : true,
45+
es6 : null,
4546
lint : lintDefault
4647
}
4748
});
4849

4950
var target = targets[argv.target],
5051
files = argv._,
51-
paths = typeof argv.path === 'string' ? [ argv.path ] : argv.path || [];
52+
paths = typeof argv.path === "string" ? [ argv.path ] : argv.path || [];
5253

5354
if (!files.length) {
5455
var descs = Object.keys(targets).filter(function(key) { return !targets[key].private; }).map(function(key) {
@@ -77,14 +78,16 @@ exports.main = function(args, callback) {
7778
" default Default wrapper supporting both CommonJS and AMD",
7879
" commonjs CommonJS wrapper",
7980
" amd AMD wrapper",
80-
" es6 ES6 wrapper",
81+
" es6 ES6 wrapper (implies --es6)",
8182
"",
8283
" -r, --root Specifies an alternative protobuf.roots name.",
8384
"",
8485
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
8586
"",
8687
" " + lintDefault,
8788
"",
89+
" --es6 Enables ES6 syntax (const/let instead of var)",
90+
"",
8891
chalk.bold.gray(" Proto sources only:"),
8992
"",
9093
" --keep-case Keeps field casing instead of converting to camel case.",
@@ -134,6 +137,10 @@ exports.main = function(args, callback) {
134137
return filepath;
135138
};
136139

140+
// Use es6 syntax if not explicitly specified on the command line and the es6 wrapper is used
141+
if (argv.wrap === "es6" && argv.es6 === null)
142+
argv.es6 = true;
143+
137144
var parseOptions = {
138145
"keepCase": argv["keep-case"] || false
139146
};

cli/targets/json-module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function json_module(root, options, callback) {
99
try {
1010
var rootProp = util.safeProp(options.root || "default");
1111
var output = [
12-
"var $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
12+
(options.es6 ? "const" : "var") + " $root = ($protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = new $protobuf.Root()))\n"
1313
];
1414
if (root.options) {
1515
var optionsJson = util.jsonSafeProp(JSON.stringify(root.options, null, 2));

cli/targets/static.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ function static_target(root, options, callback) {
2525
try {
2626
if (config.comments)
2727
push("// Common aliases");
28-
push("var $Reader = $protobuf.Reader,");
29-
push(" $Writer = $protobuf.Writer,");
30-
push(" $util = $protobuf.util;");
28+
push((config.es6 ? "const" : "var") + " $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;");
3129
push("");
3230
if (config.comments)
3331
push("// Lazily resolved type references");
34-
push("var $lazyTypes = [];");
32+
push((config.es6 ? "const" : "var") + " $lazyTypes = [];");
3533
push("");
3634
if (config.comments) {
3735
if (root.comment)
@@ -40,7 +38,7 @@ function static_target(root, options, callback) {
4038
push("// Exported root namespace");
4139
}
4240
var rootProp = cliUtil.safeProp(config.root || "default");
43-
push("var $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
41+
push((config.es6 ? "const" : "var") + " $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
4442
buildNamespace(null, root);
4543
push("");
4644
if (config.comments)
@@ -113,7 +111,7 @@ function buildNamespace(ref, ns) {
113111
"@exports " + ns.fullName.substring(1),
114112
"@namespace"
115113
]);
116-
push("var " + name(ns.name) + " = {};");
114+
push((config.es6 ? "const" : "var") + " " + name(ns.name) + " = {};");
117115
}
118116

119117
ns.nestedArray.forEach(function(nested) {
@@ -172,6 +170,11 @@ function beautifyCode(code) {
172170
"type": "Identifier",
173171
"name": shortVars[node.name]
174172
};
173+
// replace var with let if es6
174+
if (config.es6 && node.type === "VariableDeclaration" && node.kind === "var") {
175+
node.kind = "let";
176+
return undefined;
177+
}
175178
// remove braces around block statements with a single child
176179
if (node.type === "BlockStatement" && reduceableBlockStatements[parent.type] && node.body.length === 1)
177180
return node.body[0];
@@ -281,7 +284,7 @@ function buildType(ref, type) {
281284
++indent;
282285
push("if (properties)");
283286
++indent;
284-
push("for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)");
287+
push("for (" + (config.es6 ? "let" : "var") + " keys = Object.keys(properties), i = 0; i < keys.length; ++i)");
285288
++indent;
286289
push("this[keys[i]] = properties[keys[i]];");
287290
--indent;
@@ -327,14 +330,14 @@ function buildType(ref, type) {
327330
});
328331

329332
// virtual oneof fields
330-
var firstOneOf = true;;
333+
var firstOneOf = true;
331334
type.oneofsArray.forEach(function(oneof) {
332335
if (firstOneOf) {
333336
firstOneOf = false;
334337
push("");
335338
if (config.comments)
336339
push("// OneOf field names bound to virtual getters and setters");
337-
push("var $oneOfFields;");
340+
push((config.es6 ? "let" : "var") + " $oneOfFields;");
338341
}
339342
oneof.resolve();
340343
push("");
@@ -364,7 +367,7 @@ function buildType(ref, type) {
364367
push("");
365368
if (config.comments)
366369
push("// Lazily resolved type references");
367-
push("var $types = {");
370+
push((config.es6 ? "const" : "var") + " $types = {");
368371
++indent;
369372
types.forEach(function(line, i) {
370373
push(line + (i === types.length - 1 ? "" : ","));
@@ -609,8 +612,7 @@ function buildEnum(ref, enm) {
609612
pushComment(comment);
610613
push(name(ref) + "." + name(enm.name) + " = (function() {");
611614
++indent;
612-
push("var valuesById = {},");
613-
push(" values = Object.create(valuesById);");
615+
push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
614616
Object.keys(enm.values).forEach(function(key) {
615617
var val = enm.values[key];
616618
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");

tests/data/comments.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
var $protobuf = require("../../minimal");
55

66
// Common aliases
7-
var $Reader = $protobuf.Reader,
8-
$Writer = $protobuf.Writer,
9-
$util = $protobuf.util;
7+
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
108

119
// Lazily resolved type references
1210
var $lazyTypes = [];
@@ -362,8 +360,7 @@ $root.Test2 = (function() {
362360
* @property {number} THREE=3 Value with a comment.
363361
*/
364362
$root.Test3 = (function() {
365-
var valuesById = {},
366-
values = Object.create(valuesById);
363+
var valuesById = {}, values = Object.create(valuesById);
367364
values[valuesById[1] = "ONE"] = 1;
368365
values[valuesById[2] = "TWO"] = 2;
369366
values[valuesById[3] = "THREE"] = 3;

tests/data/convert.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
var $protobuf = require("../../minimal");
55

66
// Common aliases
7-
var $Reader = $protobuf.Reader,
8-
$Writer = $protobuf.Writer,
9-
$util = $protobuf.util;
7+
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
108

119
// Lazily resolved type references
1210
var $lazyTypes = [];
@@ -505,8 +503,7 @@ $root.Message = (function() {
505503
* @property {number} TWO=2 TWO value
506504
*/
507505
Message.SomeEnum = (function() {
508-
var valuesById = {},
509-
values = Object.create(valuesById);
506+
var valuesById = {}, values = Object.create(valuesById);
510507
values[valuesById[1] = "ONE"] = 1;
511508
values[valuesById[2] = "TWO"] = 2;
512509
return values;

tests/data/mapbox/vector_tile.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
var $protobuf = require("../../../minimal");
55

66
// Common aliases
7-
var $Reader = $protobuf.Reader,
8-
$Writer = $protobuf.Writer,
9-
$util = $protobuf.util;
7+
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
108

119
// Lazily resolved type references
1210
var $lazyTypes = [];
@@ -216,8 +214,7 @@ $root.vector_tile = (function() {
216214
* @property {number} POLYGON=3 POLYGON value
217215
*/
218216
Tile.GeomType = (function() {
219-
var valuesById = {},
220-
values = Object.create(valuesById);
217+
var valuesById = {}, values = Object.create(valuesById);
221218
values[valuesById[0] = "UNKNOWN"] = 0;
222219
values[valuesById[1] = "POINT"] = 1;
223220
values[valuesById[2] = "LINESTRING"] = 2;

tests/data/package.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
var $protobuf = require("../../minimal");
55

66
// Common aliases
7-
var $Reader = $protobuf.Reader,
8-
$Writer = $protobuf.Writer,
9-
$util = $protobuf.util;
7+
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
108

119
// Lazily resolved type references
1210
var $lazyTypes = [];

tests/data/rpc.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
var $protobuf = require("../../minimal");
55

66
// Common aliases
7-
var $Reader = $protobuf.Reader,
8-
$Writer = $protobuf.Writer,
9-
$util = $protobuf.util;
7+
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
108

119
// Lazily resolved type references
1210
var $lazyTypes = [];

tests/data/test.js

+10-21
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
var $protobuf = require("../../minimal");
55

66
// Common aliases
7-
var $Reader = $protobuf.Reader,
8-
$Writer = $protobuf.Writer,
9-
$util = $protobuf.util;
7+
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
108

119
// Lazily resolved type references
1210
var $lazyTypes = [];
@@ -177,8 +175,7 @@ $root.jspb = (function() {
177175
* @property {number} BAR=2 BAR value
178176
*/
179177
test.OuterEnum = (function() {
180-
var valuesById = {},
181-
values = Object.create(valuesById);
178+
var valuesById = {}, values = Object.create(valuesById);
182179
values[valuesById[1] = "FOO"] = 1;
183180
values[valuesById[2] = "BAR"] = 2;
184181
return values;
@@ -3135,8 +3132,7 @@ $root.jspb = (function() {
31353132
* @property {number} E2=77 E2 value
31363133
*/
31373134
DefaultValues.Enum = (function() {
3138-
var valuesById = {},
3139-
values = Object.create(valuesById);
3135+
var valuesById = {}, values = Object.create(valuesById);
31403136
values[valuesById[13] = "E1"] = 13;
31413137
values[valuesById[77] = "E2"] = 77;
31423138
return values;
@@ -6497,8 +6493,7 @@ $root.jspb = (function() {
64976493
* @property {number} MAP_VALUE_BAZ_NOBINARY=2 MAP_VALUE_BAZ_NOBINARY value
64986494
*/
64996495
test.MapValueEnumNoBinary = (function() {
6500-
var valuesById = {},
6501-
values = Object.create(valuesById);
6496+
var valuesById = {}, values = Object.create(valuesById);
65026497
values[valuesById[0] = "MAP_VALUE_FOO_NOBINARY"] = 0;
65036498
values[valuesById[1] = "MAP_VALUE_BAR_NOBINARY"] = 1;
65046499
values[valuesById[2] = "MAP_VALUE_BAZ_NOBINARY"] = 2;
@@ -9177,8 +9172,7 @@ $root.google = (function() {
91779172
* @property {number} TYPE_SINT64=18 TYPE_SINT64 value
91789173
*/
91799174
FieldDescriptorProto.Type = (function() {
9180-
var valuesById = {},
9181-
values = Object.create(valuesById);
9175+
var valuesById = {}, values = Object.create(valuesById);
91829176
values[valuesById[1] = "TYPE_DOUBLE"] = 1;
91839177
values[valuesById[2] = "TYPE_FLOAT"] = 2;
91849178
values[valuesById[3] = "TYPE_INT64"] = 3;
@@ -9210,8 +9204,7 @@ $root.google = (function() {
92109204
* @property {number} LABEL_REPEATED=3 LABEL_REPEATED value
92119205
*/
92129206
FieldDescriptorProto.Label = (function() {
9213-
var valuesById = {},
9214-
values = Object.create(valuesById);
9207+
var valuesById = {}, values = Object.create(valuesById);
92159208
values[valuesById[1] = "LABEL_OPTIONAL"] = 1;
92169209
values[valuesById[2] = "LABEL_REQUIRED"] = 2;
92179210
values[valuesById[3] = "LABEL_REPEATED"] = 3;
@@ -10827,8 +10820,7 @@ $root.google = (function() {
1082710820
* @property {number} LITE_RUNTIME=3 LITE_RUNTIME value
1082810821
*/
1082910822
FileOptions.OptimizeMode = (function() {
10830-
var valuesById = {},
10831-
values = Object.create(valuesById);
10823+
var valuesById = {}, values = Object.create(valuesById);
1083210824
values[valuesById[1] = "SPEED"] = 1;
1083310825
values[valuesById[2] = "CODE_SIZE"] = 2;
1083410826
values[valuesById[3] = "LITE_RUNTIME"] = 3;
@@ -11447,8 +11439,7 @@ $root.google = (function() {
1144711439
* @property {number} STRING_PIECE=2 STRING_PIECE value
1144811440
*/
1144911441
FieldOptions.CType = (function() {
11450-
var valuesById = {},
11451-
values = Object.create(valuesById);
11442+
var valuesById = {}, values = Object.create(valuesById);
1145211443
values[valuesById[0] = "STRING"] = 0;
1145311444
values[valuesById[1] = "CORD"] = 1;
1145411445
values[valuesById[2] = "STRING_PIECE"] = 2;
@@ -11465,8 +11456,7 @@ $root.google = (function() {
1146511456
* @property {number} JS_NUMBER=2 JS_NUMBER value
1146611457
*/
1146711458
FieldOptions.JSType = (function() {
11468-
var valuesById = {},
11469-
values = Object.create(valuesById);
11459+
var valuesById = {}, values = Object.create(valuesById);
1147011460
values[valuesById[0] = "JS_NORMAL"] = 0;
1147111461
values[valuesById[1] = "JS_STRING"] = 1;
1147211462
values[valuesById[2] = "JS_NUMBER"] = 2;
@@ -12567,8 +12557,7 @@ $root.google = (function() {
1256712557
* @property {number} IDEMPOTENT=2 IDEMPOTENT value
1256812558
*/
1256912559
MethodOptions.IdempotencyLevel = (function() {
12570-
var valuesById = {},
12571-
values = Object.create(valuesById);
12560+
var valuesById = {}, values = Object.create(valuesById);
1257212561
values[valuesById[0] = "IDEMPOTENCY_UNKNOWN"] = 0;
1257312562
values[valuesById[1] = "NO_SIDE_EFFECTS"] = 1;
1257412563
values[valuesById[2] = "IDEMPOTENT"] = 2;

0 commit comments

Comments
 (0)