Class: YARDSorbet::Handlers::StructPropHandler

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

Overview

Handles all const/prop calls, creating accessor methods, and compiles them for later usage at the class level in creating a constructor

Instance Method Summary collapse

Instance Method Details

#decorate_object(object, prop) ⇒ void (private)

This method returns an undefined value.

Add the source and docstring to the method object

Parameters:

  • object (YARD::CodeObjects::MethodObject)
  • prop (TStructProp)


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

def decorate_object(object, prop)
  object.source = prop.source
  # TODO: this should use `+` to delimit the prop name when markdown is disabled
  reader_docstring = prop.doc.empty? ? "Returns the value of prop `#{prop.prop_name}`." : prop.doc
  docstring = YARD::DocstringParser.new.parse(reader_docstring).to_docstring
  docstring.add_tag(YARD::Tags::Tag.new(:return, '', prop.types))
  object.docstring = docstring.to_raw
end

#immutable?Boolean (private)

Returns:

  • (Boolean)


38
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 38

def immutable? = statement.method_name(true) == :const || kw_arg('immutable:') == 'true'

#kw_arg(kwd) ⇒ String? (private)

Returns the value passed to the keyword argument, or nil.

Parameters:

  • kwd (String)

Returns:

  • (String, nil)

    the value passed to the keyword argument, or nil



42
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 42

def kw_arg(kwd) = params[2]&.find { _1[0].source == kwd }&.[](1)&.source

#make_prop(name) ⇒ TStructProp (private)

Parameters:

  • name (String)

Returns:



45
46
47
48
49
50
51
52
53
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 45

def make_prop(name)
  TStructProp.new(
    default: kw_arg('default:'),
    doc: statement.docstring.to_s,
    prop_name: name,
    source: statement.source,
    types: SigToYARD.convert(params.fetch(1))
  )
end

#paramsArray<YARD::Parser::Ruby::AstNode> (private)

Returns:

  • (Array<YARD::Parser::Ruby::AstNode>)


56
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 56

def params = @params ||= T.let(statement.parameters(false), T.nilable(T::Array[YARD::Parser::Ruby::AstNode]))

#processvoid

This method returns an undefined value.



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

def process
  name = params.dig(0, -1, -1).source
  prop = make_prop(name)
  update_state(prop)
  object = YARD::CodeObjects::MethodObject.new(namespace, name, scope)
  decorate_object(object, prop)
  register_attrs(object, name)
end

#register_attrs(object, name) ⇒ void (private)

This method returns an undefined value.

Register the field explicitly as an attribute.

Parameters:

  • object (YARD::CodeObjects::MethodObject)
  • name (String)


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

def register_attrs(object, name)
  write = immutable? ? nil : object
  # Create the virtual attribute in our current scope
  namespace.attributes[scope][name] ||= SymbolHash[read: object, write: write]
end

#update_state(prop) ⇒ void (private)

This method returns an undefined value.

Store the prop for use in the constructor definition

Parameters:



68
69
70
71
# File 'lib/yard-sorbet/handlers/struct_prop_handler.rb', line 68

def update_state(prop)
  extra_state.prop_docs ||= Hash.new { |h, k| h[k] = [] }
  extra_state.prop_docs[namespace] << prop
end