From 8825fdbeef64a7c3be479b0b4985bb9be108a13d Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 22 Mar 2023 19:23:55 +0000 Subject: [PATCH] fix: type names can be split into multiple tokens --- src/parse.js | 10 ++++++++++ tests/comp_whitespace-in-type.js | 14 ++++++++++++++ tests/data/whitespace-in-type.proto | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 tests/comp_whitespace-in-type.js create mode 100644 tests/data/whitespace-in-type.proto diff --git a/src/parse.js b/src/parse.js index be9dd5a2f..ebb8b9c42 100644 --- a/src/parse.js +++ b/src/parse.js @@ -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)) diff --git a/tests/comp_whitespace-in-type.js b/tests/comp_whitespace-in-type.js new file mode 100644 index 000000000..3b47c6a72 --- /dev/null +++ b/tests/comp_whitespace-in-type.js @@ -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(); +}); diff --git a/tests/data/whitespace-in-type.proto b/tests/data/whitespace-in-type.proto new file mode 100644 index 000000000..70c1b056e --- /dev/null +++ b/tests/data/whitespace-in-type.proto @@ -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; +}