Skip to content

Commit f4c498d

Browse files
authored
Merge pull request #506 from rhenium/ky/constant-visibility
Support constant visibility
2 parents e897076 + 4ba9249 commit f4c498d

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

lib/rdoc/constant.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def initialize(name, value, comment)
3636
@value = value
3737

3838
@is_alias_for = nil
39-
@visibility = nil
39+
@visibility = :public
4040

4141
self.comment = comment
4242
end
@@ -136,7 +136,7 @@ def marshal_load array
136136
initialize array[1], nil, array[5]
137137

138138
@full_name = array[2]
139-
@visibility = array[3]
139+
@visibility = array[3] || :public
140140
@is_alias_for = array[4]
141141
# 5 handled above
142142
# 6 handled below

lib/rdoc/context.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ def remove_invisible min_visibility
10791079
return if [:private, :nodoc].include? min_visibility
10801080
remove_invisible_in @method_list, min_visibility
10811081
remove_invisible_in @attributes, min_visibility
1082+
remove_invisible_in @constants, min_visibility
10821083
end
10831084

10841085
##
@@ -1165,6 +1166,17 @@ def set_visibility_for(methods, visibility, singleton = false)
11651166
end
11661167
end
11671168

1169+
##
1170+
# Given an array +names+ of constants, set the visibility of each constant to
1171+
# +visibility+
1172+
1173+
def set_constant_visibility_for(names, visibility)
1174+
names.each do |name|
1175+
constant = @constants_hash[name] or next
1176+
constant.visibility = visibility
1177+
end
1178+
end
1179+
11681180
##
11691181
# Sorts sections alphabetically (default) or in TomDoc fashion (none,
11701182
# Public, Internal, Deprecated)

lib/rdoc/parser/ruby.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# * aliases
2525
# * private, public, protected
2626
# * private_class_function, public_class_function
27+
# * private_constant, public_constant
2728
# * module_function
2829
# * attr, attr_reader, attr_writer, attr_accessor
2930
# * extra accessors given on the command line
@@ -1084,6 +1085,9 @@ def parse_identifier container, single, tk, comment # :nodoc:
10841085
'public_class_method', 'module_function' then
10851086
parse_visibility container, single, tk
10861087
return true
1088+
when 'private_constant', 'public_constant'
1089+
parse_constant_visibility container, single, tk
1090+
return true
10871091
when 'attr' then
10881092
parse_attr container, single, tk, comment
10891093
when /^attr_(reader|writer|accessor)$/ then
@@ -1888,6 +1892,22 @@ def parse_visibility(container, single, tk)
18881892
end
18891893
end
18901894

1895+
##
1896+
# Parses a Module#private_constant or Module#public_constant call from +tk+.
1897+
1898+
def parse_constant_visibility(container, single, tk)
1899+
args = parse_symbol_arg
1900+
case tk.name
1901+
when 'private_constant'
1902+
vis = :private
1903+
when 'public_constant'
1904+
vis = :public
1905+
else
1906+
raise RDoc::Error, 'Unreachable'
1907+
end
1908+
container.set_constant_visibility_for args, vis
1909+
end
1910+
18911911
##
18921912
# Determines the block parameter for +context+
18931913

test/test_rdoc_constant.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_marshal_dump
8686
assert_equal top_level, loaded.file
8787
assert_equal 'Klass::CONST', loaded.full_name
8888
assert_equal 'CONST', loaded.name
89-
assert_nil loaded.visibility
89+
assert_equal :public, loaded.visibility
9090
assert_equal cm, loaded.parent
9191
assert_equal section, loaded.section
9292
end
@@ -114,7 +114,7 @@ def test_marshal_load
114114
assert_equal top_level, loaded.file
115115
assert_equal 'Klass::CONST', loaded.full_name
116116
assert_equal 'CONST', loaded.name
117-
assert_nil loaded.visibility
117+
assert_equal :public, loaded.visibility
118118
assert_equal cm, loaded.parent
119119
assert_equal section, loaded.section
120120

@@ -146,7 +146,7 @@ def test_marshal_load_version_0
146146
assert_equal top_level, loaded.file
147147
assert_equal 'Klass::CONST', loaded.full_name
148148
assert_equal 'CONST', loaded.name
149-
assert_nil loaded.visibility
149+
assert_equal :public, loaded.visibility
150150
assert_equal cm, loaded.parent
151151
assert_equal section, loaded.section
152152

