Skip to content

Commit 8817ee6

Browse files
authoredMar 27, 2023
fix: type names can be split into multiple tokens (#1877)
1 parent e721d04 commit 8817ee6

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
 

‎src/parse.js

+10
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@ function parse(source, root, options) {
360360
parseGroup(parent, rule);
361361
return;
362362
}
363+
// Type names can consume multiple tokens, in multiple variants:
364+
// package.subpackage field tokens: "package.subpackage" [TYPE NAME ENDS HERE] "field"
365+
// package . subpackage field tokens: "package" "." "subpackage" [TYPE NAME ENDS HERE] "field"
366+
// package. subpackage field tokens: "package." "subpackage" [TYPE NAME ENDS HERE] "field"
367+
// package .subpackage field tokens: "package" ".subpackage" [TYPE NAME ENDS HERE] "field"
368+
// Keep reading tokens until we get a type name with no period at the end,
369+
// and the next token does not start with a period.
370+
while (type.endsWith(".") || peek().startsWith(".")) {
371+
type += next();
372+
}
363373

364374
/* istanbul ignore if */
365375
if (!typeRefRe.test(type))

‎tests/comp_whitespace-in-type.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var tape = require("tape");
2+
var protobuf = require("..");
3+
4+
tape.test("parsing line breaks", function (test) {
5+
6+
test.test(test.name + " - field options (Int)", function (test) {
7+
var root = protobuf.loadSync("tests/data/whitespace-in-type.proto");
8+
var message = root.lookupType('some.really.long.name.which.does.not.really.make.any.sense.but.sometimes.we.still.see.stuff.like.this.WouldYouParseThisForMePlease');
9+
test.equal(message.fields.field.type, 'some.really.long.name.which.does.not.really.make.any.sense.but.sometimes.we.still.see.stuff.like.this.Test');
10+
test.end();
11+
});
12+
13+
test.end();
14+
});

‎tests/data/whitespace-in-type.proto

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
3+
package some.really.long.name.which.does.not.really.make.any.sense.but.sometimes.we.still.see.stuff.like.this;
4+
5+
message WouldYouParseThisForMePlease {
6+
optional some.really.long. name . which . does .not
7+
.really.make.any. sense.but.
8+
sometimes.we.still
9+
.
10+
see.stuff .like.this
11+
.Test field = 1;
12+
}
13+
14+
message Test {
15+
string field = 1;
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.