Skip to content

Commit 80a146b

Browse files
tompngst0012
andauthored
Stop accepting Document objects as CodeObject#comment (#1210)
`CodeObject#comment` is normally String or Comment, but Marshal dump and load creates CodeObject with Document as a comment. So there are two types of CodeObject that shouldn't be mixed: - ParsedCodeObject: comment is a String or a Comment - MarshalLoadedCodeObject: comment is a Document This is implicitly doubling the number of total classes in RDoc. It is mixing huge complexity and potential bugs to RDoc codebase. Some method only accepts ParsedCodeObject. Some method only accepts MarshalLoadedCodeObject. It is difficult to know. ```ruby def add_extension_modules_single out, store, include # include should be RDoc::Markup::(MarshalLoaded)Include. Does not accept RDoc::Markup::(Parsed)Include ... out << RDoc::Markup::BlankLine.new out << include.comment end ``` It looks like document is assigned to comment to represent parse-result-cached comment. Alternatively, `Comment#document=` can be used to represent it. Ideally, we should avoid mixing String with Comment, but String it is too frequently used. I think it will be easy to remove mixing String after switching to PrismRuby Parser. I believe we should do this refactor someday but maybe not before Ruby 3.4 because the release date is pretty close. --------- Co-authored-by: Stan Lo <[email protected]>
1 parent 75d1511 commit 80a146b

23 files changed

+150
-202
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

+10-68
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,10 @@ 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
75-
else
76-
raise TypeError, "unknown comment type: #{comment.inspect}"
64+
comments = Array(comment)
65+
comments.each do |c|
66+
extracted_comment = extract_comment(c)
67+
@comments << extracted_comment unless extracted_comment.empty?
7768
end
7869
end
7970

@@ -97,10 +88,6 @@ def aref
9788

9889
def extract_comment comment
9990
case comment
100-
when Array then
101-
comment.map do |c|
102-
extract_comment c
103-
end
10491
when nil
10592
RDoc::Comment.new ''
10693
when RDoc::Comment then
@@ -115,8 +102,6 @@ def extract_comment comment
115102
end
116103
end
117104

118-
comment
119-
when RDoc::Markup::Document then
120105
comment
121106
else
122107
raise TypeError, "unknown comment #{comment.inspect}"
@@ -135,20 +120,7 @@ def hash # :nodoc:
135120
# The files comments in this section come from
136121

137122
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}"
151-
end
123+
@comments.map(&:file)
152124
end
153125

154126
##
@@ -170,34 +142,15 @@ def marshal_load array
170142
@parent = nil
171143

172144
@title = array[1]
173-
@comments = array[2]
145+
@comments = array[2].parts.map { |doc| RDoc::Comment.from_document(doc) }
174146
end
175147

176148
##
177149
# Parses +comment_location+ into an RDoc::Markup::Document composed of
178150
# multiple RDoc::Markup::Documents with their file set.
179151

180152
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
153+
RDoc::Markup::Document.new(*@comments.map(&:parse))
201154
end
202155

203156
##
@@ -213,20 +166,9 @@ def plain_html
213166
# Removes a comment from this section if it is from the same file as
214167
# +comment+
215168

216-
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}"
169+
def remove_comment target_comment
170+
@comments.delete_if do |stored_comment|
171+
stored_comment.file == target_comment.file
230172
end
231173
end
232174

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/generator/darkfish.rb

+5-12
Original file line numberDiff line numberDiff line change
@@ -783,20 +783,13 @@ def template_for file, page = true, klass = ERB
783783
template
784784
end
785785

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

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

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

0 commit comments

Comments
 (0)