Class: RuboCop::Cop::MagicNumbers::NoArgument

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/magic_numbers/no_argument.rb

Overview

Adds violations for magic numbers when used as the argument to a method

BAD: object.bottles_on_the_wall(100)

GOOD: object.bottles_on_the_wall(DEFAULT_BOTTLE_COUNT)

Constant Summary collapse

MAGIC_NUMBER_ARGUMENT_PATTERN =
<<-PATTERN
  (send
    {
      _
      _
      (%<illegal_scalar_pattern>s $_)
      | # This is a union of lhs and rhs literal
      (%<illegal_scalar_pattern>s $_)
      _
      _
    }
  )
PATTERN
ARGUMENT_MSG =
'Do not use magic number arguments to methods'
CONFIG_IGNORED_METHODS_NAME =
'IgnoredMethods'
DEFAULT_CONFIG =

By default, don’t raise an offense for magic numbers arguments for these methods

{
  CONFIG_IGNORED_METHODS_NAME => ['[]']
}.freeze

Constants inherited from Base

Base::CONFIG_ALL, Base::CONFIG_FLOAT, Base::CONFIG_INTEGER, Base::CONFIG_NAME_FORBIDDEN_NUMERICS, Base::ILLEGAL_SCALAR_TYPES

Instance Method Summary collapse

Instance Method Details

#cop_configObject



40
41
42
# File 'lib/rubocop/cop/magic_numbers/no_argument.rb', line 40

def cop_config
  DEFAULT_CONFIG.merge(super)
end

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



44
45
46
47
48
49
# File 'lib/rubocop/cop/magic_numbers/no_argument.rb', line 44

def on_message_send(node)
  return unless illegal_argument?(node)
  return if ignored_method?(node)

  add_offense(node, message: ARGUMENT_MSG)
end