123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::HashTransformMethod::Autocorrection

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Struct
Instance Chain:
self, Struct
Inherits: Struct
  • Object
Defined in: lib/rubocop/cop/mixin/hash_transform_method.rb

Overview

Internal helper class to hold autocorrect data

Class Method Summary

Instance Attribute Summary

  • #block_node rw private

    Internal helper class to hold autocorrect data.

  • #leading rw private

    Internal helper class to hold autocorrect data.

  • #match rw private

    Internal helper class to hold autocorrect data.

  • #trailing rw private

    Internal helper class to hold autocorrect data.

Instance Method Summary

Class Method Details

.from_each_with_object(node, match)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 137

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

.from_hash_brackets_map(node, match)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 141

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

.from_map_to_h(node, match)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 145

def self.from_map_to_h(node, match)
  if node.parent&.block_type? && node.parent.send_node == node
    strip_trailing_chars = 0
  else
    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)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 157

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

Instance Attribute Details

#block_node (rw, private)

Internal helper class to hold autocorrect data

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 136

Autocorrection = Struct.new(:match, :block_node, :leading, :trailing)

#leading (rw, private)

Internal helper class to hold autocorrect data

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 136

Autocorrection = Struct.new(:match, :block_node, :leading, :trailing)

#match (rw, private)

Internal helper class to hold autocorrect data

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 136

Autocorrection = Struct.new(:match, :block_node, :leading, :trailing)

#trailing (rw, private)

Internal helper class to hold autocorrect data

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 136

Autocorrection = Struct.new(:match, :block_node, :leading, :trailing)

Instance Method Details

#set_new_arg_name(transformed_argname, corrector)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 177

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

#set_new_body_expression(transforming_body_expr, corrector)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 181

def set_new_body_expression(transforming_body_expr, corrector)
  body_source = transforming_body_expr.source
  if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
    body_source = "{ #{body_source} }"
  end

  corrector.replace(block_node.body, body_source)
end

#set_new_method_name(new_method_name, corrector)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 167

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)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/hash_transform_method.rb', line 161

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