Skip to content

Commit 3f6ffd0

Browse files
committed
Other: Restrict comment parsing for static code to explicit /**-blocks because old protos may generate a lot of nonsense otherwise, see #640
1 parent 340d6aa commit 3f6ffd0

22 files changed

+186
-540
lines changed

cli/targets/static.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ function buildEnum(ref, enm) {
652652
);
653653
Object.keys(enm.values).forEach(function(key) {
654654
var val = enm.values[key];
655-
comment.push("@property {number} " + key + "=" + val + " " + (enm.comments[key] ? enm.comments[key] : key + " value"));
655+
comment.push("@property {number} " + key + "=" + val + " " + (enm.comments[key] || key + " value"));
656656
});
657657
pushComment(comment);
658658
push(name(ref) + "." + name(enm.name) + " = (function() {");

dist/noparse/protobuf.js

+3-1
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

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

dist/noparse/protobuf.min.js.gz

14 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

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

dist/protobuf.min.js

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

dist/protobuf.min.js.gz

-14 Bytes
Binary file not shown.

dist/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/runtime/protobuf.js

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

dist/runtime/protobuf.min.js

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

dist/runtime/protobuf.min.js.gz

0 Bytes
Binary file not shown.

src/converter.js

+2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ function genValuePartial_toObject(gen, field, fieldIndex, prop) {
184184
converter.toObject = function toObject(mtype) {
185185
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
186186
var fields = mtype.fieldsArray;
187+
if (!fields.length)
188+
return util.codegen()("return {}");
187189
var gen = util.codegen("m", "o")
188190
("if(!o)")
189191
("o={}")

src/parse.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,14 @@ function parse(source, root, options) {
313313
throw illegal(name, "name");
314314
name = applyCase(name);
315315
skip("=");
316-
var id = parseId(next());
317-
var field = parseInlineOptions(new Field(name, id, type, rule, extend));
316+
var line = tn.line(),
317+
field = new Field(name, parseId(next()), type, rule, extend);
318318
field.comment = cmnt();
319+
parseInlineOptions(field);
320+
if (!field.comment) {
321+
peek();
322+
field.comment = cmnt(/* if on */ line);
323+
}
319324
// JSON defaults to packed=true if not set so we have to set packed=false explicity when
320325
// parsing proto2 descriptors without the option, where applicable.
321326
if (field.repeated && types.packed[type] !== undefined && !isProto3)
@@ -379,8 +384,14 @@ function parse(source, root, options) {
379384
name = applyCase(name);
380385
skip("=");
381386
var id = parseId(next());
382-
var field = parseInlineOptions(new MapField(name, id, keyType, valueType));
387+
var field = new MapField(name, id, keyType, valueType);
388+
var line = tn.line();
383389
field.comment = cmnt();
390+
parseInlineOptions(field);
391+
if (!field.comment) {
392+
peek();
393+
field.comment = cmnt(/* if on */ line);
394+
}
384395
parent.add(field);
385396
}
386397

@@ -447,7 +458,7 @@ function parse(source, root, options) {
447458
parent.add(name, value, comment);
448459
parseInlineOptions({}); // skips enum value options
449460
if (!comment) {
450-
peek(); // trailing comment?
461+
peek();
451462
parent.comments[name] = cmnt(/* if on */ line);
452463
}
453464
}

src/tokenize.js

