Skip to content

Commit 523622d

Browse files
committed
Document is not a Comment. Document shouldn't be assigned to code_object.comment
CodeObject#comment is normally String or Comment, but Marshal.dump and load creates CodeObject with comment=Document. Some method requires Document stored in CodeObject#comment, some requires Comment. We should stop mixing Document with Comment because it is mixing huge complexity and potential bugs to RDoc codebase.
1 parent 6852567 commit 523622d

21 files changed

+141
-180
lines changed

lib/rdoc/code_object.rb

-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ def initialize_visibility # :nodoc:
141141
def comment=(comment)
142142
@comment = case comment
143143
when NilClass then ''
144-
when RDoc::Markup::Document then comment
145144
when RDoc::Comment then comment.normalize
146145
else
147146
if comment and not comment.empty? then

lib/rdoc/code_object/any_method.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def marshal_load array
198198
@full_name = array[2]
199199
@singleton = array[3]
200200
@visibility = array[4]
201-
@comment = array[5]
201+
@comment = RDoc::Comment.from_document array[5]
202202
@call_seq = array[6]
203203
@block_params = array[7]
204204
# 8 handled below
@@ -210,8 +210,8 @@ def marshal_load array
210210
@section_title = array[14]
211211
@is_alias_for = array[15]
212212

213-
array[8].each do |new_name, comment|
214-
add_alias RDoc::Alias.new(nil, @name, new_name, comment, @singleton)
213+
array[8].each do |new_name, document|
214+
add_alias RDoc::Alias.new(nil, @name, new_name, RDoc::Comment.from_document(document), @singleton)
215215
end
216216

217217
@parent_name ||= if @full_name =~ /#/ then

lib/rdoc/code_object/attr.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def marshal_load array
136136
@full_name = array[2]
137137
@rw = array[3]
138138
@visibility = array[4]
139-
@comment = array[5]
139+
@comment = RDoc::Comment.from_document array[5]
140140
@singleton = array[6] || false # MARSHAL_VERSION == 0
141141
# 7 handled below
142142
@parent_name = array[8]

lib/rdoc/code_object/class_module.rb

+14-11
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,14 @@ def marshal_load array # :nodoc:
359359
@name = array[1]
360360
@full_name = array[2]
361361
@superclass = array[3]
362-
@comment = array[4]
362+
document = array[4]
363363

364-
@comment_location = if RDoc::Markup::Document === @comment.parts.first then
365-
@comment
364+
@comment = RDoc::Comment.from_document document
365+
366+
@comment_location = if RDoc::Markup::Document === document.parts.first then
367+
document
366368
else
367-
RDoc::Markup::Document.new @comment
369+
RDoc::Markup::Document.new document
368370
end
369371

370372
array[5].each do |name, rw, visibility, singleton, file|
@@ -378,18 +380,18 @@ def marshal_load array # :nodoc:
378380
attr.record_location RDoc::TopLevel.new file
379381
end
380382

381-
array[6].each do |constant, comment, file|
383+
array[6].each do |constant, document, file|
382384
case constant
383385
when RDoc::Constant then
384386
add_constant constant
385387
else
386-
constant = add_constant RDoc::Constant.new(constant, nil, comment)
388+
constant = add_constant RDoc::Constant.new(constant, nil, RDoc::Comment.from_document(document))
387389
constant.record_location RDoc::TopLevel.new file
388390
end
389391
end
390392

391-
array[7].each do |name, comment, file|
392-
incl = add_include RDoc::Include.new(name, comment)
393+
array[7].each do |name, document, file|
394+
incl = add_include RDoc::Include.new(name, RDoc::Comment.from_document(document))
393395
incl.record_location RDoc::TopLevel.new file
394396
end
395397

@@ -406,8 +408,8 @@ def marshal_load array # :nodoc:
406408
end
407409
end
408410

409-
array[9].each do |name, comment, file|
410-
ext = add_extend RDoc::Extend.new(name, comment)
411+
array[9].each do |name, document, file|
412+
ext = add_extend RDoc::Extend.new(name, RDoc::Comment.from_document(document))
411413
ext.record_location RDoc::TopLevel.new file
412414
end if array[9] # Support Marshal version 1
413415

@@ -444,7 +446,8 @@ def merge class_module
444446

445447
document = document.merge other_document
446448

447-
@comment = @comment_location = document
449+
@comment = RDoc::Comment.from_document(document)
450+
@comment_location = document
448451
end
449452

450453
cm = class_module

lib/rdoc/code_object/constant.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def marshal_dump
133133
# * #parent_name
134134

135135
def marshal_load array
136-
initialize array[1], nil, array[5]
136+
initialize array[1], nil, RDoc::Comment.from_document(array[5])
137137

