Skip to content

Commit 7a297e8

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 3c67824 commit 7a297e8

23 files changed

+151
-198
lines changed

lib/rdoc/code_object.rb

Lines changed: 0 additions & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 14 additions & 11 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 12 additions & 65 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 1 deletion
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/generator/darkfish.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -780,20 +780,13 @@ def template_for file, page = true, klass = ERB
780780
template
781781
end
782782

783-
# Returns an excerpt of the content for usage in meta description tags
784-
def excerpt(content)
785-
text = case content
783+
# Returns an excerpt of the comment for usage in meta description tags
784+
def excerpt(comment)
785+
text = case comment
786786
when RDoc::Comment
787-
content.text
788-
when RDoc::Markup::Document
789-
# This case is for page files that are not markdown nor rdoc
790-
# We convert them to markdown for now as it's easier to extract the text
791-
formatter = RDoc::Markup::ToMarkdown.new
792-
formatter.start_accepting
793-
formatter.accept_document(content)
794-
formatter.end_accepting
787+
comment.text
795788
else
796-
content
789+
comment
797790
end
798791

799792
# Match from a capital letter to the first period, discarding any links, so

lib/rdoc/parser/changelog.rb

Lines changed: 3 additions & 2 deletions
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

0 commit comments

Comments
 (0)