Class: YARD::Handlers::RBS::MethodHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/handlers/rbs/method_handler.rb

Overview

Handles RBS method definitions (def name: signature).

Creates a CodeObjects::MethodObject for each declaration and infers @param, @return, @yield, and @yieldparam tags from the RBS type signature when those tags are absent from the docstring.

Constant Summary

Constants included from CodeObjects

CodeObjects::BUILTIN_ALL, CodeObjects::BUILTIN_CLASSES, CodeObjects::BUILTIN_EXCEPTIONS, CodeObjects::BUILTIN_EXCEPTIONS_HASH, CodeObjects::BUILTIN_MODULES, CodeObjects::CONSTANTMATCH, CodeObjects::CONSTANTSTART, CodeObjects::CSEP, CodeObjects::CSEPQ, CodeObjects::ISEP, CodeObjects::ISEPQ, CodeObjects::METHODMATCH, CodeObjects::METHODNAMEMATCH, CodeObjects::NAMESPACEMATCH, CodeObjects::NSEP, CodeObjects::NSEPQ

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CodeObjects::NamespaceMapper

#clear_separators, #default_separator, on_invalidate, #register_separator, #separators, #separators_for_type, #separators_match, #types_for_separator, #unregister_separator_by_type

Constructor Details

This class inherits a constructor from YARD::Handlers::Base

Class Method Details

.bracket_depth(str) ⇒ Object

Return the bracket depth of the full string (should be 0 for well-formed types).



317
318
319
320
321
322
323
324
325
326
# File 'lib/yard/handlers/rbs/method_handler.rb', line 317

def self.bracket_depth(str)
  depth = 0
  str.each_char do |c|
    case c
    when '(', '[', '{' then depth += 1
    when ')', ']', '}' then depth -= 1
    end
  end
  depth
end

.rbs_type_to_yard_types(rbs) ⇒ Array<String>

Convert an RBS type string to an array of YARD type strings.

Parameters:

  • rbs (String)

    e.g. “String | Integer”, “Array”, “bool”

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/yard/handlers/rbs/method_handler.rb', line 30

def self.rbs_type_to_yard_types(rbs)
  rbs = rbs.strip
  return ['void']    if rbs == 'void'
  return ['Boolean'] if rbs == 'bool'
  return ['Object']  if rbs == 'untyped'
  return ['nil']     if rbs == 'nil'

  # Strip outer parentheses: `(String | Integer)` → recurse on inner.
  if rbs.start_with?('(') && rbs.end_with?(')') && bracket_depth(rbs[1..-2]) == 0
    return rbs_type_to_yard_types(rbs[1..-2])
  end

  # `Type?` is shorthand for `Type | nil` when the ? is outermost.
  if rbs =~ /\A(.+)\?\z/ && bracket_depth($1) == 0
    return rbs_type_to_yard_types($1) + ['nil']
  end

  split_on_pipe(rbs).map { |t| t.strip }
end

.split_on_pipe(str) ⇒ Object

Split str on ‘|` that are not inside brackets.



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/yard/handlers/rbs/method_handler.rb', line 289

def self.split_on_pipe(str)
  depth = 0
  parts = []
  cur   = String.new('')
  str.each_char do |c|
    case c
    when '(', '[', '{'
      depth += 1
      cur << c
    when ')', ']', '}'
      depth -= 1
      cur << c
    when '|'
      if depth == 0
        parts << cur.strip
        cur = String.new('')
      else
        cur << c
      end
    else
      cur << c
    end
  end
  parts << cur.strip unless cur.strip.empty?
  parts
end

Instance Method Details

#processvoid

This method returns an undefined value.

Main processing callback



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yard/handlers/rbs/method_handler.rb', line 10

process do
  meth_scope = statement.visibility == :class ? :class : :instance
  obj = register MethodObject.new(namespace, statement.name, meth_scope)
  apply_signature_tags(obj, statement.signatures)

  # For initialize, ensure the return type is the class, not void.
  if statement.name == 'initialize'
    ret_tags = obj.tags(:return)
    if ret_tags.none? || (ret_tags.length == 1 && ret_tags.first.types == ['void'])
      obj.docstring.delete_tags(:return)
      obj.add_tag YARD::Tags::Tag.new(:return, "a new instance of #{namespace.name}",
                                      [namespace.name.to_s])
    end
  end
end