Skip to content

Commit 3cb8262

Browse files
committed
CLI: Further improved generated static code style
1 parent 1cc8a24 commit 3cb8262

30 files changed

+3811
-6113
lines changed

CHANGELOG.md

+34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
# [6.5.0](https://github.com/dcodeIO/protobuf.js/releases/tag/6.5.0)
2+
3+
## Breaking
4+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/3946e0fefea415f52a16ea7a74109ff40eee9643) Initial upgrade of converters to real generated functions, see [#620](https://github.com/dcodeIO/protobuf.js/issues/620)<br />
5+
6+
## Fixed
7+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c7e14b1d684aaba2080195cc83900288c5019bbc) Use common utility for virtual oneof getters and setters in both reflection and static code, see [#644](https://github.com/dcodeIO/protobuf.js/issues/644)<br />
8+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/508984b7ff9529906be282375d36fdbada66b8e6) Use Type.toObject/Message.toObject within converters, see [#641](https://github.com/dcodeIO/protobuf.js/issues/641)<br />
9+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/5bca18f2d32e8687986e23edade7c2aeb6b6bac1) Generate null/undefined assertion in fromObject if actually NOT an enum, see [#620](https://github.com/dcodeIO/protobuf.js/issues/620)<br />
10+
11+
## New
12+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/cda5c5452fa0797f1e4c375471aef96f844711f1) Removed scoping iifes from generated static code<br />
13+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/def7b45fb9b5e01028cfa3bf2ecd8272575feb4d) Removed even more clutter from generated static code<br />
14+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/dbd19fd9d3a57d033aad1d7173f7f66db8f8db3e) Removed various clutter from generated static code<br />
15+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/340d6aa82ac17c4a761c681fa71d5a0955032c8b) Now also parses comments, sets them on reflected objects and re-uses them when generating static code, see [#640](https://github.com/dcodeIO/protobuf.js/issues/640)<br />
16+
17+
## CLI
18+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/1cc8a2460c7e161c9bc58fa441ec88e752df409c) Made sure that static target's replacement regexes don't match fields<br />
19+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/d4272dbf5d0b2577af8efb74a94d246e2e0d728e) Also accept (trailing) triple-slash comments for compatibility with protoc-gen-doc, see [#640](https://github.com/dcodeIO/protobuf.js/issues/640)<br />
20+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/508984b7ff9529906be282375d36fdbada66b8e6) Replace all occurencies of types[%d].values in static code, see [#641](https://github.com/dcodeIO/protobuf.js/issues/641)<br />
21+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/0a3862b75fa60ef732e0cd36d623f025acc2fb45) Use semver to validate that CLI dependencies actually satisfy the required version, see [#637](https://github.com/dcodeIO/protobuf.js/issues/637)<br />
22+
23+
## Docs
24+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/9e360ea6a74d41307483e51f18769df7f5b047b9) Added a hint on documenting .proto files for static code<br />
25+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/d2a97bb818474645cf7ce1832952b2c3c739b234) Documented internally used codegen partials for what it's worth<br />
26+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/a08ee2305a8d29290b8203c187a4a453898e8f6f) Updated docs to reflect recent updates<br />
27+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/079388ca65dfd581d74188a6ae49cfa01b103809) Updated converter documentation<br />
28+
29+
## Other
30+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/c280a4a18c6d81c3468177b2ea58ae3bc4f25e73) Removed now useless trailing comment checks, see [#640](https://github.com/dcodeIO/protobuf.js/issues/640)<br />
31+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/3f6ffd01c3cb7d7fc4129c45dc803abb5211a1fd) Restrict comment parsing for static code to explicit /**-blocks because old protos may generate a lot of nonsense otherwise, see [#640](https://github.com/dcodeIO/protobuf.js/issues/640)<br />
32+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/a4954ed2b63e349c96acc5c471241a5abb79854d) Added semver to dev/cli dependencies<br />
33+
[:hash:](https://github.com/dcodeIO/protobuf.js/commit/44167db494c49d9e4b561a66ad9ce2d8ed865a21) Ensured that pbjs' beautify does not break regular expressions in generated verify functions<br />
34+
135
# [6.4.6](https://github.com/dcodeIO/protobuf.js/releases/tag/6.4.6)
236

337
## Fixed

cli/targets/static.js

+69-31
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
"use strict";
22
module.exports = static_target;
33

4-
var protobuf = require("../.."),
5-
cliUtil = require("../util"),
6-
UglifyJS = require("uglify-js");
4+
var protobuf = require("../.."),
5+
cliUtil = require("../util"),
6+
UglifyJS = require("uglify-js"),
7+
esprima = require("esprima"),
8+
escodegen = require("escodegen"),
9+
estraverse = require("estraverse");
710

811
var Type = protobuf.Type,
912
Service = protobuf.Service,
@@ -139,33 +142,63 @@ function buildNamespace(ref, ns) {
139142
}
140143
}
141144

142-
function beautify(code) {
143-
return UglifyJS.minify(code
145+
var reduceableBlockStatements = {
146+
IfStatement: true,
147+
ForStatement: true,
148+
WhileStatement: true
149+
};
150+
151+
function beautifyCode(code) {
152+
// Rename short vars
153+
code = code
144154
.replace(/\b(?!\\)r\b/g, "reader")
145155
.replace(/\b(?!\\)w\b/g, "writer")
146156
.replace(/\b(?!\\)m\b/g, "message")
147157
.replace(/\b(?!\\)t\b/g, "tag")
148-
.replace(/\b(?!\\)l\b/g, "len")
158+
.replace(/\b(?!\\)l\b/g, "length")
149159
.replace(/\b(?!\\)c\b/g, "end")
150160
.replace(/\b(?!\\)c2\b/g, "end2")
151161
.replace(/\b(?!\\)k\b/g, "key")
152162
.replace(/\b(?!\\)ks\b/g, "keys")
153163
.replace(/\b(?!\\)ks2\b/g, "keys2")
154-
.replace(/\b(?!\\)e\b/g, "err")
164+
.replace(/\b(?!\\)e\b/g, "error")
155165
.replace(/\b(?!\\)f\b/g, "impl")
156166
.replace(/\b(?!\\)o\b/g, "options")
157167
.replace(/\b(?!\\)d\b/g, "object")
158-
.replace(/\b(?!\\)n\b/g, "long"),
159-
{
160-
fromString: true,
161-
compress: false,
162-
mangle: false,
163-
output: {
164-
beautify: true,
165-
bracketize: true
166-
}
168+
.replace(/\b(?!\\)n\b/g, "long");
169+
// Add semicolons
170+
code = UglifyJS.minify(code, {
171+
fromString: true,
172+
compress: false,
173+
mangle: false,
174+
output: {
175+
beautify: true
176+
}
177+
}).code;
178+
// Properly beautify
179+
var ast = esprima.parse(code);
180+
estraverse.replace(ast, {
181+
enter: function(node, parent) {
182+
// remove braces around block statements with a single child
183+
if (node.type === "BlockStatement" && reduceableBlockStatements[parent.type] && node.body.length === 1)
184+
return node.body[0];
185+
return undefined;
186+
}
187+
});
188+
code = escodegen.generate(ast, {
189+
format: {
190+
newline: "\r\n",
191+
quotes: "double"
167192
}
168-
).code.replace(/ {4}/g, "\t");
193+
});
194+
// Add id, wireType comments
195+
if (config.comments)
196+
code = code.replace(/\.uint32\((\d+)\)/g, function($0, $1) {
197+
var id = $1 >>> 3,
198+
wireType = $1 & 7;
199+
return ".uint32(/* id " + id + ", wireType " + wireType + " =*/" + $1 + ")";
200+
});
201+
return code;
169202
}
170203

