Skip to content

Commit ee577ea

Browse files
st0012vinistock
andauthored
Reduce unnecessary external state assignment (singleton) (#1312)
It really bugs me that RDoc has a tendency of assigning a state through a setter immediately after the object's initialization. This makes the dependencies unclear and code unnecessarily verbose. I'm taking a first step to fix the pattern by passing `singleton` as a constructor argument whenever possible. --------- Co-authored-by: Vinicius Stock <[email protected]>
1 parent f517b02 commit ee577ea

16 files changed

+42
-72
lines changed

lib/rdoc/code_object/alias.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class RDoc::Alias < RDoc::CodeObject
2323
##
2424
# Is this an alias declared in a singleton context?
2525

26-
attr_accessor :singleton
26+
attr_reader :singleton
2727

2828
##
2929
# Source file token stream
@@ -34,7 +34,7 @@ class RDoc::Alias < RDoc::CodeObject
3434
# Creates a new Alias with a token stream of +text+ that aliases +old_name+
3535
# to +new_name+, has +comment+ and is a +singleton+ context.
3636

37-
def initialize(text, old_name, new_name, comment, singleton = false)
37+
def initialize(text, old_name, new_name, comment, singleton: false)
3838
super()
3939

4040
@text = text

lib/rdoc/code_object/any_method.rb

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class RDoc::AnyMethod < RDoc::MethodAttr
3939
##
4040
# Creates a new AnyMethod with a token stream +text+ and +name+
4141

42-
def initialize text, name
43-
super
42+
def initialize(text, name, singleton: false)
43+
super(text, name, singleton: singleton)
4444

4545
@c_function = nil
4646
@dont_rename_initialize = false
@@ -53,10 +53,9 @@ def initialize text, name
5353
# Adds +an_alias+ as an alias for this method in +context+.
5454

5555
def add_alias an_alias, context = nil
56-
method = self.class.new an_alias.text, an_alias.new_name
56+
method = self.class.new an_alias.text, an_alias.new_name, singleton: singleton
5757

5858
method.record_location an_alias.file
59-
method.singleton = self.singleton
6059
method.params = self.params
6160
method.visibility = self.visibility
6261
method.comment = an_alias.comment
@@ -207,7 +206,7 @@ def marshal_load array
207206
@is_alias_for = array[15]
208207

209208
array[8].each do |new_name, document|
210-
add_alias RDoc::Alias.new(nil, @name, new_name, RDoc::Comment.from_document(document), @singleton)
209+
add_alias RDoc::Alias.new(nil, @name, new_name, RDoc::Comment.from_document(document), singleton: @singleton)
211210
end
212211

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

lib/rdoc/code_object/attr.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ class RDoc::Attr < RDoc::MethodAttr
2222
# Creates a new Attr with body +text+, +name+, read/write status +rw+ and
2323
# +comment+. +singleton+ marks this as a class attribute.
2424

25-
def initialize(text, name, rw, comment, singleton = false)
26-
super text, name
25+
def initialize(text, name, rw, comment, singleton: false)
26+
super(text, name, singleton: singleton)
2727

2828
@rw = rw
29-
@singleton = singleton
3029
self.comment = comment
3130
end
3231

@@ -44,9 +43,7 @@ def == other
4443
# Add +an_alias+ as an attribute in +context+.
4544

4645
def add_alias(an_alias, context)
47-
new_attr = self.class.new(self.text, an_alias.new_name, self.rw,
48-
self.comment, self.singleton)
49-
46+
new_attr = self.class.new(text, an_alias.new_name, rw, comment, singleton: singleton)
5047
new_attr.record_location an_alias.file
5148
new_attr.visibility = self.visibility
5249
new_attr.is_alias_for = self

lib/rdoc/code_object/class_module.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def marshal_load array # :nodoc:
392392
singleton ||= false
393393
visibility ||= :public
394394

395-
attr = RDoc::Attr.new nil, name, rw, nil, singleton
395+
attr = RDoc::Attr.new nil, name, rw, nil, singleton: singleton
396396

397397
add_attribute attr
398398
attr.visibility = visibility
@@ -419,8 +419,7 @@ def marshal_load array # :nodoc:
419419
@visibility = visibility
420420

421421
methods.each do |name, file|
422-
method = RDoc::AnyMethod.new nil, name
423-
method.singleton = true if type == 'class'
422+
method = RDoc::AnyMethod.new nil, name, singleton: type == 'class'
424423
method.record_location RDoc::TopLevel.new file
425424
add_method method
426425
end

lib/rdoc/code_object/method_attr.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class RDoc::MethodAttr < RDoc::CodeObject
6969
#
7070
# Usually this is called by super from a subclass.
7171

72-
def initialize text, name
72+
def initialize(text, name, singleton: false)
7373
super()
7474

7575
@text = text
@@ -78,7 +78,7 @@ def initialize text, name
7878
@aliases = []
7979
@is_alias_for = nil
8080
@parent_name = nil
81-
@singleton = nil
81+
@singleton = singleton
8282
@visibility = :public
8383
@see = false
8484

lib/rdoc/parser/c.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,7 @@ def do_aliases
248248
# method that reference the same function.
249249

250250
def add_alias(var_name, class_obj, old_name, new_name, comment)
251-
al = RDoc::Alias.new '', old_name, new_name, ''
252-
al.singleton = @singleton_classes.key? var_name
253-
al.comment = comment
251+
al = RDoc::Alias.new '', old_name, new_name, comment, singleton: @singleton_classes.key?(var_name)
254252
al.record_location @top_level
255253
class_obj.add_alias al
256254
@stats.add_alias al
@@ -1015,10 +1013,9 @@ def handle_method(type, var_name, meth_name, function, param_count,
10151013
type = 'method' # force public
10161014
end
10171015

1018-
meth_obj = RDoc::AnyMethod.new '', meth_name
1016+
singleton = singleton || %w[singleton_method module_function].include?(type)
1017+
meth_obj = RDoc::AnyMethod.new '', meth_name, singleton: singleton
10191018
meth_obj.c_function = function
1020-
meth_obj.singleton =
1021-
singleton || %w[singleton_method module_function].include?(type)
10221019

10231020
p_count = Integer(param_count) rescue -1
10241021

lib/rdoc/parser/prism_ruby.rb

+6-13
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,16 @@ def handle_meta_method_comment(comment, node)
289289

290290
if attributes
291291
attributes.each do |attr|
292-
a = RDoc::Attr.new(@container, attr, rw, processed_comment)
292+
a = RDoc::Attr.new(@container, attr, rw, processed_comment, singleton: @singleton)
293293
a.store = @store
294294
a.line = line_no
295-
a.singleton = @singleton
296295
record_location(a)
297296
@container.add_attribute(a)
298297
a.visibility = visibility
299298
end
300299
elsif line_no || node
301300
method_name ||= call_node_name_arguments(node).first if is_call_node
302-
meth = RDoc::AnyMethod.new(@container, method_name)
303-
meth.singleton = @singleton || singleton_method
301+
meth = RDoc::AnyMethod.new(@container, method_name, singleton: @singleton || singleton_method)
304302
handle_consecutive_comment_directive(meth, comment)
305303
comment.normalize
306304
meth.call_seq = comment.extract_call_seq
@@ -316,7 +314,6 @@ def handle_meta_method_comment(comment, node)
316314
meth,
317315
line_no: line_no,
318316
visibility: visibility,
319-
singleton: @singleton || singleton_method,
320317
params: '()',
321318
calls_super: false,
322319
block_params: nil,
@@ -452,8 +449,7 @@ def add_alias_method(old_name, new_name, line_no)
452449
comment = consecutive_comment(line_no)
453450
handle_consecutive_comment_directive(@container, comment)
454451
visibility = @container.find_method(old_name, @singleton)&.visibility || :public
455-
a = RDoc::Alias.new(nil, old_name, new_name, comment, @singleton)
456-
a.comment = comment
452+
a = RDoc::Alias.new(nil, old_name, new_name, comment, singleton: @singleton)
457453
handle_modifier_directive(a, line_no)
458454
a.store = @store
459455
a.line = line_no
@@ -472,10 +468,9 @@ def add_attributes(names, rw, line_no)
472468
return unless @container.document_children
473469

474470
names.each do |symbol|
475-
a = RDoc::Attr.new(nil, symbol.to_s, rw, comment)
471+
a = RDoc::Attr.new(nil, symbol.to_s, rw, comment, singleton: @singleton)
476472
a.store = @store
477473
a.line = line_no
478-
a.singleton = @singleton
479474
record_location(a)
480475
handle_modifier_directive(a, line_no)
481476
@container.add_attribute(a) if should_document?(a)
@@ -514,7 +509,7 @@ def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singl
514509
return if @in_proc_block
515510

516511
receiver = receiver_name ? find_or_create_module_path(receiver_name, receiver_fallback_type) : @container
517-
meth = RDoc::AnyMethod.new(nil, name)
512+
meth = RDoc::AnyMethod.new(nil, name, singleton: singleton)
518513
if (comment = consecutive_comment(start_line))
519514
handle_consecutive_comment_directive(@container, comment)
520515
handle_consecutive_comment_directive(meth, comment)
@@ -533,7 +528,6 @@ def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singl
533528
meth,
534529
line_no: start_line,
535530
visibility: visibility,
536-
singleton: singleton,
537531
params: params,
538532
calls_super: calls_super,
539533
block_params: block_params,
@@ -553,12 +547,11 @@ def add_method(name, receiver_name:, receiver_fallback_type:, visibility:, singl
553547
end
554548
end
555549

556-
private def internal_add_method(container, meth, line_no:, visibility:, singleton:, params:, calls_super:, block_params:, tokens:) # :nodoc:
550+
private def internal_add_method(container, meth, line_no:, visibility:, params:, calls_super:, block_params:, tokens:) # :nodoc:
557551
meth.name ||= meth.call_seq[/\A[^()\s]+/] if meth.call_seq
558552
meth.name ||= 'unknown'
559553
meth.store = @store
560554
meth.line = line_no
561-
meth.singleton = singleton
562555
container.add_method(meth) # should add after setting singleton and before setting visibility
563556
meth.visibility = visibility
564557
meth.params ||= params

lib/rdoc/parser/ruby.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def consume_trailing_spaces # :nodoc:
293293
# Creates a new attribute in +container+ with +name+.
294294

295295
def create_attr container, single, name, rw, comment # :nodoc:
296-
att = RDoc::Attr.new get_tkread, name, rw, comment, single == SINGLE
296+
att = RDoc::Attr.new get_tkread, name, rw, comment, singleton: single == SINGLE
297297
record_location att
298298

299299
container.add_attribute att
@@ -792,8 +792,7 @@ def parse_alias(context, single, tk, comment)
792792
return
793793
end
794794

795-
al = RDoc::Alias.new(get_tkread, old_name, new_name, comment,
796-
single == SINGLE)
795+
al = RDoc::Alias.new(get_tkread, old_name, new_name, comment, singleton: single == SINGLE)
797796
record_location al
798797
al.line = line_no
799798

@@ -1358,10 +1357,9 @@ def parse_meta_method(container, single, tk, comment)
13581357

13591358
return unless name
13601359

1361-
meth = RDoc::MetaMethod.new get_tkread, name
1360+
meth = RDoc::MetaMethod.new get_tkread, name, singleton: singleton
13621361
record_location meth
13631362
meth.line = line_no
1364-
meth.singleton = singleton
13651363

13661364
remove_token_listener self
13671365

@@ -1461,9 +1459,8 @@ def parse_method(container, single, tk, comment)
14611459

14621460
return unless name
14631461

1464-
meth = RDoc::AnyMethod.new get_tkread, name
1462+
meth = RDoc::AnyMethod.new get_tkread, name, singleton: single == SINGLE ? true : singleton
14651463
look_for_directives_in meth, comment
1466-
meth.singleton = single == SINGLE ? true : singleton
14671464
if singleton
14681465
# `current_line_visibility' is useless because it works against
14691466
# the normal method named as same as the singleton method, after

test/rdoc/test_rdoc_any_method.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_marshal_dump
236236
assert_equal 'Klass#method', loaded.full_name
237237
assert_equal 'method', loaded.name
238238
assert_equal 'param', loaded.params
239-
assert_nil loaded.singleton # defaults to nil
239+
assert_equal false, loaded.singleton
240240
assert_equal :public, loaded.visibility
241241
assert_equal cm, loaded.parent
242242
assert_equal section, loaded.section

test/rdoc/test_rdoc_class_module.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_marshal_dump
171171

172172
a1 = RDoc::Attr.new nil, 'a1', 'RW', ''
173173
a1.record_location tl
174-
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true
174+
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', singleton: true
175175
a2.record_location tl
176176

177177
m1 = RDoc::AnyMethod.new nil, 'm1'
@@ -358,7 +358,7 @@ def test_marshal_load_version_1
358358

359359
a1 = RDoc::Attr.new nil, 'a1', 'RW', ''
360360
a1.record_location tl
361-
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true
361+
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', singleton: true
362362
a2.record_location tl
363363

364364
m1 = RDoc::AnyMethod.new nil, 'm1'
@@ -437,7 +437,7 @@ def test_marshal_load_version_2
437437

438438
a1 = RDoc::Attr.new nil, 'a1', 'RW', ''
439439
a1.record_location tl
440-
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true
440+
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', singleton: true
441441
a2.record_location tl
442442

443443
m1 = RDoc::AnyMethod.new nil, 'm1'
@@ -522,7 +522,7 @@ def test_marshal_load_version_3
522522

523523
a1 = RDoc::Attr.new nil, 'a1', 'RW', ''
524524
a1.record_location tl
525-
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true
525+
a2 = RDoc::Attr.new nil, 'a2', 'RW', '', singleton: true
526526
a2.record_location tl
527527

528528
m1 = RDoc::AnyMethod.new nil, 'm1'
@@ -1583,14 +1583,14 @@ def setup
15831583
extmod_method = @extmod.add_method(RDoc::AnyMethod.new(nil, "extmod_method"))
15841584
extmod_method.section = @extmod.add_section("Extmod method section")
15851585

1586-
extmod_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_attr_without_a_section", "RW", "", true))
1587-
extmod_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_attr", "RW", "", true))
1586+
extmod_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_attr_without_a_section", "RW", "", singleton: true))
1587+
extmod_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_attr", "RW", "", singleton: true))
15881588
extmod_attr.section = @extmod.add_section("Extmod attr section")
15891589

15901590
extmod_private_method = @extmod.add_method(RDoc::AnyMethod.new(nil, "extmod_private_method"))
15911591
extmod_private_method.visibility = :private
15921592

1593-
extmod_private_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_private_attr", "RW", "", true))
1593+
extmod_private_attr = @extmod.add_attribute(RDoc::Attr.new(nil, "extmod_private_attr", "RW", "", singleton: true))
15941594
extmod_private_attr.visibility = :private
15951595

15961596
@klass.add_include(RDoc::Include.new("Incmod", nil))

test/rdoc/test_rdoc_context.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ def test_add_alias_method
8989
end
9090

9191
def test_add_alias_method_singleton
92-
meth = RDoc::AnyMethod.new nil, 'old_name'
93-
meth.singleton = true
92+
meth = RDoc::AnyMethod.new nil, 'old_name', singleton: true
9493

95-
as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment'
96-
as.singleton = true
94+
as = RDoc::Alias.new nil, 'old_name', 'new_name', 'comment', singleton: true
9795

9896
as.parent = @context
9997

test/rdoc/test_rdoc_cross_reference.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,9 @@ def assert_resolve_method(x)
176176
@c1.methods_hash.clear
177177

178178
i_op = RDoc::AnyMethod.new nil, x
179-
i_op.singleton = false
180179
@c1.add_method i_op
181180

182-
c_op = RDoc::AnyMethod.new nil, x
183-
c_op.singleton = true
181+
c_op = RDoc::AnyMethod.new nil, x, singleton: true
184182
@c1.add_method c_op
185183

186184
assert_ref i_op, x

test/rdoc/test_rdoc_markup_to_html_crossref.rb

-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ def test_convert_RDOCLINK_rdoc_ref_method_label
122122

123123
def test_convert_RDOCLINK_rdoc_ref_method_percent
124124
m = @c1.add_method RDoc::AnyMethod.new nil, '%'
125-
m.singleton = false
126125

127126
result = @to.convert 'rdoc-ref:C1#%'
128127

@@ -137,7 +136,6 @@ def test_convert_RDOCLINK_rdoc_ref_method_percent
137136

138137
def test_convert_RDOCLINK_rdoc_ref_method_escape_html
139138
m = @c1.add_method RDoc::AnyMethod.new nil, '<<'
140-
m.singleton = false
141139

142140
result = @to.convert 'rdoc-ref:C1#<<'
143141

@@ -151,7 +149,6 @@ def test_convert_RDOCLINK_rdoc_ref_method_escape_html
151149

152150
def test_convert_RDOCLINK_rdoc_ref_method_percent_label
153151
m = @c1.add_method RDoc::AnyMethod.new nil, '%'
154-
m.singleton = false
155152

156153
result = @to.convert 'rdoc-ref:C1#%@f'
157154

test/rdoc/test_rdoc_ri_driver.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,7 @@ def test_list_methods_matching_regexp
12141214
@cFoo.add_method index
12151215
@store1.save_method @cFoo, index
12161216

1217-
c_index = RDoc::AnyMethod.new nil, '[]'
1218-
c_index.singleton = true
1217+
c_index = RDoc::AnyMethod.new nil, '[]', singleton: true
12191218
c_index.record_location @top_level
12201219
@cFoo.add_method c_index
12211220
@store1.save_method @cFoo, c_index
@@ -1577,9 +1576,8 @@ def util_store
15771576
@bother.params = "(things)"
15781577
@bother.record_location @top_level
15791578

1580-
@new = @cFoo_Bar.add_method RDoc::AnyMethod.new nil, 'new'
1579+
@new = @cFoo_Bar.add_method RDoc::AnyMethod.new nil, 'new', singleton: true
15811580
@new.record_location @top_level
1582-
@new.singleton = true
15831581

15841582
@attr = @cFoo_Bar.add_attribute RDoc::Attr.new nil, 'attr', 'RW', ''
15851583
@attr.record_location @top_level

0 commit comments

Comments
 (0)