Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Ruby 3 inline attribute visibility #1541

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 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,22 @@ 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)
parse_block(statement.parameters.first, visibility: ident.first.to_sym)
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