Class: TestProf::RSpecStamp::Parser::Prism

Inherits:
Object
  • Object
show all
Defined in:
lib/test_prof/rspec_stamp/parser/prism.rb

Instance Method Summary collapse

Instance Method Details

#parse(code) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test_prof/rspec_stamp/parser/prism.rb', line 9

def parse(code)
  result = ::Prism.parse(code)
  return unless result.success?

  node = result.value.statements.body.first
  return unless node.is_a?(::Prism::CallNode)

  res = Result.new
  res.fname =
    if node.receiver
      "#{node.receiver.full_name}.#{node.name}"
    else
      node.name.name
    end

  args = node.arguments&.arguments
  return res if args.nil?

  rest =
    case (first = args.first).type
    when :string_node
      res.desc = first.content
      args[1..]
    when :constant_read_node, :constant_path_node
      res.desc_const = first.full_name
      args[1..]
    else
      args
    end

  rest.each do |arg|
    case arg.type
    when :symbol_node
      res.add_tag(arg.value.to_sym)
    when :keyword_hash_node
      arg.elements.each do |assoc|
        res.add_htag(
          assoc.key.value.to_sym,
          parse_htag_value(assoc.value)
        )
      end
    end
  end

  res
end