Class: YardToRbsInline::Converter::CodeVisitor

Inherits:
Prism::Visitor
  • Object
show all
Includes:
Subscriptable
Defined in:
lib/yard_to_rbs_inline/converter/code_visitor.rb

Defined Under Namespace

Modules: TypeUtils

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Subscriptable

#capture_error_message, #capture_error_to_write_comment, #emit_error_message, #subscriber_to_prepend_error

Constructor Details

#initialize(source:, comments:) ⇒ CodeVisitor

: (source: Prism::Source, comments: Array) -> untyped



25
26
27
28
29
# File 'lib/yard_to_rbs_inline/converter/code_visitor.rb', line 25

def initialize(source:, comments:)
  @source = source
  @comments = comments
  @text_with_mod = TextWithMod.new(source.source)
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



19
20
21
# File 'lib/yard_to_rbs_inline/converter/code_visitor.rb', line 19

def comments
  @comments
end

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/yard_to_rbs_inline/converter/code_visitor.rb', line 16

def source
  @source
end

#text_with_modObject (readonly)

Returns the value of attribute text_with_mod.



22
23
24
# File 'lib/yard_to_rbs_inline/converter/code_visitor.rb', line 22

def text_with_mod
  @text_with_mod
end

Instance Method Details

#visit_call_node(node) ⇒ Object

: (Prism::CallNode) -> void



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yard_to_rbs_inline/converter/code_visitor.rb', line 32

def visit_call_node(node)
  parse_annotation = lambda do
    annotation_comments = annotations_for(node)
    annotation_strs = annotation_comments.map(&method(:source_for_comment))
    annotation_strs.map(&:chomp).join("\n")
  end

  case node.name.to_sym
  when :attr_reader
    annotation = parse_annotation.call

    unless annotation.match?(/^\s*#(:|\s*@rbs)/)
      docstring = YARD::DocstringParser.new.parse(annotation.gsub(/^\s*#\s*/, "")).to_docstring

      sig = capture_error_to_write_comment(node) { build_return_sig(docstring) }
      text_with_mod.mods << AppendLineContent.from_node_and_content(node, "#: #{sig}")
    end
  end

  super
end

#visit_def_node(node) ⇒ Object

: (Prism::DefNode) -> void



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/yard_to_rbs_inline/converter/code_visitor.rb', line 55

def visit_def_node(node)
  annotation_comments = annotations_for(node)
  annotation_strs = annotation_comments.map(&method(:source_for_comment))
  annotation = annotation_strs.map(&:chomp).join("\n")

  unless annotation.match?(/^\s*#(:|\s*@rbs)/)
    docstring = YARD::DocstringParser.new.parse(annotation.gsub(/^\s*#\s*/, "")).to_docstring

    sig = capture_error_to_write_comment(node) { build_signature(node.parameters, docstring) }

    text_with_mod.mods << PrependLine.from_node_and_content(node, "#: #{sig}")

    # STDERR.puts "#{node.name_loc.inspect} #{node.name}: #{sig}"
  end

  super
end