Class: Orthoses::YARD::YARD2RBS

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/yard/yard2rbs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yardoc:, block:) ⇒ YARD2RBS

Returns a new instance of YARD2RBS.



25
26
27
28
29
30
31
32
33
# File 'lib/orthoses/yard/yard2rbs.rb', line 25

def initialize(yardoc:, block:)
  @yardoc = yardoc
  @block = block

  # statics
  @untyped = ::RBS::Types::Bases::Any.new(location: nil)
  @void = ::RBS::Types::Bases::Void.new(location: nil)
  @bool = ::RBS::Types::Bases::Bool.new(location: nil)
end

Instance Attribute Details

#blockProc (readonly)

Returns:

  • (Proc)


14
15
16
# File 'lib/orthoses/yard/yard2rbs.rb', line 14

def block
  @block
end

#boolRBS::Types::Bases::Bool (readonly)

Returns:

  • (RBS::Types::Bases::Bool)


23
24
25
# File 'lib/orthoses/yard/yard2rbs.rb', line 23

def bool
  @bool
end

#untypedRBS::Types::Bases::Any (readonly)

Returns:

  • (RBS::Types::Bases::Any)


17
18
19
# File 'lib/orthoses/yard/yard2rbs.rb', line 17

def untyped
  @untyped
end

#voidRBS::Types::Bases::Void (readonly)

Returns:

  • (RBS::Types::Bases::Void)


20
21
22
# File 'lib/orthoses/yard/yard2rbs.rb', line 20

def void
  @void
end

#yardocYARD::CodeObjects::t (readonly)

Returns:

  • (YARD::CodeObjects::t)


11
12
13
# File 'lib/orthoses/yard/yard2rbs.rb', line 11

def yardoc
  @yardoc
end

Class Method Details

.run(yardoc:, &block) ⇒ Object



5
6
7
# File 'lib/orthoses/yard/yard2rbs.rb', line 5

def run(yardoc:, &block)
  new(yardoc: yardoc, block: block).run
end

Instance Method Details

#runvoid

This method returns an undefined value.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/orthoses/yard/yard2rbs.rb', line 36

def run
  Orthoses.logger.info("YARD will generate about #{yardoc.inspect}")
  emit(yardoc.path, yardoc.docstring.all, nil, false)
  yardoc.children.each do |child|
    case child.type
    when :module, :class
      self.class.new(yardoc: child, block: block).run
    end
  end
  generate_for_attributes
  generate_for_methods
  generate_for_constants
  generate_for_classvariable
end

#tag_types_to_rbs_type(tag_types) ⇒ RBS::Types::t

Returns:

  • (RBS::Types::t)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/orthoses/yard/yard2rbs.rb', line 52

def tag_types_to_rbs_type(tag_types)
  return untyped if tag_types.nil?

  tag_types = tag_types.compact.uniq
  return untyped if tag_types.empty?

  begin
    types_explainers = ::YARD::Tags::TypesExplainer::Parser.parse(tag_types.join(", "))
  rescue SyntaxError => e
    Orthoses.logger.warn("#{tag_types} in #{yardoc.inspect} cannot parse as tags. use untyped instead")
    Orthoses.logger.warn("    => exception message=`#{e.message}`")
    return untyped
  end

  Utils::TypeList.new(recursive_resolve(types_explainers)).inject.tap do |rbs|
    Orthoses.logger.debug("#{yardoc.inspect} tag #{tag_types} => #{rbs}")
  end
end