Class: RuboCop::Cop::Style::MapToHash

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/style/map_to_hash.rb

Overview

Looks for uses of ‘map.to_h` or `collect.to_h` that could be written with just `to_h` in Ruby >= 2.6.

NOTE: ‘Style/HashTransformKeys` and `Style/HashTransformValues` will also change this pattern if only hash keys or hash values are being transformed.

Examples:

# bad
something.map { |v| [v, v * 2] }.to_h

# good
something.to_h { |v| [v, v * 2] }

# bad
{foo: bar}.collect { |k, v| [k.to_s, v.do_something] }.to_h

# good
{foo: bar}.to_h { |k, v| [k.to_s, v.do_something] }

Constant Summary collapse

MSG =
'Pass a block to `to_h` instead of calling `%<method>s%<dot>sto_h`.'
RESTRICT_ON_SEND =
%i[to_h].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

minimum_target_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Class Method Details

.autocorrect_incompatible_withObject



48
49
50
# File 'lib/rubocop/cop/style/map_to_hash.rb', line 48

def self.autocorrect_incompatible_with
  [Layout::SingleLineBlockChain]
end

Instance Method Details

#map_to_h(node) ⇒ Object



41
42
43
44
45
46
# File 'lib/rubocop/cop/style/map_to_hash.rb', line 41

def_node_matcher :map_to_h, <<~PATTERN
  {
    $(call ({block numblock} $(call _ {:map :collect}) ...) :to_h)
    $(call $(call _ {:map :collect} (block_pass sym)) :to_h)
  }
PATTERN

#on_send(node) ⇒ Object Also known as: on_csend



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/style/map_to_hash.rb', line 52

def on_send(node)
  return unless (to_h_node, map_node = map_to_h(node))

  message = format(MSG, method: map_node.loc.selector.source, dot: to_h_node.loc.dot.source)
  add_offense(map_node.loc.selector, message: message) do |corrector|
    # If the `to_h` call already has a block, do not autocorrect.
    next if to_h_node.block_literal?

    autocorrect(corrector, to_h_node, map_node)
  end
end