138138
@full_name = array[2]
139139
@visibility = array[3] || :public

lib/rdoc/code_object/context/section.rb

+12-65
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,13 @@ def == other
6161
# Adds +comment+ to this section
6262

6363
def add_comment comment
64-
comment = extract_comment comment
65-
66-
return if comment.empty?
67-
68-
case comment
69-
when RDoc::Comment then
70-
@comments << comment
71-
when RDoc::Markup::Document then
72-
@comments.concat comment.parts
73-
when Array then
74-
@comments.concat comment
64+
if comment.is_a?(Array)
65+
comment.each do |c|
66+
@comments << extract_comment(c)
67+
end
7568
else
76-
raise TypeError, "unknown comment type: #{comment.inspect}"
69+
comment = extract_comment(comment)
70+
@comments << comment unless comment.empty?
7771
end
7872
end
7973

@@ -97,10 +91,6 @@ def aref
9791

9892
def extract_comment comment
9993
case comment
100-
when Array then
101-
comment.map do |c|
102-
extract_comment c
103-
end
10494
when nil
10595
RDoc::Comment.new ''
10696
when RDoc::Comment then
@@ -115,8 +105,6 @@ def extract_comment comment
115105
end
116106
end
117107

118-
comment
119-
when RDoc::Markup::Document then
120108
comment
121109
else
122110
raise TypeError, "unknown comment #{comment.inspect}"
@@ -135,19 +123,8 @@ def hash # :nodoc:
135123
# The files comments in this section come from
136124

137125
def in_files
138-
return [] if @comments.empty?
139-
140-
case @comments
141-
when Array then
142-
@comments.map do |comment|
143-
comment.file
144-
end
145-
when RDoc::Markup::Document then
146-
@comment.parts.map do |document|
147-
document.file
148-
end
149-
else
150-
raise RDoc::Error, "BUG: unknown comment class #{@comments.class}"
126+
@comments.map do |comment|
127+
comment.file
151128
end
152129
end
153130

@@ -170,34 +147,15 @@ def marshal_load array
170147
@parent = nil
171148

172149
@title = array[1]
173-
@comments = array[2]
150+
@comments = array[2].parts.map { |doc| RDoc::Comment.from_document(doc) }
174151
end
175152

176153
##
177154
# Parses +comment_location+ into an RDoc::Markup::Document composed of
178155
# multiple RDoc::Markup::Documents with their file set.
179156

180157
def parse
181-
case @comments
182-
when String then
183-
super
184-
when Array then
185-
docs = @comments.map do |comment, location|
186-
doc = super comment
187-
doc.file = location if location
188-
doc
189-
end
190-
191-
RDoc::Markup::Document.new(*docs)
192-
when RDoc::Comment then
193-
doc = super @comments.text, comments.format
194-
doc.file = @comments.location
195-
doc
196-
when RDoc::Markup::Document then
197-
return @comments
198-
else
199-
raise ArgumentError, "unknown comment class #{comments.class}"
200-
end
158+
RDoc::Markup::Document.new(*@comments.map(&:parse))
201159
end
202160

203161
##
@@ -214,19 +172,8 @@ def plain_html
214172
# +comment+
215173

216174
def remove_comment comment
217-
return if @comments.empty?
218-
219-
case @comments
220-
when Array then
221-
@comments.delete_if do |my_comment|
222-
my_comment.file == comment.file
223-
end
224-
when RDoc::Markup::Document then
225-
@comments.parts.delete_if do |document|
226-
document.file == comment.file.name
227-
end
228-
else
229-
raise RDoc::Error, "BUG: unknown comment class #{@comments.class}"
175+
@comments.delete_if do |my_comment|
176+
my_comment.file == comment.file
230177
end
231178
end
232179

lib/rdoc/code_object/top_level.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def marshal_load array # :nodoc:
214214
initialize array[1]
215215

216216
@parser = array[2]
217-
@comment = array[3]
217+
@comment = RDoc::Comment.from_document array[3]
218218

219219
@file_stat = nil
220220
end

lib/rdoc/comment.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def extract_call_seq method
126126
# A comment is empty if its text String is empty.
127127

128128
def empty?
129-
@text.empty?
129+
@text.empty? && (@document.nil? || @document.empty?)
130130
end
131131

132132
##
@@ -226,4 +226,14 @@ def tomdoc?
226226
@format == 'tomdoc'
227227
end
228228

229+
##
230+
# Create a new parsed comment from a document
231+
232+
def self.from_document(document) # :nodoc:
233+
comment = RDoc::Comment.new('')
234+
comment.document = document
235+
comment.location = RDoc::TopLevel.new(document.file) if document.file
236+
comment
237+
end
238+
229239
end

