Skip to content

Commit e550cd2

Browse files
authored
fix: Correct alternate comment mode line numbers (#2159)
1 parent 1c01075 commit e550cd2

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/tokenize.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ function tokenize(source, alternateCommentMode) {
225225
curr,
226226
start,
227227
isDoc,
228+
nextLineIsComment,
228229
isLeadingComment = offset === 0;
229230
do {
230231
if (offset === length)
@@ -278,7 +279,11 @@ function tokenize(source, alternateCommentMode) {
278279
// Trailing comment cannot not be multi-line
279280
break;
280281
}
281-
} while (isDoubleSlashCommentLine(offset));
282+
nextLineIsComment = isDoubleSlashCommentLine(offset);
283+
if (nextLineIsComment) {
284+
line++;
285+
}
286+
} while (nextLineIsComment);
282287
} else {
283288
offset = Math.min(length, findEndOfLine(offset) + 1);
284289
}

tests/docs_comments_alternate_parse.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,24 @@ tape.test("proto comments in alternate-parse mode with trailing comment preferre
8484
test.end();
8585
});
8686
});
87+
88+
tape.test("proto comments in alternate-parse mode maintain line numbers", function(test) {
89+
var source = [
90+
"syntax = \"proto3\";",
91+
"",
92+
"// Multiple lines of",
93+
"// end-of-line comments",
94+
"// should keep the tokenizer",
95+
"// line count correct.",
96+
"",
97+
"message Foo {};a"
98+
].join("\n");
99+
100+
try {
101+
protobuf.parse(source, { alternateCommentMode: true });
102+
test.fail("should throw on invalid token");
103+
} catch (err) {
104+
test.equal(err.message, "illegal token 'a' (line 8)");
105+
}
106+
test.end();
107+
});

0 commit comments

Comments
 (0)