Skip to content

Commit 3d84ecd

Browse files
committed
CLI: pbjs now generates more convenient dot-notation property accessors
1 parent 1e0ebc0 commit 3d84ecd

25 files changed

+329
-311
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
protobuf.js
22
===========
3-
[![travis][travis-image]][travis-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url]
3+
[![travis][travis-image]][travis-url] [![david][david-image]][david-url] [![npm][npm-dl-image]][npm-url] [![npm][npm-image]][npm-url] [![donate][paypal-image]][paypal-url]
44

55
**Protocol Buffers** are a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more, originally designed at Google ([see](https://developers.google.com/protocol-buffers/)).
66

77
**protobuf.js** is a pure JavaScript implementation for node and the browser. It efficiently encodes plain objects and custom classes and works out of the box with .proto files.
88

99
[travis-image]: https://img.shields.io/travis/dcodeIO/protobuf.js.svg
1010
[travis-url]: https://travis-ci.org/dcodeIO/protobuf.js
11+
[david-url]: https://david-dm.org/dcodeIO/protobuf.js
12+
[david-image]: https://img.shields.io/david/dcodeIO/protobuf.js.svg
1113
[npm-image]: https://img.shields.io/npm/v/protobufjs.svg
1214
[npm-url]: https://npmjs.org/package/protobufjs
1315
[npm-dl-image]: https://img.shields.io/npm/dm/protobufjs.svg

cli/pbjs.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,30 @@ exports.main = function(args, callback) {
121121
"keepCase": argv["keep-case"] || false
122122
};
123123

124-
var root;
125124
try {
126-
root = root.loadSync(files, parseOptions); // sync is deterministic while async is not
125+
root.loadSync(files, parseOptions); // sync is deterministic while async is not
127126
} catch (err) {
128127
if (callback) {
129128
callback(err);
130-
return;
131-
} else
132-
throw err;
129+
return undefined;
130+
}
131+
throw err;
133132
}
134133

135-
target(root, argv, function(err, output) {
134+
target(root, argv, function targetCallback(err, output) {
136135
if (err) {
137136
if (callback)
138137
return callback(err);
139-
else
140-
throw err;
138+
throw err;
141139
}
142140
if (output !== "") {
143141
if (argv.out)
144142
fs.writeFileSync(argv.out, output, { encoding: "utf8" });
145143
else
146144
process.stdout.write(output, "utf8");
147145
}
148-
if (callback)
149-
return callback(null);
146+
return callback
147+
? callback(null)
148+
: undefined;
150149
});
151150
};

cli/targets/static.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = static_target;
33

44
// - Static code does not have any reflection or JSON features.
55

6-
var protobuf = require("../..");
6+
var protobuf = require("../.."),
7+
cliUtil = require("../util");
78

89
var Type = protobuf.Type,
910
Service = protobuf.Service,
@@ -76,14 +77,23 @@ function pushComment(lines) {
7677
push(" */");
7778
}
7879

79-
var reservedRe = /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/;
80-
8180
function name(name) {
8281
if (!name)
8382
return "$root";
84-
return reservedRe.test(name) ? name + "_" : name;
83+
return cliUtil.reserved(name) ? name + "_" : name;
8584
}
8685

86+
// generate dot-notation property accessors where possible. this saves a few chars (i.e. m.hello
87+
// instead of m["hello"]) but has no measurable performance impact (on V8). not present within the
88+
// library itself because the reserved words check requires a rather longish regex.
89+
util.safeProp = (function(safeProp) {
90+
return function safeProp_dn(name) {
91+
return /^[$\w]+$/.test(name) && cliUtil.reserved(name)
92+
? safeProp(name)
93+
: "." + name;
94+
}
95+
})(util.safeProp);
96+
8797
function buildNamespace(ref, ns) {
8898
if (!ns)
8999
return;

cli/util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ exports.pad = function(str, len, l) {
113113
str = l ? str + " " : " " + str;
114114
return str;
115115
};
116+
117+
exports.reserved = function(name) {
118+
return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name);
119+
};

dist/noparse/protobuf.js

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js.gz

4 Bytes
Binary file not shown.

dist/noparse/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js

Lines changed: 11 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)