Skip to content

Commit 297cd5a

Browse files
committed
Added RDoc::MethodAttr#output_name to reduce the occurances of SomeClass::method in output. This should help prevent people from using :: to call class methods, even though that works
1 parent 6261486 commit 297cd5a

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

History.rdoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
<tt>RDoc::Markup@Links</tt>
5353

5454
See RDoc::Markup@Links for further details.
55-
* RDoc now looks for a <tt>markup: parser_name</tt> magic comment to choose
56-
alternate documentation formats. The format comment must be in the first
57-
three lines of the file (to allow for a shebang or modeline).
55+
* For HTML output RDoc uses +SomeClass.method_name+ and
56+
+SomeClass#method_name+ for remote methods and attributes and
57+
+::method_name+ and +#method_name+ for local methods.
5858
* RDoc makes an effort to syntax-highlight ruby code in verbatim sections.
5959
See RDoc::Markup@Paragraphs+and+Verbatim
6060
* Added RDoc::TopLevel#text? and RDoc::Parser::Text to indicate a

lib/rdoc/markup/to_html_crossref.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def link name, text
131131

132132
ref = @cross_reference.resolve name, text
133133

134+
text = ref.output_name @context if RDoc::MethodAttr === ref and not label
135+
134136
case ref
135137
when String then
136138
ref

lib/rdoc/method_attr.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,27 @@ def html_name
268268
# Full method/attribute name including namespace
269269

270270
def full_name
271-
@full_name || "#{parent_name}#{pretty_name}"
271+
@full_name ||= "#{parent_name}#{pretty_name}"
272272
end
273273

274274
##
275275
# '::' for a class method/attribute, '#' for an instance method.
276276

277277
def name_prefix
278-
singleton ? '::' : '#'
278+
@singleton ? '::' : '#'
279+
end
280+
281+
##
282+
# Name for output to HTML. For class methods the full name with a "." is
283+
# used like +SomeClass.method_name+. For instance methods the class name is
284+
# used if +context+ does not match the parent.
285+
#
286+
# This is to help prevent people from using :: to call class methods.
287+
288+
def output_name context
289+
return "#{name_prefix}#{@name}" if context == parent
290+
291+
"#{parent_name}#{@singleton ? '.' : '#'}#{@name}"
279292
end
280293

281294
##

test/test_rdoc_attr.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def test_marshal_dump
6565
assert_equal 'RW', loaded.rw
6666
assert_equal false, loaded.singleton
6767
assert_equal :public, loaded.visibility
68+
end
69+
70+
def test_marshal_dump_singleton
71+
tl = RDoc::TopLevel.new 'file.rb'
72+
73+
@a.comment = 'this is a comment'
74+
@a.record_location tl
75+
76+
cm = RDoc::ClassModule.new 'Klass'
77+
cm.add_attribute @a
6878

6979
@a.rw = 'R'
7080
@a.singleton = true
@@ -74,6 +84,9 @@ def test_marshal_dump
7484

7585
assert_equal @a, loaded
7686

87+
comment = RDoc::Markup::Document.new(
88+
RDoc::Markup::Paragraph.new('this is a comment'))
89+
7790
assert_equal comment, loaded.comment
7891
assert_equal 'Klass::attr', loaded.full_name
7992
assert_equal 'attr', loaded.name

test/test_rdoc_markup_to_html_crossref.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_convert_RDOCLINK_rdoc_ref
4343
def test_convert_RDOCLINK_rdoc_ref_method
4444
result = @to.convert 'rdoc-ref:C1#m'
4545

46-
assert_equal para("<a href=\"C1.html#method-i-m\">C1#m</a>"), result
46+
assert_equal para("<a href=\"C1.html#method-i-m\">#m</a>"), result
4747
end
4848

4949
def test_convert_RDOCLINK_rdoc_ref_method_label
@@ -59,14 +59,13 @@ def test_convert_RDOCLINK_rdoc_ref_method_percent
5959

6060
result = @to.convert 'rdoc-ref:C1#%'
6161

62-
assert_equal para("<a href=\"C1.html#method-i-25\">C1#%</a>"), result
62+
assert_equal para("<a href=\"C1.html#method-i-25\">#%</a>"), result
6363

6464
m.singleton = true
6565

6666
result = @to.convert 'rdoc-ref:C1::%'
6767

68-
assert_equal para("<a href=\"C1.html#method-c-25\">C1::%</a>"), result
69-
68+
assert_equal para("<a href=\"C1.html#method-c-25\">::%</a>"), result
7069
end
7170

7271
def test_convert_RDOCLINK_rdoc_ref_method_percent_label
@@ -113,7 +112,7 @@ def test_handle_special_CROSSREF_label
113112
def test_handle_special_CROSSREF_show_hash_false
114113
@to.show_hash = false
115114

116-
assert_equal "<a href=\"C1.html#method-i-m\">m</a>",
115+
assert_equal "<a href=\"C1.html#method-i-m\">#m</a>",
117116
SPECIAL('#m')
118117
end
119118

@@ -165,7 +164,12 @@ def test_handle_special_TIDYLINK_label
165164
def test_link
166165
assert_equal 'n', @to.link('n', 'n')
167166

168-
assert_equal '<a href="C1.html#method-c-m">m</a>', @to.link('m', 'm')
167+
assert_equal '<a href="C1.html#method-c-m">::m</a>', @to.link('m', 'm')
168+
end
169+
170+
def test_link_class_method_full
171+
assert_equal '<a href="Parent.html#method-c-m">Parent.m</a>',
172+
@to.link('Parent::m', 'Parent::m')
169173
end
170174

171175
def para text

test/test_rdoc_method_attr.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ def test_find_method_or_attribute_recursive
111111
assert_nil @m1_m.find_method_or_attribute 'm'
112112
end
113113

114+
def test_full_name
115+
assert_equal 'C1#m', @c1_m.full_name
116+
assert_equal 'C1::m', @c1__m.full_name
117+
end
118+
119+
def test_output_name
120+
assert_equal '#m', @c1_m.output_name(@c1)
121+
assert_equal '::m', @c1__m.output_name(@c1)
122+
123+
assert_equal 'C1#m', @c1_m.output_name(@c2)
124+
assert_equal 'C1.m', @c1__m.output_name(@c2)
125+
end
126+
114127
def test_search_record
115128
@c1_m.comment = 'This is a comment.'
116129

0 commit comments

Comments
 (0)