test/test_rdoc_context.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ def test_remove_invisible_private
719719

720720
assert_equal [@pub, @prot, @priv], @vis.method_list
721721
assert_equal [@apub, @aprot, @apriv], @vis.attributes
722+
assert_equal [@cpub, @cpriv], @vis.constants
722723
end
723724

724725
def test_remove_invisible_nodoc
@@ -728,6 +729,7 @@ def test_remove_invisible_nodoc
728729

729730
assert_equal [@pub, @prot, @priv], @vis.method_list
730731
assert_equal [@apub, @aprot, @apriv], @vis.attributes
732+
assert_equal [@cpub, @cpriv], @vis.constants
731733
end
732734

733735
def test_remove_invisible_protected
@@ -737,6 +739,7 @@ def test_remove_invisible_protected
737739

738740
assert_equal [@pub, @prot], @vis.method_list
739741
assert_equal [@apub, @aprot], @vis.attributes
742+
assert_equal [@cpub], @vis.constants
740743
end
741744

742745
def test_remove_invisible_public
@@ -746,6 +749,7 @@ def test_remove_invisible_public
746749

747750
assert_equal [@pub], @vis.method_list
748751
assert_equal [@apub], @vis.attributes
752+
assert_equal [@cpub], @vis.constants
749753
end
750754

751755
def test_remove_invisible_public_force
@@ -755,11 +759,13 @@ def test_remove_invisible_public_force
755759
@prot.force_documentation = true
756760
@apriv.force_documentation = true
757761
@aprot.force_documentation = true
762+
@cpriv.force_documentation = true
758763

759764
@vis.remove_invisible :public
760765

761766
assert_equal [@pub, @prot, @priv], @vis.method_list
762767
assert_equal [@apub, @aprot, @apriv], @vis.attributes
768+
assert_equal [@cpub, @cpriv], @vis.constants
763769
end
764770

765771
def test_remove_invisible_in_protected
@@ -922,6 +928,9 @@ def util_visibilities
922928
@aprot = RDoc::Attr.new nil, 'prot', 'RW', nil
923929
@apriv = RDoc::Attr.new nil, 'priv', 'RW', nil
924930

931+
@cpub = RDoc::Constant.new 'CONST_PUBLIC', nil, nil
932+
@cpriv = RDoc::Constant.new 'CONST_PRIVATE', nil, nil
933+
925934
@vis = RDoc::NormalClass.new 'Vis'
926935
@vis.add_method @pub
927936
@vis.add_method @prot
@@ -931,11 +940,16 @@ def util_visibilities
931940
@vis.add_attribute @aprot
932941
@vis.add_attribute @apriv
933942

943+
@vis.add_constant @cpub
944+
@vis.add_constant @cpriv
945+
934946
@prot.visibility = :protected
935947
@priv.visibility = :private
936948

937949
@aprot.visibility = :protected
938950
@apriv.visibility = :private
951+
952+
@cpriv.visibility = :private
939953
end
940954

941955
end

test/test_rdoc_parser_ruby.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,34 @@ def m4() end
35373537
assert_equal 2, public_method_count
35383538
end
35393539

3540+
def test_scan_constant_visibility
3541+
util_parser <<-RUBY
3542+
class C
3543+
CONST_A = 123
3544+
3545+
CONST_B = 234
3546+
private_constant :CONST_B
3547+
3548+
CONST_C = 345
3549+
public_constant :CONST_C
3550+
end
3551+
RUBY
3552+
3553+
@parser.scan
3554+
3555+
c = @store.find_class_named 'C'
3556+
const_a, const_b, const_c, const_d = c.constants.sort_by(&:name)
3557+
3558+
assert_equal 'CONST_A', const_a.name
3559+
assert_equal :public, const_a.visibility
3560+
3561+
assert_equal 'CONST_B', const_b.name
3562+
assert_equal :private, const_b.visibility
3563+
3564+
assert_equal 'CONST_C', const_c.name
3565+
assert_equal :public, const_c.visibility
3566+
end
3567+
35403568
def test_singleton_method_via_eigenclass
35413569
util_parser <<-RUBY
35423570
class C

0 commit comments

Comments
 (0)