AaronC81 / sord

Convert YARD docs to Sorbet RBI and Ruby 3/Steep RBS files
https://sord.aaronc.cc
MIT License
304 stars 18 forks source link

Typing a class instance variable #159

Open jaredcwhite opened 2 years ago

jaredcwhite commented 2 years ago

This may be a limitation of YARD tags, but I have a module that's trying to set class instance variables, and I'm not sure how I can get those typings to be output by Sord. The RBS file needs to have something like this:

  module ClassMethods
    @tag_name: String

but how to get that instance variable definition in there is the question…

grncdr commented 2 years ago

there is some precedent with Solargraphs @type tag:

module ClassMethods
  # @type [String]
  @tag_name
end

It would be pretty cool if sord were to pick that up.

AaronC81 commented 2 years ago

Thanks for the suggestion - this would be neat!

Unfortunately, I'm not sure that YARD is actually aware of instance variables :( Sord doesn't parse the tags or Ruby code itself; it just loads the YARD registry, so can only use what's there. YARD appears to know about class variables, but I can't spot anything about instance variables in the API docs, nor does it seem to generate any docs for them (unless I'm doing it wrong):

image

nogweii commented 1 year ago

A not-great workaround I've found is to use the @!attribute directive:

# @!attribute [r] http
#   @return [::HTTPX::Session]

But then that leads to the generated documentation mentioning it as, well, an attribute.

Would it make sense for sord to register a custom tag in YARD? Something like @itype name [String]?