Skip to content

Commit 340d6aa

Browse files
committed
New: Now also parses comments, sets them on reflected objects and re-uses them when generating static code, see #640
1 parent 508984b commit 340d6aa

34 files changed

+2203
-232
lines changed

cli/targets/static.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ function push(line) {
7474
function pushComment(lines) {
7575
if (!config.comments)
7676
return;
77+
var split = [];
78+
for (var i = 0; i < lines.length; ++i)
79+
if (lines[i] !== null)
80+
Array.prototype.push.apply(split, lines[i].split(/\r?\n/g));
7781
push("/**");
78-
lines.forEach(function(line) {
82+
split.forEach(function(line) {
7983
if (line === null)
8084
return;
81-
push(" * " + line);
85+
push(" * " + line.replace(/\*\//g, "* /"));
8286
});
8387
push(" */");
8488
}
@@ -175,7 +179,7 @@ function beautify(code) {
175179
function buildFunction(type, functionName, gen, scope) {
176180
var code = gen.str(functionName)
177181
.replace(/\(this.ctor\)/g, " $root" + type.fullName) // types: construct directly instead of using reflected ctor
178-
.replace(/(types\[\d+])(\.values)/g,"$1"); // enums: use types[N] instead of reflected types[N].values
182+
.replace(/(types\[\d+])(\.values)/g, "$1"); // enums: use types[N] instead of reflected types[N].values
179183

180184
if (config.beautify)
181185
code = beautify(code);
@@ -237,6 +241,7 @@ function buildType(ref, type) {
237241
push("");
238242
pushComment([
239243
"Constructs a new " + type.name + ".",
244+
type.comment ? "@classdesc " + type.comment : null,
240245
"@exports " + fullName,
241246
"@constructor",
242247
"@param {Object} [properties] Properties to set"
@@ -274,7 +279,7 @@ function buildType(ref, type) {
274279
if (config.comments) {
275280
push("");
276281
pushComment([
277-
type.name + " " + field.name + ".",
282+
field.comment || type.name + " " + field.name + ".",
278283
prop.charAt(0) !== "." ? "@name " + fullName + "#" + field.name : null,
279284
"@type {" + jsType + "}"
280285
]);
@@ -300,7 +305,7 @@ function buildType(ref, type) {
300305
oneof.resolve();
301306
push("");
302307
pushComment([
303-
type.name + " " + oneof.name + ".",
308+
oneof.comment || type.name + " " + oneof.name + ".",
304309
"@name " + fullName + "#" + name(oneof.name),
305310
"@type {string|undefined}"
306311
]);
@@ -537,6 +542,7 @@ function buildService(ref, service) {
537542
push("");
538543
pushComment([
539544
"Constructs a new " + service.name + " service.",
545+
service.comment ? "@classdesc " + service.comment : null,
540546
"@exports " + fullName,
541547
"@constructor",
542548
"@param {RPCImpl} rpc RPC implementation",
@@ -581,7 +587,7 @@ function buildService(ref, service) {
581587
]);
582588
push("");
583589
pushComment([
584-
"Calls " + method.name + ".",
590+
method.comment || "Calls " + method.name + ".",
585591
"@param {" + method.resolvedRequestType.fullName.substring(1) + "|Object} request " + method.resolvedRequestType.name + " message or plain object",
586592
"@param {" + cbName + "} callback Node-style callback called with the error, if any, and " + method.resolvedResponseType.name,
587593
"@returns {undefined}"
@@ -630,19 +636,23 @@ function buildService(ref, service) {
630636
function buildEnum(ref, enm) {
631637
var parentFullName = enm.parent.fullName.substring(1);
632638
push("");
633-
var comment = parentFullName.length ? [
634-
enm.name + " enum.",
635-
"@name " + name(enm.name),
636-
"@memberof " + parentFullName,
637-
"@enum {number}"
638-
] : [
639-
enm.name + " enum.",
640-
"@exports " + name(enm.name),
641-
"@enum {number}"
639+
var comment = [
640+
enm.comment || enm.name + " enum.",
642641
];
642+
if (parentFullName.length) // member
643+
comment.push(
644+
"@name " + name(enm.name),
645+
"@memberof " + parentFullName,
646+
"@enum {number}"
647+
);
648+
else // export
649+
comment.push(
650+
"@exports " + name(enm.name),
651+
"@enum {number}"
652+
);
643653
Object.keys(enm.values).forEach(function(key) {
644654
var val = enm.values[key];
645-
comment.push("@property {number} " + key + "=" + val + " " + key + " value");
655+
comment.push("@property {number} " + key + "=" + val + " " + (enm.comments[key] ? enm.comments[key] : key + " value"));
646656
});
647657
pushComment(comment);
648658
push(name(ref) + "." + name(enm.name) + " = (function() {");

dist/noparse/protobuf.js

Lines changed: 18 additions & 3 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

24 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.

0 commit comments

Comments
 (0)