Skip to content

Commit

Permalink
Fix a bug in comment parsing (#879)
Browse files Browse the repository at this point in the history
Fixes #871.
  • Loading branch information
osa1 authored Oct 9, 2023
1 parent c16bc89 commit 32ed0fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
7 changes: 7 additions & 0 deletions protoc_plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 21.1.2-dev

* Fix a bug in comment parsing ([#871], [#879]).

[#871]: https://github.com/google/protobuf.dart/issues/871
[#879]: https://github.com/google/protobuf.dart/pull/879

## 21.1.1

* Rename a local variable used with message constructors to avoid potential
Expand Down
13 changes: 9 additions & 4 deletions protoc_plugin/lib/src/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ String? toDartComment(String value) {

var lines = LineSplitter.split(value).toList();

// Find any leading spaces in the first line.
// If all of the lines have the same leading spaces, remove them all.
// Find any leading spaces in the first line. If all of the lines have the
// same leading spaces, remove them all.
final leadingSpaces = _leadingSpaces.firstMatch(lines.first);
if (leadingSpaces != null) {
final prefix = leadingSpaces.group(0)!;
Expand All @@ -48,11 +48,16 @@ String? toDartComment(String value) {
}
}

// Remove empty, trailing lines
while (lines.last.isEmpty) {
// Remove empty, trailing lines.
while (lines.isNotEmpty && lines.last.trim().isEmpty) {
lines.removeLast();
}

// Don't generate a documentation comment if all lines are empty.
if (lines.isEmpty) {
return null;
}

return lines.map((e) => '/// $e'.trimRight()).join('\n');
}

Expand Down
2 changes: 1 addition & 1 deletion protoc_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: protoc_plugin
version: 21.1.1
version: 21.1.2-dev
description: A protobuf protoc compiler plugin used to generate Dart code.
repository: https://github.com/google/protobuf.dart/tree/master/protoc_plugin

Expand Down
4 changes: 4 additions & 0 deletions protoc_plugin/test/shared_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ void main() {
expect(toDartComment(''), null);
});

test('just space', () {
expect(toDartComment(' '), null);
});

test('indent', () {
expect(
toDartComment('''
Expand Down

0 comments on commit 32ed0fe

Please sign in to comment.