From 40204d2ff6add19dc1f3b8cdcde7533c59fc0358 Mon Sep 17 00:00:00 2001 From: Theodor Tonum Date: Sat, 23 Mar 2024 20:27:02 +0100 Subject: [PATCH] Support Ruby 3 inline attribute visibility See https://redmine.ruby-lang.org/issues/17314 --- lib/yard/handlers/ruby/visibility_handler.rb | 14 +++++++++++++- .../examples/visibility_handler_002.rb.txt | 7 +++++++ spec/handlers/visibility_handler_spec.rb | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 spec/handlers/examples/visibility_handler_002.rb.txt diff --git a/lib/yard/handlers/ruby/visibility_handler.rb b/lib/yard/handlers/ruby/visibility_handler.rb index 0d13d5127..0d2f5dd79 100644 --- a/lib/yard/handlers/ruby/visibility_handler.rb +++ b/lib/yard/handlers/ruby/visibility_handler.rb @@ -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 diff --git a/spec/handlers/examples/visibility_handler_002.rb.txt b/spec/handlers/examples/visibility_handler_002.rb.txt new file mode 100644 index 000000000..8a6627286 --- /dev/null +++ b/spec/handlers/examples/visibility_handler_002.rb.txt @@ -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 diff --git a/spec/handlers/visibility_handler_spec.rb b/spec/handlers/visibility_handler_spec.rb index bb1a3456a..ae202bea0 100644 --- a/spec/handlers/visibility_handler_spec.rb +++ b/spec/handlers/visibility_handler_spec.rb @@ -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