Skip to content

Commit

Permalink
fix: type names can be split into multiple tokens (#1877)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-fenster authored Mar 27, 2023
1 parent e721d04 commit 8817ee6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,16 @@ function parse(source, root, options) {
parseGroup(parent, rule);
return;
}
// Type names can consume multiple tokens, in multiple variants:
// package.subpackage field tokens: "package.subpackage" [TYPE NAME ENDS HERE] "field"
// package . subpackage field tokens: "package" "." "subpackage" [TYPE NAME ENDS HERE] "field"
// package. subpackage field tokens: "package." "subpackage" [TYPE NAME ENDS HERE] "field"
// package .subpackage field tokens: "package" ".subpackage" [TYPE NAME ENDS HERE] "field"
// Keep reading tokens until we get a type name with no period at the end,
// and the next token does not start with a period.
while (type.endsWith(".") || peek().startsWith(".")) {
type += next();
}

/* istanbul ignore if */
if (!typeRefRe.test(type))
Expand Down
14 changes: 14 additions & 0 deletions tests/comp_whitespace-in-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var tape = require("tape");
var protobuf = require("..");

tape.test("parsing line breaks", function (test) {

test.test(test.name + " - field options (Int)", function (test) {
var root = protobuf.loadSync("tests/data/whitespace-in-type.proto");
var message = root.lookupType('some.really.long.name.which.does.not.really.make.any.sense.but.sometimes.we.still.see.stuff.like.this.WouldYouParseThisForMePlease');
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');
test.end();
});

test.end();
});
16 changes: 16 additions & 0 deletions tests/data/whitespace-in-type.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";

package some.really.long.name.which.does.not.really.make.any.sense.but.sometimes.we.still.see.stuff.like.this;

message WouldYouParseThisForMePlease {
optional some.really.long. name . which . does .not
.really.make.any. sense.but.
sometimes.we.still
.
see.stuff .like.this
.Test field = 1;
}

message Test {
string field = 1;
}

0 comments on commit 8817ee6

Please sign in to comment.