Module: YARDSorbet::Handlers::StructClassHandler

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

Overview

Note:

this module is included in YARD::Handlers::Ruby::ClassHandler

Class-level handler that folds all const and prop declarations into the constructor documentation this needs to be injected as a module otherwise the default Class handler will overwrite documentation

Instance Method Summary collapse

Instance Method Details

#decorate_t_struct_init(object, props, docstring, directives) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • object (YARD::CodeObjects::MethodObject)
  • props (Array<TStructProp>)
  • docstring (YARD::Docstring)
  • directives (Array<String>)


50
51
52
53
54
55
56
57
# File 'lib/yard-sorbet/handlers/struct_class_handler.rb', line 50

def decorate_t_struct_init(object, props, docstring, directives)
  # Use kwarg style arguments, with optionals being marked with a default (unless an actual default was specified)
  object.parameters = to_object_parameters(props)
  # The "source" of our constructor is the field declarations
  object.source ||= props.map(&:source).join("\n")
  object.docstring = docstring
  Directives.add_directives(object.docstring, directives)
end

#processvoid

This method returns an undefined value.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yard-sorbet/handlers/struct_class_handler.rb', line 14

def process
  super
  return if extra_state.prop_docs.nil?

  # lookup the full YARD path for the current class
  class_ns = YARD::CodeObjects::ClassObject.new(namespace, statement[0].source.gsub(/\s/, ''))
  props = extra_state.prop_docs[class_ns]
  return if props.empty?

  process_t_struct_props(props, class_ns)
end

#process_t_struct_props(props, class_ns) ⇒ void (private)

This method returns an undefined value.

Create a virtual initialize method with all the prop/const arguments

Parameters:

  • props (Array<TStructProp>)
  • class_ns (YARD::CodeObjects::ClassObject)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yard-sorbet/handlers/struct_class_handler.rb', line 30

def process_t_struct_props(props, class_ns)
  # having the name :initialize & the scope :instance marks this as the constructor.
  object = YARD::CodeObjects::MethodObject.new(class_ns, :initialize, :instance)
  # There is a chance that there is a custom initializer, so make sure we steal the existing docstring
  # and source
  docstring, directives = Directives.extract_directives(object.docstring)
  object.tags.each { docstring.add_tag(_1) }
  props.each { TagUtils.upsert_tag(docstring, 'param', _1.types, _1.prop_name, _1.doc) }
  TagUtils.upsert_tag(docstring, 'return', TagUtils::VOID_RETURN_TYPE)
  decorate_t_struct_init(object, props, docstring, directives)
end

#to_object_parameters(props) ⇒ Array<Array<(String, [String, nil])>> (private)

Parameters:

Returns:

  • (Array<Array<(String, [String, nil])>>)


60
61
62
63
64
65
# File 'lib/yard-sorbet/handlers/struct_class_handler.rb', line 60

def to_object_parameters(props)
  props.map do |prop|
    default = prop.default || (prop.types.include?('nil') ? 'nil' : nil)
    ["#{prop.prop_name}:", default]
  end
end