Class: RaaP::Coverage::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/raap/coverage.rb

Instance Method Summary collapse

Constructor Details

#initialize(method_type, cov) ⇒ Writer

Returns a new instance of Writer.



6
7
8
9
10
# File 'lib/raap/coverage.rb', line 6

def initialize(method_type, cov)
  @method_type = method_type
  @cov = cov
  @cur = 0
end

Instance Method Details

#write(io) ⇒ Object



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
# File 'lib/raap/coverage.rb', line 12

def write(io)
  RaaP.logger.debug { "Coverage: #{@cov}" }
  ml = @method_type.location
  unless ml
    RaaP.logger.warn("No location information for `#{@method_type}`")
    return
  end
  if ml.key?(:keyword)
    # attr_{reader,writer,accessor}
    phantom_member = RBS.parse_member(ml.source)
    case phantom_member
    when ::RBS::AST::Members::Attribute
      unless phantom_member.location
        RaaP.logger.warn("No location information for `#{phantom_member}`")
        return
      end
      write_type(io, "return", phantom_member.type)
      io.write(slice(@cur, @cur...phantom_member.location.end_pos))
    else
      RaaP.logger.error("#{phantom_member.class} is not supported")
      return
    end
  else
    # def name: () -> type
    phantom_method_type = RBS.parse_method_type(ml.source)
    phantom_method_type.type.yield_self do |fun|
      case fun
      when ::RBS::Types::Function
        fun.required_positionals.each_with_index { |param, i| write_param(io, "req_#{i}", param) }
        fun.optional_positionals.each_with_index { |param, i| write_param(io, "opt_#{i}", param) }
        fun.rest_positionals&.yield_self         { |param| write_param(io, "rest", param) }
        fun.trailing_positionals.each_with_index { |param, i| write_param(io, "trail_#{i}", param) }
        fun.required_keywords.each               { |key, param| write_param(io, "keyreq_#{key}", param) }
        fun.optional_keywords.each               { |key, param| write_param(io, "key_#{key}", param) }
        fun.rest_keywords&.yield_self            { |param| write_param(io, "keyrest", param) }
        # when ::RBS::Types::UntypedFunction
      end
    end

    phantom_method_type.block&.yield_self do |b|
      b.type.each_param.with_index { |param, i| write_param(io, "block_param_#{i}", param) }
      write_type(io, "block_return", b.type.return_type)
    end
    write_type(io, "return", phantom_method_type.type.return_type)
    raise unless phantom_method_type.location

    io.write(slice(@cur, @cur...phantom_method_type.location.end_pos))
  end

  io.puts
end