Skip to content

Commit 1366f7e

Browse files
committed
Simplify switch statement in parse_statements.
`:on_nl`, `:on_ignored_nl`, `:on_comment`, `:on_embdoc` are handled by a single `when` branch. In this branch there are multiple `if` branches handling `:on_nl` and `:on_ignored_nl` separately from `:on_comment` and `:on_embdoc`. By having separate `when` branches for `:on_nl`/`:on_ignored_nl` and `:on_comment`/`:on_embdoc` we can remove the `if` statements.
1 parent dd3b36e commit 1366f7e

File tree

2 files changed

+92
-49
lines changed

2 files changed

+92
-49
lines changed

lib/rdoc/parser/ruby.rb

+44-49
Original file line numberDiff line numberDiff line change
@@ -1804,65 +1804,60 @@ def parse_statements(container, single = NORMAL, current_method = nil,
18041804
non_comment_seen = true unless (:on_comment == tk[:kind] or :on_embdoc == tk[:kind])
18051805

18061806
case tk[:kind]
1807-
when :on_nl, :on_ignored_nl, :on_comment, :on_embdoc then
1808-
if :on_nl == tk[:kind] or :on_ignored_nl == tk[:kind]
1809-
skip_tkspace
1810-
tk = get_tk
1811-
else
1812-
past_tokens = @read.size > 1 ? @read[0..-2] : []
1813-
nl_position = 0
1814-
past_tokens.reverse.each_with_index do |read_tk, i|
1815-
if read_tk =~ /^\n$/ then
1816-
nl_position = (past_tokens.size - 1) - i
1817-
break
1818-
elsif read_tk =~ /^#.*\n$/ then
1819-
nl_position = ((past_tokens.size - 1) - i) + 1
1820-
break
1821-
end
1822-
end
1823-
comment_only_line = past_tokens[nl_position..-1].all?{ |c| c =~ /^\s+$/ }
1824-
unless comment_only_line then
1825-
tk = get_tk
1807+
when :on_nl, :on_ignored_nl then
1808+
skip_tkspace
1809+
1810+
keep_comment = true
1811+
container.current_line_visibility = nil
1812+
1813+
when :on_comment, :on_embdoc then
1814+
past_tokens = @read.size > 1 ? @read[0..-2] : []
1815+
nl_position = 0
1816+
past_tokens.reverse.each_with_index do |read_tk, i|
1817+
if read_tk =~ /^\n$/ then
1818+
nl_position = (past_tokens.size - 1) - i
1819+
break
1820+
elsif read_tk =~ /^#.*\n$/ then
1821+
nl_position = ((past_tokens.size - 1) - i) + 1
1822+
break
18261823
end
18271824
end
1825+
comment_only_line = past_tokens[nl_position..-1].all?{ |c| c =~ /^\s+$/ }
1826+
unless comment_only_line then
1827+
tk = get_tk
1828+
end
18281829

1829-
if tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) then
1830-
if non_comment_seen then
1831-
# Look for RDoc in a comment about to be thrown away
1832-
non_comment_seen = parse_comment container, tk, comment unless
1833-
comment.empty?
1830+
if non_comment_seen then
1831+
# Look for RDoc in a comment about to be thrown away
1832+
non_comment_seen = parse_comment container, tk, comment unless
1833+
comment.empty?
18341834

1835-
comment = ''
1836-
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
1837-
end
1835+
comment = ''
1836+
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
1837+
end
18381838

1839-
line_no = nil
1840-
while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
1841-
comment_body = retrieve_comment_body(tk)
1842-
line_no = tk[:line_no] if comment.empty?
1843-
comment += comment_body
1844-
comment << "\n" unless comment_body =~ /\n\z/
1845-
1846-
if comment_body.size > 1 && comment_body =~ /\n\z/ then
1847-
skip_tkspace_without_nl # leading spaces
1848-
end
1849-
tk = get_tk
1839+
line_no = nil
1840+
while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
1841+
comment_body = retrieve_comment_body(tk)
1842+
line_no = tk[:line_no] if comment.empty?
1843+
comment += comment_body
1844+
comment << "\n" unless comment_body =~ /\n\z/
1845+
1846+
if comment_body.size > 1 && comment_body =~ /\n\z/ then
1847+
skip_tkspace_without_nl # leading spaces
18501848
end
1849+
tk = get_tk
1850+
end
18511851

1852-
comment = new_comment comment, line_no
1852+
comment = new_comment comment, line_no
18531853

1854-
unless comment.empty? then
1855-
look_for_directives_in container, comment
1854+
unless comment.empty? then
1855+
look_for_directives_in container, comment
18561856

1857-
if container.done_documenting then
1858-
throw :eof if RDoc::TopLevel === container
1859-
container.ongoing_visibility = save_visibility
1860-
end
1857+
if container.done_documenting then
1858+
throw :eof if RDoc::TopLevel === container
1859+
container.ongoing_visibility = save_visibility
18611860
end
1862-
1863-
keep_comment = true
1864-
else
1865-
non_comment_seen = true
18661861
end
18671862

18681863
unget_tk tk

test/rdoc/test_rdoc_parser_ruby.rb

+48
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,54 @@ class Foo
872872
assert_equal 2, foo.method_list.length
873873
end
874874

875+
def test_parse_comments_followed_by_linebreaks
876+
util_parser <<-CLASS
877+
class Foo
878+
# Ignored comment
879+
880+
##
881+
# :method: ghost
882+
# This is a ghost method
883+
884+
# Comment followed by a method
885+
def regular
886+
end
887+
888+
##
889+
# :method: another_ghost
890+
# This is another ghost method
891+
892+
# Comment followed by a linebreak
893+
894+
def regular2
895+
end
896+
end
897+
CLASS
898+
899+
tk = @parser.get_tk
900+
901+
@parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, @comment
902+
903+
foo = @top_level.classes.first
904+
assert_equal 'Foo', foo.full_name
905+
906+
ghost = foo.method_list.first
907+
assert_equal 'Foo#ghost', ghost.full_name
908+
assert_equal 'This is a ghost method', ghost.comment.to_s
909+
910+
regular = foo.method_list[1]
911+
assert_equal 'Foo#regular', regular.full_name
912+
assert_equal 'Comment followed by a method', regular.comment.to_s
913+
914+
another_ghost = foo.method_list[2]
915+
assert_equal 'Foo#another_ghost', another_ghost.full_name
916+
assert_equal 'This is another ghost method', another_ghost.comment.to_s
917+
918+
regular2 = foo.method_list[3]
919+
assert_equal 'Foo#regular2', regular2.full_name
920+
assert_equal 'Comment followed by a linebreak', regular2.comment.to_s
921+
end
922+
875923
def test_parse_class_nodoc
876924
comment = RDoc::Comment.new "##\n# my class\n", @top_level
877925

0 commit comments

Comments
 (0)