+11-19
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ function tokenize(source) {
4343
length = source.length,
4444
line = 1,
4545
comment = null,
46-
commentLine = 0,
47-
commentType = 0;
46+
commentLine = 0;
4847

4948

5049
var stack = [];
@@ -96,23 +95,19 @@ function tokenize(source) {
9695
* @returns {undefined}
9796
* @inner
9897
*/
99-
function setComment(start, end, type) {
100-
var count = 0;
98+
function setComment(start, end) {
10199
var text = source
102-
.substring(start, end)
100+
.substring(start, end);
101+
if (text.charAt(0) !== "*") // use /**-blocks only
102+
return;
103+
text = text
103104
.split(/\n/g)
104105
.map(function(line) {
105-
++count;
106106
return line.replace(/ *[*/]+ */, "").trim();
107107
})
108108
.join("\n")
109109
.trim();
110-
if (comment && commentLine === line - count && type === commentType)
111-
comment += "\n" + text;
112-
else {
113-
comment = text;
114-
commentType = type;
115-
}
110+
comment = text;
116111
commentLine = line;
117112
}
118113

@@ -149,7 +144,6 @@ function tokenize(source) {
149144
if (offset === length)
150145
return null;
151146
++offset;
152-
setComment(start, offset - 1, 0);
153147
++line;
154148
repeat = true;
155149
} else if ((curr = charAt(offset)) === "*") { /* Block */
@@ -234,13 +228,11 @@ function tokenize(source) {
234228
* @returns {?string} Comment, if any
235229
* @inner
236230
*/
237-
function cmnt(ifOnLine) {
238-
var ret = (ifOnLine !== undefined
239-
? commentLine === ifOnLine
240-
: commentLine === line - 1) && comment || null;
241-
if (comment) {
231+
function cmnt() {
232+
var ret = commentLine === line - 1 && comment || null;
233+
if (ret) {
242234
comment = null;
243-
commentLine = -1;
235+
commentLine = 0;
244236
}
245237
return ret;
246238
}

tests/comments.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ tape.test("comments", function(test) {
77
if (err)
88
throw test.fail(err.message);
99

10-
test.equal(root.lookup("Test1").comment, "Message\nwith\na\ncomment.", "should parse * blocks");
11-
test.equal(root.lookup("Test2").comment, "Message\nwith\na\ncomment.", "should parse // blocks");
12-
test.equal(root.lookup("Test3").comment, "a\ncomment.", "should parse the last coherent type of mixed blocks");
10+
test.equal(root.lookup("Test1").comment, "Message\nwith\na\ncomment.", "should parse /**-blocks");
11+
test.equal(root.lookup("Test2").comment, null, "should not parse //-blocks");
12+
test.equal(root.lookup("Test3").comment, null, "should not parse /*-blocks");
1313

1414
test.equal(root.lookup("Test1.field1").comment, "Field with a comment.", "should parse blocks for message fields");
15-
test.equal(root.lookup("Test1.field2").comment, "Field with a comment.", "should parse lines for message fields");
15+
test.equal(root.lookup("Test1.field2").comment, null, "should not parse lines for message fields");
1616

1717
test.equal(root.lookup("Test3").comments.ONE, "Value with a comment.", "should parse blocks for enum values");
18-
test.equal(root.lookup("Test3").comments.TWO, "Value with a comment.", "should parse lines for enum values");
19-
test.equal(root.lookup("Test3").comments.THREE, "Value with a comment.", "should parse trailing lines for enum values");
18+
test.equal(root.lookup("Test3").comments.TWO, null, "should not parse lines for enum values");
2019

2120
test.end();
2221
});

tests/data/comments.js

+5-19
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ $root.Test1 = (function() {
3939
$prototype.field1 = "";
4040

4141
/**
42-
* Field with a comment.
42+
* Test1 field2.
4343
* @type {number}
4444
*/
4545
$prototype.field2 = 0;
@@ -230,10 +230,6 @@ $root.Test2 = (function() {
230230

231231
/**
232232
* Constructs a new Test2.
233-
* @classdesc Message
234-
* with
235-
* a
236-
* comment.
237233
* @exports Test2
238234
* @constructor
239235
* @param {Object} [properties] Properties to set
@@ -350,15 +346,8 @@ $root.Test2 = (function() {
350346
* @param {$protobuf.ConversionOptions} [options] Conversion options
351347
* @returns {Object.<string,*>} Plain object
352348
*/
353-
Test2.toObject = (function() { return function toObject(message, options) {
354-
if (!options) {
355-
options = {};
356-
}
357-
var object = {};
358-
for (var keys = Object.keys(message), i = 0; i < keys.length; ++i) {
359-
switch (keys[i]) {}
360-
}
361-
return object;
349+
Test2.toObject = (function() { return function toObject() {
350+
return {};
362351
};})();
363352

364353
/**
@@ -386,20 +375,17 @@ $root.Test2 = (function() {
386375
})();
387376

388377
/**
389-
* a
390-
* comment.
378+
* Test3 enum.
391379
* @exports Test3
392380
* @enum {number}
393381
* @property {number} ONE=1 Value with a comment.
394-
* @property {number} TWO=2 Value with a comment.
395-
* @property {number} THREE=3 Value with a comment.
382+
* @property {number} TWO=2 TWO value
396383
*/
397384
$root.Test3 = (function() {
398385
var valuesById = {},
399386
values = Object.create(valuesById);
400387
values[valuesById[1] = "ONE"] = 1;
401388
values[valuesById[2] = "TWO"] = 2;
402-
values[valuesById[3] = "THREE"] = 3;
403389
return values;
404390
})();
405391

0 commit comments

Comments
 (0)