Class: TestProf::RSpecStamp::Parser::Ripper

Inherits:
Object
  • Object
show all
Defined in:
lib/test_prof/rspec_stamp/parser/ripper.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
55
56
57
58
59
60
61
62
63
# File 'lib/test_prof/rspec_stamp/parser/ripper.rb', line 9

def parse(code)
  sexp = ::Ripper.sexp(code)
  return unless sexp

  # sexp has the following format:
  # [:program,
  #   [
  #     [
  #       :command,
  #       [:@ident, "it", [1, 0]],
  #       [:args_add_block, [ ... ]]
  #     ]
  #   ]
  # ]
  #
  # or
  #
  # [:program,
  #   [
  #     [
  #       :vcall,
  #       [:@ident, "it", [1, 0]]
  #     ]
  #   ]
  # ]
  res = Result.new

  fcall = sexp[1][0][1]
  args_block = sexp[1][0][2]

  if fcall.first == :fcall
    fcall = fcall[1]
  elsif fcall.first == :var_ref
    res.fname = [parse_const(fcall), sexp[1][0][3][1]].join(".")
    args_block = sexp[1][0][4]
  end

  res.fname ||= fcall[1]

  return res if args_block.nil?

  args_block = args_block[1] if args_block.first == :arg_paren

  args = args_block[1]

  if args.first.first == :string_literal
    res.desc = parse_literal(args.shift)
  elsif args.first.first == :var_ref || args.first.first == :const_path_ref
    res.desc_const = parse_const(args.shift)
  end

  parse_arg(res, args.shift) until args.empty?

  res
end