Skip to content

Commit 9d0281d

Browse files
committed
Fix handling of mixed types of first comment
When rd-style and hash-style comments are adjacent at the top of a file Parser::Ruby#collect_first_comment would merge them together. This would lead to incorrect normalization of the comment. Now RDoc detects the mixing and uses the first comment type only. Fixes #266
1 parent f5a22e8 commit 9d0281d

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

History.rdoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Bug fixes
44
* Fixed reporting of undocumented method parameters when including when
55
yield and &block are present. Pull request #281 by Victor Bilyk.
6+
* Fixed merging of rd-style and hash-style comments at the top of a file.
7+
Bug #266 by Zachary Scott.
68
* Removed duplicated condition in superclass fixup. Pull request #282 by
79
Benoit Daloze.
810

lib/rdoc/parser/ruby.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def collect_first_comment
226226
comment = ''
227227
comment.force_encoding @encoding if @encoding
228228
first_line = true
229+
first_comment_tk_class = nil
229230

230231
tk = get_tk
231232

@@ -238,6 +239,9 @@ def collect_first_comment
238239
skip_tkspace
239240
tk = get_tk
240241
else
242+
break if first_comment_tk_class and not first_comment_tk_class === tk
243+
first_comment_tk_class = tk.class
244+
241245
first_line = false
242246
comment << tk.text << "\n"
243247
tk = get_tk

test/test_rdoc_parser_ruby.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ class C; end
6363
assert_equal Encoding::CP852, comment.text.encoding
6464
end
6565

66+
def test_collect_first_comment_rd_hash
67+
parser = util_parser <<-CONTENT
68+
=begin
69+
first
70+
=end
71+
72+
# second
73+
class C; end
74+
CONTENT
75+
76+
comment = parser.collect_first_comment
77+
78+
assert_equal RDoc::Comment.new("first\n\n", @top_level), comment
79+
end
80+
6681
def test_get_class_or_module
6782
ctxt = RDoc::Context.new
6883
ctxt.store = @store

0 commit comments

Comments
 (0)