Class: YARDSorbet::Handlers::SigHandler

Inherits:
YARD::Handlers::Ruby::Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/yard-sorbet/handlers/sig_handler.rb

Overview

A YARD Handler for Sorbet type declarations

Constant Summary collapse

ATTR_NODE_TYPES =

These node types attached to sigs represent attr_* declarations

T.let(%i[command fcall].freeze, T::Array[Symbol])

Instance Method Summary collapse

Instance Method Details

#parse_params(method_node, node, docstring) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • method_node (YARD::Parser::Ruby::AstNode)
  • node (YARD::Parser::Ruby::AstNode)
  • docstring (YARD::Docstring)


52
53
54
55
56
57
58
59
60
61
# File 'lib/yard-sorbet/handlers/sig_handler.rb', line 52

def parse_params(method_node, node, docstring)
  return if ATTR_NODE_TYPES.include?(method_node.type)

  sibling = NodeUtils.sibling_node(node)
  sibling[0][0].each do |p|
    param_name = p[0][0]
    types = SigToYARD.convert(p.last)
    TagUtils.upsert_tag(docstring, 'param', types, param_name)
  end
end

#parse_return(node, docstring) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • node (YARD::Parser::Ruby::AstNode)
  • docstring (YARD::Docstring)


64
65
66
67
# File 'lib/yard-sorbet/handlers/sig_handler.rb', line 64

def parse_return(node, docstring)
  type = node.source == 'void' ? ['void'] : SigToYARD.convert(NodeUtils.sibling_node(node))
  TagUtils.upsert_tag(docstring, 'return', type)
end

#parse_sig(method_node, docstring) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • method_node (YARD::Parser::Ruby::AstNode)
  • docstring (YARD::Docstring)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yard-sorbet/handlers/sig_handler.rb', line 32

def parse_sig(method_node, docstring)
  NodeUtils.bfs_traverse(statement) do |n|
    case n.source
    when 'abstract'
      YARDSorbet::TagUtils.upsert_tag(docstring, 'abstract')
    when 'params'
      parse_params(method_node, n, docstring)
    when 'returns', 'void'
      parse_return(n, docstring)
    end
  end
end

#processvoid

This method returns an undefined value.

Swap the method definition docstring and the sig docstring. Parse relevant parts of the sig and include them as well.



20
21
22
23
24
25
26
27
# File 'lib/yard-sorbet/handlers/sig_handler.rb', line 20

def process
  method_node = NodeUtils.get_method_node(NodeUtils.sibling_node(statement))
  docstring, directives = Directives.extract_directives(statement.docstring)
  parse_sig(method_node, docstring)
  method_node.docstring = docstring.to_raw
  Directives.add_directives(method_node.docstring, directives)
  statement.docstring = nil
end