|
| 1 | +# frozen_string_literal: true |
| 2 | +# Handles RBS attr_reader, attr_writer, and attr_accessor declarations. |
| 3 | +# |
| 4 | +# Registers one or two {YARD::CodeObjects::MethodObject} instances (reader |
| 5 | +# and/or writer) with @return / @param tags derived from the RBS type. |
| 6 | +class YARD::Handlers::RBS::AttributeHandler < YARD::Handlers::RBS::Base |
| 7 | + handles :attr_reader, :attr_writer, :attr_accessor |
| 8 | + |
| 9 | + process do |
| 10 | + attr_name = statement.name |
| 11 | + rbs_type = statement.attr_rbs_type |
| 12 | + yard_types = rbs_type ? YARD::Handlers::RBS::MethodHandler.rbs_type_to_yard_types(rbs_type) : nil |
| 13 | + mscope = statement.visibility == :class ? :class : :instance |
| 14 | + |
| 15 | + case statement.type |
| 16 | + when :attr_reader |
| 17 | + register_reader(attr_name, yard_types, mscope) |
| 18 | + when :attr_writer |
| 19 | + register_writer(attr_name, yard_types, mscope) |
| 20 | + when :attr_accessor |
| 21 | + register_reader(attr_name, yard_types, mscope) |
| 22 | + register_writer(attr_name, yard_types, mscope) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + private |
| 27 | + |
| 28 | + def register_reader(name, types, scope) |
| 29 | + obj = register MethodObject.new(namespace, name, scope) |
| 30 | + if types && !obj.has_tag?(:return) |
| 31 | + obj.add_tag YARD::Tags::Tag.new(:return, '', types) |
| 32 | + end |
| 33 | + obj |
| 34 | + end |
| 35 | + |
| 36 | + def register_writer(name, types, scope) |
| 37 | + obj = register MethodObject.new(namespace, "#{name}=", scope) |
| 38 | + if types && !obj.has_tag?(:param) |
| 39 | + obj.add_tag YARD::Tags::Tag.new(:param, '', types, "value") |
| 40 | + end |
| 41 | + obj |
| 42 | + end |
| 43 | +end |
0 commit comments