Class: RuboCop::Cop::IndexMethod::Autocorrection

Inherits:
Struct
  • Object
show all
Defined in:
lib/rubocop/cop/mixin/index_method.rb

Overview

Internal helper class to hold autocorrect data

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_each_with_object(node, match) ⇒ Object



120
121
122
# File 'lib/rubocop/cop/mixin/index_method.rb', line 120

def self.from_each_with_object(node, match)
  new(match, node, 0, 0)
end

.from_hash_brackets_map(node, match) ⇒ Object



140
141
142
# File 'lib/rubocop/cop/mixin/index_method.rb', line 140

def self.from_hash_brackets_map(node, match)
  new(match, node.children.last, 'Hash['.length, ']'.length)
end

.from_map_to_h(node, match) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rubocop/cop/mixin/index_method.rb', line 128

def self.from_map_to_h(node, match)
  strip_trailing_chars = 0

  unless node.parent&.block_type?
    map_range = node.children.first.source_range
    node_range = node.source_range
    strip_trailing_chars = node_range.end_pos - map_range.end_pos
  end

  new(match, node.children.first, 0, strip_trailing_chars)
end

.from_to_h(node, match) ⇒ Object



124
125
126
# File 'lib/rubocop/cop/mixin/index_method.rb', line 124

def self.from_to_h(node, match)
  new(match, node, 0, 0)
end

Instance Method Details

#set_new_arg_name(transformed_argname, corrector) ⇒ Object



159
160
161
162
163
164
# File 'lib/rubocop/cop/mixin/index_method.rb', line 159

def set_new_arg_name(transformed_argname, corrector)
  corrector.replace(
    block_node.arguments.loc.expression,
    "|#{transformed_argname}|"
  )
end

#set_new_body_expression(transforming_body_expr, corrector) ⇒ Object



166
167
168
169
170
171
# File 'lib/rubocop/cop/mixin/index_method.rb', line 166

def set_new_body_expression(transforming_body_expr, corrector)
  corrector.replace(
    block_node.body.loc.expression,
    transforming_body_expr.loc.expression.source
  )
end

#set_new_method_name(new_method_name, corrector) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/rubocop/cop/mixin/index_method.rb', line 150

def set_new_method_name(new_method_name, corrector)
  range = block_node.send_node.loc.selector
  if (send_end = block_node.send_node.loc.end)
    # If there are arguments (only true in the `each_with_object` case)
    range = range.begin.join(send_end)
  end
  corrector.replace(range, new_method_name)
end

#strip_prefix_and_suffix(node, corrector) ⇒ Object



144
145
146
147
148
# File 'lib/rubocop/cop/mixin/index_method.rb', line 144

def strip_prefix_and_suffix(node, corrector)
  expression = node.loc.expression
  corrector.remove_leading(expression, leading)
  corrector.remove_trailing(expression, trailing)
end