Skip to content

Commit 6bd9bf3

Browse files
authored
Merge pull request #509 from aycabta/fix-constant-assignment-as-alias-with-hash
Fix constant assignment as alias with Hash
2 parents e94579a + 6278252 commit 6bd9bf3

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/rdoc/parser/ruby.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,23 @@ def parse_constant container, tk, comment, ignore_constants = false
861861
eq_tk = get_tk
862862
end
863863

864+
is_array_or_hash = false
865+
if TkfLBRACK === eq_tk
866+
nest = 1
867+
while bracket_tk = get_tk
868+
case bracket_tk
869+
when TkfLBRACK, TkLBRACK
870+
nest += 1
871+
when TkRBRACK
872+
nest -= 1
873+
break if nest == 0
874+
end
875+
end
876+
skip_tkspace false
877+
eq_tk = get_tk
878+
is_array_or_hash = true
879+
end
880+
864881
unless TkASSIGN === eq_tk then
865882
unget_tk eq_tk
866883
return false
@@ -874,7 +891,7 @@ def parse_constant container, tk, comment, ignore_constants = false
874891
value = ''
875892
con = RDoc::Constant.new name, value, comment
876893

877-
body = parse_constant_body container, con
894+
body = parse_constant_body container, con, is_array_or_hash
878895

879896
return unless body
880897

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

903+
return if is_array_or_hash
904+
886905
@stats.add_constant con
887906
container.add_constant con
888907

889908
true
890909
end
891910

892-
def parse_constant_body container, constant # :nodoc:
911+
def parse_constant_body container, constant, is_array_or_hash # :nodoc:
893912
nest = 0
894913
rhs_name = ''
895914

@@ -918,7 +937,7 @@ def parse_constant_body container, constant # :nodoc:
918937
rhs_name << tk.name
919938

920939
if nest <= 0 and TkNL === peek_tk then
921-
create_module_alias container, constant, rhs_name
940+
create_module_alias container, constant, rhs_name unless is_array_or_hash
922941
break
923942
end
924943
when TkNL then

test/test_rdoc_parser_ruby.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,33 @@ def test_parse_comment_nested
13581358
assert_equal 'comment', c.comment
13591359
end
13601360

1361+
def test_parse_constant_with_bracket
1362+
util_parser <<-RUBY
1363+
class Klass
1364+
end
1365+
1366+
class Klass2
1367+
CONSTANT = Klass
1368+
end
1369+
1370+
class Klass3
1371+
CONSTANT_2 = {}
1372+
CONSTANT_2[1] = Klass
1373+
end
1374+
RUBY
1375+
1376+
@parser.scan
1377+
1378+
klass = @store.find_class_named 'Klass'
1379+
klass2 = @store.find_class_named 'Klass2'
1380+
klass3 = @store.find_class_named 'Klass3'
1381+
constant = klass2.find_module_named 'CONSTANT'
1382+
constant2 = klass3.find_module_named 'CONSTANT_2'
1383+
assert_equal klass, klass2.constants.first.is_alias_for
1384+
refute_equal klass, klass3.constants.first.is_alias_for
1385+
assert_nil klass3.find_module_named 'CONSTANT_2'
1386+
end
1387+
13611388
def test_parse_extend_or_include_extend
13621389
klass = RDoc::NormalClass.new 'C'
13631390
klass.parent = @top_level

0 commit comments

Comments
 (0)