Skip to content

Commit

Permalink
Support Ruby 3 inline attribute visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorton committed Mar 24, 2024
1 parent e833aac commit 4b9bcda
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/yard/handlers/ruby/visibility_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@ class YARD::Handlers::Ruby::VisibilityHandler < YARD::Handlers::Ruby::Base
case statement.type
when :var_ref, :vcall
self.visibility = ident.first.to_sym
when :fcall, :command
when :command
if RUBY_VERSION >= '3.' && is_attribute_method?(statement.parameters.first)
visibility_was = visibility
self.visibility = ident.first.to_sym
parse_block(statement.parameters.first, visibility: visibility)
self.visibility = visibility_was
return
end
process_decorator do |method|
method.visibility = ident.first if method.respond_to? :visibility=
end
when :fcall
process_decorator do |method|
method.visibility = ident.first if method.respond_to? :visibility=
end
end
end

def is_attribute_method?(node)
node.type == :command && node.jump(:ident).first.to_s =~ /^attr_(accessor|writer|reader)$/
end
end
7 changes: 7 additions & 0 deletions spec/handlers/examples/visibility_handler_002.rb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Testing
private attr_accessor :inline_private_attr
protected attr_writer :inline_protected_writer

# This one should be public
attr_reader :inline_public_reader
end
11 changes: 11 additions & 0 deletions spec/handlers/visibility_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@
it "can decorate a method definition" do
expect(Registry.at('Testing#decpriv').visibility).to eq :private
end unless LEGACY_PARSER

describe 'ruby 3 specific features' do
before(:all) { parse_file :visibility_handler_002, __FILE__ }

it "handles attr_accessor when inlined" do
expect(Registry.at('Testing#inline_private_attr').visibility).to eq :private
expect(Registry.at('Testing#inline_private_attr=').visibility).to eq :private
expect(Registry.at('Testing#inline_protected_writer=').visibility).to eq :protected
expect(Registry.at('Testing#inline_public_reader').visibility).to eq :public
end
end if RUBY_VERSION >= "3."
end

0 comments on commit 4b9bcda

Please sign in to comment.