Replies: 3 comments
-
I think supporting this would belong in a plugin since Sorbet is not yet a Ruby standard. Fortunately, here's a quick little YARD extension you can use to add support: # place in something like `yardext/sorbet.rb`
module YardSorbet
class SigHandler < YARD::Handlers::Ruby::Base
namespace_only
handles method_call
process do
case statement.jump(:ident).first
when "sig"
extra_state.sorbet_signature_docstring ||= ""
extra_state.sorbet_signature_docstring += statement.docstring
else
extra_state.sorbet_signature_docstring = nil
end
end
end
module SigStopHandler
def process
super
extra_state.sorbet_signature_docstring = nil
end
end
module SigDefHandler
def process
return unless extra_state.sorbet_signature_docstring
statement.docstring = [
extra_state.sorbet_signature_docstring,
statement.docstring
].join("\n")
super
end
end
YARD::Handlers::Ruby::ClassHandler.include SigStopHandler
YARD::Handlers::Ruby::ModuleHandler.include SigStopHandler
YARD::Handlers::Ruby::MethodHandler.include SigDefHandler
end Run with
Or add You could turn this into a gem if you are interested, say, A nice addition to this would be to parse out the sigs and add YARD param/return type decorations to docstrings-- fairly easy to do given the above base code. That would allow the # @param value the value to reverse
# @return the reversed string
sig { params(value: String).returns(String) }
def reverse(value); end But I will leave this as an exercise for the reader 😊. |
Beta Was this translation helpful? Give feedback.
-
Thank you! I'll give this a try when I can.
I've been doing almost exactly the opposite of this recently, where the |
Beta Was this translation helpful? Give feedback.
-
The yard-sorbet plugin supports this. |
Beta Was this translation helpful? Give feedback.
-
This motivation behind this is primarily introducing better support for using YARD documentation alongside Sorbet type signatures. These are specified by a
sig
call before the method's definition.Currently the YARD documentation must go in-between the
sig
and definition, which feels awkward.Steps to reproduce
Generate docs for these two snippets:
Actual Output
The first snippet generates the documentation fine. The second snippet generates no documentation for
example
and prints:Expected Output
Both snippets should generate documentation for
example
.Environment details:
ruby -v
):ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
yard -v
):yard 0.9.20
Sorbet typechecker 0.4.4254
I have read the Contributing Guide.
Beta Was this translation helpful? Give feedback.
All reactions