Module: YARDSorbet::SigToYARD

Extended by:
T::Sig
Defined in:
lib/yard-sorbet/sig_to_yard.rb

Overview

Translate sig type syntax to YARD type syntax.

Constant Summary collapse

REF_TYPES =

Map of common types to YARD conventions (in order to reduce allocations)

T.let({
  'T::Boolean' => ['Boolean'].freeze, # YARD convention for booleans
  # YARD convention is use singleton objects when applicable:
  # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Literals
  'FalseClass' => ['false'].freeze,
  'NilClass' => ['nil'].freeze,
  'TrueClass' => ['true'].freeze
}.freeze, T::Hash[String, [String]])

Class Method Summary collapse

Class Method Details

.build_generic_type(node) ⇒ String (private)

Parameters:

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

Returns:

  • (String)


55
56
57
58
59
60
61
# File 'lib/yard-sorbet/sig_to_yard.rb', line 55

private_class_method def self.build_generic_type(node)
  return node.source if node.empty? || node.type != :aref

  collection_type = node.first.source
  member_type = node.last.children.map { build_generic_type(_1) }.join(', ')
  "#{collection_type}[#{member_type}]"
end

.convert(node) ⇒ Array<String>

Parameters:

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

Returns:

  • (Array<String>)

See Also:



22
23
24
25
# File 'lib/yard-sorbet/sig_to_yard.rb', line 22

def self.convert(node)
  # scrub newlines, as they break the YARD parser
  convert_node(node).map { _1.gsub(/\n\s*/, ' ') }
end

.convert_aref(node) ⇒ Array<String> (private)

Parameters:

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

Returns:

  • (Array<String>)


64
65
66
67
68
69
70
71
72
73
# File 'lib/yard-sorbet/sig_to_yard.rb', line 64

private_class_method def self.convert_aref(node)
  # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Parametrized_Types
  case node.first.source
  when 'T::Array', 'T::Enumerable', 'T::Range', 'T::Set' then convert_collection(node)
  when 'T::Hash' then convert_hash(node)
  else
    log.info("Unsupported sig aref node #{node.source}")
    [build_generic_type(node)]
  end
end

.convert_array(node) ⇒ Array(String) (private)

Parameters:

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

Returns:

  • (Array(String))


76
77
78
79
80
81
# File 'lib/yard-sorbet/sig_to_yard.rb', line 76

private_class_method def self.convert_array(node)
  # https://www.rubydoc.info/gems/yard/file/docs/Tags.md#Order-Dependent_Lists
  member_types = node.first.children.map { convert_node(_1) }
  sequence = member_types.map { _1.size == 1 ? _1[0] : _1.to_s.tr('"', '') }.join(', ')
  ["Array(#{sequence})"]
end

.convert_call(node) ⇒ Array<String> (private)

Parameters:

  • node (YARD::Parser::Ruby::MethodCallNode)

Returns:

  • (Array<String>)


84
85
86
# File 'lib/yard-sorbet/sig_to_yard.rb', line 84

private_class_method def self.convert_call(node)
  node.namespace.source == 'T' ? convert_t_method(node) : [node.source]
end

.convert_collection(node) ⇒ Array(String) (private)

Parameters:

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

Returns:

  • (Array(String))


89
90
91
92
93
# File 'lib/yard-sorbet/sig_to_yard.rb', line 89

private_class_method def self.convert_collection(node)
  collection_type = node.first.source.split('::').last
  member_type = convert_node(node[-1][0]).join(', ')
  ["#{collection_type}<#{member_type}>"]
end

.convert_hash(node) ⇒ Array(String) (private)

Parameters:

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

Returns:

  • (Array(String))


96
97
98
99
100
101
# File 'lib/yard-sorbet/sig_to_yard.rb', line 96

private_class_method def self.convert_hash(node)
  kv = node.last.children
  key_type = convert_node(kv.first).join(', ')
  value_type = convert_node(kv.last).join(', ')
  ["Hash{#{key_type} => #{value_type}}"]
end

.convert_list(node) ⇒ Array<String> (private)

Parameters:

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

Returns:

  • (Array<String>)


104
105
106
# File 'lib/yard-sorbet/sig_to_yard.rb', line 104

private_class_method def self.convert_list(node)
  node.children.size == 1 ? convert_node(node.children.first) : [node.source]
end

.convert_node(node) ⇒ Array<String> (private)

Parameters:

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

Returns:

  • (Array<String>)


28
29
30
31
32
33
34
# File 'lib/yard-sorbet/sig_to_yard.rb', line 28

private_class_method def self.convert_node(node)
  case node
  when YARD::Parser::Ruby::MethodCallNode then convert_call(node)
  when YARD::Parser::Ruby::ReferenceNode then convert_ref(node.source)
  else convert_node_type(node)
  end
end

.convert_node_type(node) ⇒ Array<String> (private)

Parameters:

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

Returns:

  • (Array<String>)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yard-sorbet/sig_to_yard.rb', line 37

private_class_method def self.convert_node_type(node)
  case node.type
  when :aref then convert_aref(node)
  when :arg_paren then convert_node(node.first)
  when :array then convert_array(node)
  # Fixed hashes as return values are unsupported:
  # https://github.com/lsegal/yard/issues/425
  #
  # Hash key params can be individually documented with `@option`, but
  # sig translation is currently unsupported.
  when :hash then ['Hash']
  # seen when sig methods omit parentheses
  when :list then convert_list(node)
  else convert_unknown(node)
  end
end

.convert_ref(node_source) ⇒ Array(String) (private)

Parameters:

  • node_source (String)

Returns:

  • (Array(String))


109
110
111
# File 'lib/yard-sorbet/sig_to_yard.rb', line 109

private_class_method def self.convert_ref(node_source)
  REF_TYPES[node_source] || [node_source]
end

.convert_t_method(node) ⇒ Array<String> (private)

Parameters:

  • node (YARD::Parser::Ruby::MethodCallNode)

Returns:

  • (Array<String>)


114
115
116
117
118
119
120
121
122
# File 'lib/yard-sorbet/sig_to_yard.rb', line 114

private_class_method def self.convert_t_method(node)
  case node.method_name(true)
  # Order matters here, putting `nil` last results in a more concise return syntax in the UI (superscripted `?`):
  # https://github.com/lsegal/yard/blob/cfa62ae/lib/yard/templates/helpers/html_helper.rb#L499-L500
  when :nilable then convert_node(node.last) + REF_TYPES.fetch('NilClass')
  when :any then node[-1][0].children.flat_map { convert_node(_1) }
  else [node.source]
  end
end

.convert_unknown(node) ⇒ Array(String) (private)

Parameters:

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

Returns:

  • (Array(String))


125
126
127
128
# File 'lib/yard-sorbet/sig_to_yard.rb', line 125

private_class_method def self.convert_unknown(node)
  log.warn("Unsupported sig #{node.type} node #{node.source}")
  [node.source]
end