123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Lint::ToEnumArguments

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/lint/to_enum_arguments.rb

Overview

Ensures that to_enum/enum_for, called for the current method, has correct arguments.

Examples:

# bad
def foo(x, y = 1)
  return to_enum(__callee__, x) # `y` is missing
end

# good
def foo(x, y = 1)
  # Alternatives to `__callee__` are `__method__` and `:foo`.
  return to_enum(__callee__, x, y)
end

# good
def foo(x, y = 1)
  # It is also allowed if it is wrapped in some method like Sorbet.
  return to_enum(T.must(__callee__), x, y)
end

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

Class Attribute Summary

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

.builtin?

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Cops (other than builtin) are encouraged to implement this.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, #reset_investigation, #use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Method Details

#argument_match?(send_arg, def_arg) ⇒ Boolean (private)

Metrics/CyclomaticComplexity, Metrics/MethodLength

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 83

def argument_match?(send_arg, def_arg)
  def_arg_name = def_arg.children[0]

  case def_arg.type
  when :arg, :restarg
    send_arg.source == def_arg.source
  when :optarg
    send_arg.source == def_arg_name.to_s
  when :kwoptarg, :kwarg
    send_arg.hash_type? &&
      send_arg.pairs.any? { |pair| passing_keyword_arg?(pair, def_arg_name) }
  when :kwrestarg
    send_arg.each_child_node(:kwsplat, :forwarded_kwrestarg).any? do |child|
      child.source == def_arg.source
    end
  when :forward_arg
    send_arg.forwarded_args_type?
  end
end

#arguments_match?(arguments, def_node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 68

def arguments_match?(arguments, def_node)
  index = 0

  def_node.arguments.reject(&:blockarg_type?).all? do |def_arg|
    send_arg = arguments[index]
    case def_arg.type
    when :arg, :restarg, :optarg
      index += 1
    end

    send_arg && argument_match?(send_arg, def_arg)
  end
end

#enum_conversion_call?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 33

def_node_matcher :enum_conversion_call?, <<~PATTERN
  (send {nil? self} {:to_enum :enum_for} $_ $...)
PATTERN

#method_name?(node, name)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 38

def_node_matcher :method_name?, <<~PATTERN
  {(send nil? {:__method__ :__callee__}) (sym %1)}
PATTERN

#on_send(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 47

def on_send(node)
  def_node = node.each_ancestor(:def, :defs).first
  return unless def_node

  enum_conversion_call?(node) do |method_node, arguments|
    next if method_node.call_type? &&
            !method_node.method?(:__method__) && !method_node.method?(:__callee__)

    valid = if method_name?(method_node, def_node.method_name)
              arguments_match?(arguments, def_node)
            else
              def_node.arguments.empty?
            end
    return if valid

    add_offense(node)
  end
end

#passing_keyword_arg?(node, name)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/to_enum_arguments.rb', line 43

def_node_matcher :passing_keyword_arg?, <<~PATTERN
  (pair (sym %1) (lvar %1))
PATTERN