lib/rdoc/parser/changelog.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ def scan
210210
grouped_entries = group_entries entries
211211

212212
doc = create_document grouped_entries
213-
214-
@top_level.comment = doc
213+
comment = RDoc::Comment.new(@content)
214+
comment.document = doc
215+
@top_level.comment = comment
215216

216217
@top_level
217218
end

lib/rdoc/ri/driver.rb

+16-15
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ def add_extension_modules_multiple out, store, modules # :nodoc:
510510
with.each do |incl|
511511
out << RDoc::Markup::Paragraph.new(incl.name)
512512
out << RDoc::Markup::BlankLine.new
513-
out << incl.comment
513+
out << incl.comment.parse
514514
end
515515

516516
unless wout.empty? then
@@ -534,7 +534,7 @@ def add_extension_modules_single out, store, include # :nodoc:
534534

535535
if include.comment then
536536
out << RDoc::Markup::BlankLine.new
537-
out << include.comment
537+
out << include.comment.parse
538538
end
539539
end
540540

@@ -651,12 +651,12 @@ def class_document name, found, klasses, includes, extends
651651
##
652652
# Adds the class +comment+ to +out+.
653653

654-
def class_document_comment out, comment # :nodoc:
655-
unless comment.empty? then
654+
def class_document_comment out, document # :nodoc:
655+
unless document.empty? then
656656
out << RDoc::Markup::Rule.new(1)
657657

658-
if comment.merged? then
659-
parts = comment.parts
658+
if document.merged? then
659+
parts = document.parts
660660
parts = parts.zip [RDoc::Markup::BlankLine.new] * parts.length
661661
parts.flatten!
662662
parts.pop
@@ -681,7 +681,7 @@ def class_document_constants out, klass # :nodoc:
681681
constants = klass.constants.sort_by { |constant| constant.name }
682682

683683
list.items.concat constants.map { |constant|
684-
parts = constant.comment.parts if constant.comment
684+
parts = constant.comment.parse.parts
685685
parts << RDoc::Markup::Paragraph.new('[not documented]') if
686686
parts.empty?
687687

@@ -898,7 +898,7 @@ def display_page name
898898

899899
page = store.load_page page_name
900900

901-
display page.comment
901+
display page.comment.parse
902902
end
903903

904904
##
@@ -1199,7 +1199,8 @@ def load_method store, cache, klass, type, name
11991199

12001200
store.load_method klass, "#{type}#{method}"
12011201
rescue RDoc::Store::MissingFileError => e
1202-
comment = RDoc::Comment.new("missing documentation at #{e.file}").parse
1202+
comment = RDoc::Comment.new("missing documentation at #{e.file}")
1203+
comment.parse
12031204

12041205
method = RDoc::AnyMethod.new nil, name
12051206
method.comment = comment
@@ -1361,21 +1362,21 @@ def parse_name name
13611362
# documentable items the class is added to +also_in+ instead.
13621363

13631364
def render_class out, store, klass, also_in # :nodoc:
1364-
comment = klass.comment
1365+
document = klass.comment.parse
13651366
# TODO the store's cache should always return an empty Array
13661367
class_methods = store.class_methods[klass.full_name] || []
13671368
instance_methods = store.instance_methods[klass.full_name] || []
13681369
attributes = store.attributes[klass.full_name] || []
13691370

1370-
if comment.empty? and
1371+
if document.empty? and
13711372
instance_methods.empty? and class_methods.empty? then
13721373
also_in << store
13731374
return
13741375
end
13751376

13761377
add_from out, store
13771378

1378-
class_document_comment out, comment
1379+
class_document_comment out, document
13791380

13801381
if class_methods or instance_methods or not klass.constants.empty? then
13811382
out << RDoc::Markup::Rule.new(1)
@@ -1423,16 +1424,16 @@ def render_method_comment out, method, alias_for = nil# :nodoc:
14231424
if alias_for
14241425
unless method.comment.nil? or method.comment.empty?
14251426
out << RDoc::Markup::BlankLine.new
1426-
out << method.comment
1427+
out << method.comment.parse
14271428
end
14281429
out << RDoc::Markup::BlankLine.new
14291430
out << RDoc::Markup::Paragraph.new("(This method is an alias for #{alias_for.full_name}.)")
14301431
out << RDoc::Markup::BlankLine.new
1431-
out << alias_for.comment
1432+
out << alias_for.comment.parse
14321433
out << RDoc::Markup::BlankLine.new
14331434
else
14341435
out << RDoc::Markup::BlankLine.new
1435-
out << method.comment
1436+
out << method.comment.parse
14361437
out << RDoc::Markup::BlankLine.new
14371438
end
14381439
end

0 commit comments

Comments
 (0)