Skip to content

Commit

Permalink
Merge pull request lsegal#1541 from theodorton/inline-attribute-visib…
Browse files Browse the repository at this point in the history
…ility

Support Ruby 3 inline attribute visibility
  • Loading branch information
lsegal authored Aug 26, 2024
2 parents 6be7e13 + 6e31a81 commit d1bc910
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
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

0 comments on commit d1bc910

Please sign in to comment.