171204
function buildFunction(type, functionName, gen, scope) {
@@ -178,7 +211,9 @@ function buildFunction(type, functionName, gen, scope) {
178211
.replace(/\b(?!\.)types\[\b/g, "$types[");
179212

180213
if (config.beautify)
181-
code = beautify(code);
214+
code = beautifyCode(code);
215+
216+
code = code.replace(/ {4}/g, "\t");
182217

183218
var hasScope = scope && Object.keys(scope).length;
184219

@@ -366,8 +401,8 @@ function buildType(ref, type) {
366401
push("");
367402
pushComment([
368403
"Encodes the specified " + type.name + " message.",
369-
"@param {" + fullName + "|Object} message " + type.name + " message or plain object to encode",
370-
"@param {$protobuf.Writer} [writer] Writer to encode to",
404+
"@param {" + fullName + "|Object} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode",
405+
"@param {$protobuf.Writer} [" + (config.beautify ? "writer" : "w") + "] Writer to encode to",
371406
"@returns {$protobuf.Writer} Writer"
372407
]);
373408
buildFunction(type, "encode", protobuf.encoder(type));
@@ -394,8 +429,8 @@ function buildType(ref, type) {
394429
push("");
395430
pushComment([
396431
"Decodes " + aOrAn(type.name) + " message from the specified reader or buffer.",
397-
"@param {$protobuf.Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from",
398-
"@param {number} [length] Message length if known beforehand",
432+
"@param {$protobuf.Reader|Uint8Array} " + (config.beautify ? "reader" : "r") + " Reader or buffer to decode from",
433+
"@param {number} [" + (config.beautify ? "length" : "l") + "] Message length if known beforehand",
399434
"@returns {" + fullName + "} " + type.name
400435
]);
401436
buildFunction(type, "decode", protobuf.decoder(type));
@@ -404,13 +439,16 @@ function buildType(ref, type) {
404439
push("");
405440
pushComment([
406441
"Decodes " + aOrAn(type.name) + " message from the specified reader or buffer, length delimited.",
407-
"@param {$protobuf.Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from",
442+
"@param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from",
408443
"@returns {" + fullName + "} " + type.name
409444
]);
410-
push(name(type.name) + ".decodeDelimited = function decodeDelimited(readerOrBuffer) {");
445+
push(name(type.name) + ".decodeDelimited = function decodeDelimited(reader) {");
411446
++indent;
412-
push("readerOrBuffer = readerOrBuffer instanceof $Reader ? readerOrBuffer : $Reader(readerOrBuffer);");
413-
push("return this.decode(readerOrBuffer, readerOrBuffer.uint32());");
447+
push("if (!(reader instanceof $Reader))");
448+
++indent;
449+
push("reader = $Reader(reader);");
450+
--indent;
451+
push("return this.decode(reader, reader.uint32());");
414452
--indent;
415453
push("};");
416454

@@ -421,7 +459,7 @@ function buildType(ref, type) {
421459
push("");
422460
pushComment([
423461
"Verifies " + aOrAn(type.name) + " message.",
424-
"@param {" + fullName + "|Object} message " + type.name + " message or plain object to verify",
462+
"@param {" + fullName + "|Object} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to verify",
425463
"@returns {?string} `null` if valid, otherwise the reason why it is not"
426464
]);
427465
buildFunction(type, "verify", protobuf.verifier(type));
@@ -432,7 +470,7 @@ function buildType(ref, type) {
432470
push("");
433471
pushComment([
434472
"Creates " + aOrAn(type.name) + " message from a plain object. Also converts values to their respective internal types.",
435-
"@param {Object.<string,*>} object Plain object",
473+
"@param {Object.<string,*>} " + (config.beautify ? "object" : "d") + " Plain object",
436474
"@returns {" + fullName + "} " + type.name
437475
]);
438476
buildFunction(type, "fromObject", protobuf.converter.fromObject(type));
@@ -449,8 +487,8 @@ function buildType(ref, type) {
449487
push("");
450488
pushComment([
451489
"Creates a plain object from " + aOrAn(type.name) + " message. Also converts values to other types if specified.",
452-
"@param {" + fullName + "} message " + type.name,
453-
"@param {$protobuf.ConversionOptions} [options] Conversion options",
490+
"@param {" + fullName + "} " + (config.beautify ? "message" : "m") + " " + type.name,
491+
"@param {$protobuf.ConversionOptions} [" + (config.beautify ? "options" : "o") + "] Conversion options",
454492
"@returns {Object.<string,*>} Plain object"
455493
]);
456494
buildFunction(type, "toObject", protobuf.converter.toObject(type));
@@ -561,7 +599,7 @@ function buildService(ref, service) {
561599
"@param {" + cbName + "} callback Node-style callback called with the error, if any, and " + method.resolvedResponseType.name,
562600
"@returns {undefined}"
563601
]);
564-
push(name(service.name) + ".prototype[" + JSON.stringify(lcName) + "] = function " + name(lcName) + "(request, callback) {");
602+
push(name(service.name) + ".prototype" + util.safeProp(lcName) + " = function " + name(lcName) + "(request, callback) {");
565603
++indent;
566604
push("var requestData;");
567605
push("try {");

dist/noparse/protobuf.js

+17-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/noparse/protobuf.min.js.gz

-1 Bytes
Binary file not shown.

dist/noparse/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js

+17-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)