Skip to content

Fix constant assignment as alias with Hash #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,23 @@ def parse_constant container, tk, comment, ignore_constants = false
eq_tk = get_tk
end

is_array_or_hash = false
if TkfLBRACK === eq_tk
nest = 1
while bracket_tk = get_tk
case bracket_tk
when TkfLBRACK, TkLBRACK
nest += 1
when TkRBRACK
nest -= 1
break if nest == 0
end
end
skip_tkspace false
eq_tk = get_tk
is_array_or_hash = true
end

unless TkASSIGN === eq_tk then
unget_tk eq_tk
return false
Expand All @@ -874,7 +891,7 @@ def parse_constant container, tk, comment, ignore_constants = false
value = ''
con = RDoc::Constant.new name, value, comment

body = parse_constant_body container, con
body = parse_constant_body container, con, is_array_or_hash

return unless body

Expand All @@ -883,13 +900,15 @@ def parse_constant container, tk, comment, ignore_constants = false
con.line = line_no
read_documentation_modifiers con, RDoc::CONSTANT_MODIFIERS

return if is_array_or_hash

@stats.add_constant con
container.add_constant con

true
end

def parse_constant_body container, constant # :nodoc:
def parse_constant_body container, constant, is_array_or_hash # :nodoc:
nest = 0
rhs_name = ''

Expand Down Expand Up @@ -918,7 +937,7 @@ def parse_constant_body container, constant # :nodoc:
rhs_name << tk.name

if nest <= 0 and TkNL === peek_tk then
create_module_alias container, constant, rhs_name
create_module_alias container, constant, rhs_name unless is_array_or_hash
break
end
when TkNL then
Expand Down
27 changes: 27 additions & 0 deletions test/test_rdoc_parser_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,33 @@ def test_parse_comment_nested
assert_equal 'comment', c.comment
end

def test_parse_constant_with_bracket
util_parser <<-RUBY
class Klass
end

class Klass2
CONSTANT = Klass
end

class Klass3
CONSTANT_2 = {}
CONSTANT_2[1] = Klass
end
RUBY

@parser.scan

klass = @store.find_class_named 'Klass'
klass2 = @store.find_class_named 'Klass2'
klass3 = @store.find_class_named 'Klass3'
constant = klass2.find_module_named 'CONSTANT'
constant2 = klass3.find_module_named 'CONSTANT_2'
assert_equal klass, klass2.constants.first.is_alias_for
refute_equal klass, klass3.constants.first.is_alias_for
assert_nil klass3.find_module_named 'CONSTANT_2'
end

def test_parse_extend_or_include_extend
klass = RDoc::NormalClass.new 'C'
klass.parent = @top_level
Expand Down