Class: RuboCop::Cop::Style::MapCompactWithConditionalBlock

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/style/map_compact_with_conditional_block.rb

Overview

Prefer ‘select` or `reject` over `map { … }.compact`. This cop also handles `filter_map { … }`, similar to `map { … }.compact`.

Examples:


# bad
array.map { |e| some_condition? ? e : next }.compact

# bad
array.filter_map { |e| some_condition? ? e : next }

# bad
array.map do |e|
  if some_condition?
    e
  else
    next
  end
end.compact

# bad
array.map do |e|
  next if some_condition?

  e
end.compact

# bad
array.map do |e|
  e if some_condition?
end.compact

# good
array.select { |e| some_condition? }

# good
array.reject { |e| some_condition? }

Constant Summary collapse

MSG =
'Replace `%<current>s` with `%<method>s`.'
RESTRICT_ON_SEND =
%i[compact filter_map].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, 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, 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

Instance Method Details

#conditional_block(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubocop/cop/style/map_compact_with_conditional_block.rb', line 51

def_node_matcher :conditional_block, <<~RUBY
  (block
    (call _ {:map :filter_map})
    (args
      $(arg _))
    {
      (if $_ $(lvar _) {next nil?})
      (if $_ {next nil?} $(lvar _))
      (if $_ (next $(lvar _)) {next nil nil?})
      (if $_ {next nil nil?} (next $(lvar _)))
      (begin
        {
          (if $_ next nil?)
          (if $_ nil? next)
        }
        $(lvar _))
      (begin
        {
          (if $_ (next $(lvar _)) nil?)
          (if $_ nil? (next $(lvar _)))
        }
        (nil))
    })
RUBY

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubocop/cop/style/map_compact_with_conditional_block.rb', line 76

def on_send(node)
  map_candidate = node.children.first
  if (block_argument, condition, return_value = conditional_block(map_candidate))
    return unless node.method?(:compact) && node.arguments.empty?

    range = map_with_compact_range(node)
  elsif (block_argument, condition, return_value = conditional_block(node.parent))
    return unless node.method?(:filter_map)

    range = filter_map_range(node)
  else
    return
  end

  inspect(node, block_argument, condition, return_value, range)
end