You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a strange behavior while parsing singleton methods within YARD
Steps to reproduce
Let's check for class methods in plain Ruby:
classTdefm1T.m2endprivatedefself.m2print123endclass << selfprivatedefm3;endendendT.private_methods.include?(:m2)#=> falseT.private_methods.include?(:m3)#=> trueT.m2#=> 123=> nilT.m3#=> (irb):19:in`<main>': private method `m3' called for T:Class (NoMethodError) from /home/$USER/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.6.4/exe/irb:9:in `<top (required)>'from/home/$USER/.rbenv/versions/3.2.2/bin/irb:25:in`load' from /home/$USER/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'
Look, method T.m3 is private and T.m2 -- not. There is additional notes from RuboCop report:
RuboCop: `private` (on line 6) does not make singleton methods private. Use `private_class_method` or `private` inside a `class << self` block instead. [Lint/IneffectiveAccessModifier]
Actual output
Here is output within YARD:
require'yard'code=<<~CODE class T def m1 T.m2 end private def self.m2 print 123 end class << self private def m3; end end endCODEdefdetect_private_methods(code)YARD.parse_string(code)YARD::Registry.all(:class).mapdo |class_obj|
class_obj.meths(inherited: false).mapdo |method_obj|
ifmethod_obj.scope == :class && method_obj.visibility == :privateputs"Method #{method_obj.name} is a #{method_obj.visibility}#{method_obj.scope} method!"endendendenddetect_private_methods(code)# Method m2 is a private class method!# Method m3 is a private class method!# => [[nil, nil, nil]]
Expected Output
In expected output only method T.m3 should be detected and nothing else:
require'yard'code=<<~CODE class T def m1 T.m2 end private def self.m2 print 123 end class << self private def m3; end end endCODEdefdetect_private_methods(code)YARD.parse_string(code)YARD::Registry.all(:class).mapdo |class_obj|
class_obj.meths(inherited: false).mapdo |method_obj|
ifmethod_obj.scope == :class && method_obj.visibility == :privateputs"Method #{method_obj.name} is a #{method_obj.visibility}#{method_obj.scope} method!"endendendenddetect_private_methods(code)# Method m3 is a private class method!# => [[nil, nil, nil]]
Notes
YARD correctly parses methods scope if we change private keyword to private_class_method:
require'yard'code=<<~CODE class T def m1 T.m2 end private_class_method def self.m2 print 123 end class << self private def m3; end end endCODEdefdetect_private_methods(code)YARD.parse_string(code)YARD::Registry.all(:class).mapdo |class_obj|
class_obj.meths(inherited: false).mapdo |method_obj|
ifmethod_obj.scope == :class && method_obj.visibility == :privateputs"Method #{method_obj.name} is a #{method_obj.visibility}#{method_obj.scope} method!"endendendenddetect_private_methods(code)# Method m2 is a private class method!# Method m3 is a private class method!# => [[nil, nil, nil]]
Environment details:
OS: Linux archlinux 6.3.1-arch1-1 #1 SMP PREEMPT_DYNAMIC Mon, 01 May 2023 17:42:39 +0000 x86_64 GNU/Linux
Ruby version (ruby -v): ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
There is a strange behavior while parsing singleton methods within YARD
Steps to reproduce
Let's check for class methods in plain Ruby:
Look, method
T.m3
is private andT.m2
-- not. There is additional notes from RuboCop report:Actual output
Here is output within YARD:
Expected Output
In expected output only method
T.m3
should be detected and nothing else:Notes
YARD correctly parses methods scope if we change
private
keyword toprivate_class_method
:Environment details:
Linux archlinux 6.3.1-arch1-1 #1 SMP PREEMPT_DYNAMIC Mon, 01 May 2023 17:42:39 +0000 x86_64 GNU/Linux
ruby -v
): ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]yard -v
): yard 0.9.34I have read the Contributing Guide.
The text was updated successfully, but these errors were encountered: