Class: RuboCop::Cop::RBS::Style::EmptyArgument::MethodTypeChecker

Inherits:
Object
  • Object
show all
Includes:
RBS::OnTypeHelper
Defined in:
lib/rubocop/cop/rbs/style/empty_argument.rb

Instance Method Summary collapse

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Constructor Details

#initialize(base_type: nil, &block) ⇒ MethodTypeChecker

Returns a new instance of MethodTypeChecker.



26
27
28
29
30
31
# File 'lib/rubocop/cop/rbs/style/empty_argument.rb', line 26

def initialize(base_type: nil, &block)
  @base_type = base_type
  @base = base_type.location.start_pos
  @tokens = tokenize(base_type.location.source)
  @block = block
end

Instance Method Details

#checkObject



33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rbs/style/empty_argument.rb', line 33

def check
  check_method_argument
  if @base_type.block
    check_block_argument
  end
  @base_type.each_type do |type|
    check_type(type)
  end
end

#check_block_argumentObject

{ () [self: instance] -> void } -> void



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/cop/rbs/style/empty_argument.rb', line 65

def check_block_argument
  return unless @base_type.block
  return unless @base_type.block.type.each_param.first.nil?

  if @base_type.block.self_type
    self_type_index = bsearch_token_index(@base_type.block.self_type.location.start_pos)
    # ) [self:
    # ^ ^^   ^ => pRPAREN, pLBRACKET, kSELF, pCOLON
    rparen = @tokens[self_type_index - 4]
    after_rparen = @tokens[self_type_index - 3]
  else
    block_arrow_index = @tokens.find_index { |t| t.type == :pARROW } or raise
    rparen = @tokens[block_arrow_index - 1]
    after_rparen = @tokens[block_arrow_index]
  end

  if rparen.type != :pRPAREN
    @block.call(
      @base + after_rparen.location.start_pos,
      @base + after_rparen.location.start_pos + 1
    )
  end
end

#check_method_argumentObject

T

() -> void



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/rbs/style/empty_argument.rb', line 44

def check_method_argument
  if @base_type.type_params.empty?
    will_lparen_token = @tokens[0]
  else
    rbracket_index = @tokens.index do |token|
      token.location.start_pos + @base >= @base_type.type_params.last.location.end_pos
    end or raise
    raise unless @tokens[rbracket_index].type == :pRBRACKET

    will_lparen_token = @tokens[rbracket_index + 1]
  end

  if will_lparen_token.type != :pLPAREN
    @block.call(
      @base + will_lparen_token.location.start_pos,
      @base + will_lparen_token.location.start_pos + 1
    )
  end
end

#check_proc(type) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rubocop/cop/rbs/style/empty_argument.rb', line 95

def check_proc(type)
  proc_start_index = bsearch_token_index(type.location.start_pos)
  proc_end_index = bsearch_token_index(type.location.end_pos)
  if @tokens[proc_start_index + 1].type != :pLPAREN
    @block.call(
      @base + @tokens[proc_start_index + 1].location.start_pos,
      @base + @tokens[proc_start_index + 1].location.start_pos + 1
    )
  end

  if type.block
    if type.block&.self_type
      self_type_index = bsearch_token_index(type.block.self_type.location.start_pos)
      # ) [self:
      # ^ ^^   ^ => pRPAREN, pLBRACKET, kSELF, pCOLON
      rparen = @tokens[self_type_index - 4]
      after_rparen = @tokens[self_type_index - 3]
    else
      block_arrow_index = @tokens[proc_start_index...proc_end_index].find_index { |t|
        t.type == :pARROW
      } or raise
      block_arrow_index += proc_start_index
      rparen = @tokens[block_arrow_index - 1]
      after_rparen = @tokens[block_arrow_index]
    end

    if rparen.type != :pRPAREN
      @block.call(
        @base + after_rparen.location.start_pos,
        @base + after_rparen.location.start_pos + 1
      )
    end
  end
end

#check_type(type = @base_type) ⇒ Object



89
90
91
92
93
# File 'lib/rubocop/cop/rbs/style/empty_argument.rb', line 89

def check_type(type = @base_type)
  on_type([::RBS::Types::Proc], type) do |proc_type|
    check_proc(proc_type)
  end
end