class RuboCop::Cop::Layout::SpaceAroundOperators

Checks that operators have space around them, except for ** which should not have surrounding space.

Constants

IRREGULAR_METHODS

Public Class Methods

autocorrect_incompatible_with() click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 12
def self.autocorrect_incompatible_with
  [Style::SelfAssignment]
end

Public Instance Methods

on_and(node)
Alias for: on_binary
on_and_asgn(node)
Alias for: on_binary
on_binary(node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 47
def on_binary(node)
  _, rhs, = *node

  return unless rhs

  check_operator(node.loc.operator, rhs.source_range)
end
on_casgn(node)
Alias for: on_special_asgn
on_class(node)
Alias for: on_binary
on_cvasgn(node)
Alias for: on_binary
on_gvasgn(node)
Alias for: on_binary
on_if(node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 24
def on_if(node)
  return unless node.ternary?

  check_operator(node.loc.question, node.if_branch.source_range)
  check_operator(node.loc.colon, node.else_branch.source_range)
end
on_ivasgn(node)
Alias for: on_binary
on_lvasgn(node)
Alias for: on_binary
on_masgn(node)
Alias for: on_binary
on_op_asgn(node)
Alias for: on_special_asgn
on_or(node)
Alias for: on_binary
on_or_asgn(node)
Alias for: on_binary
on_pair(node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 16
def on_pair(node)
  return unless node.hash_rocket?

  return if hash_table_style? && !node.parent.pairs_on_same_line?

  check_operator(node.loc.operator, node.source_range)
end
on_resbody(node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 31
def on_resbody(node)
  return unless node.loc.assoc

  _, variable, = *node

  check_operator(node.loc.assoc, variable.source_range)
end
on_send(node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 39
def on_send(node)
  if node.setter_method?
    on_special_asgn(node)
  elsif regular_operator?(node)
    check_operator(node.loc.selector, node.first_argument.source_range)
  end
end
on_special_asgn(node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 55
def on_special_asgn(node)
  _, _, right, = *node

  return unless right
  check_operator(node.loc.operator, right.source_range)
end
Also aliased as: on_casgn, on_op_asgn

Private Instance Methods

align_hash_cop_config() click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 134
def align_hash_cop_config
  config.for_cop('Layout/AlignHash')
end
autocorrect(range) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 122
def autocorrect(range)
  lambda do |corrector|
    if range.source =~ /\*\*/
      corrector.replace(range, '**')
    elsif range.source.end_with?("\n")
      corrector.replace(range, " #{range.source.strip}\n")
    else
      corrector.replace(range, " #{range.source.strip} ")
    end
  end
end
check_operator(op, right_operand) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 87
def check_operator(op, right_operand)
  with_space = range_with_surrounding_space(op)
  return if with_space.source.start_with?("\n")

  offense(op, with_space, right_operand) do |msg|
    add_offense(with_space, location: op, message: msg)
  end
end
excess_leading_space?(op, with_space) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 112
def excess_leading_space?(op, with_space)
  with_space.source =~ /^  / &&
    (!allow_for_alignment? || !aligned_with_operator?(op))
end
excess_trailing_space?(right_operand, with_space) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 117
def excess_trailing_space?(right_operand, with_space)
  with_space.source =~ /  $/ &&
    (!allow_for_alignment? || !aligned_with_something?(right_operand))
end
hash_table_style?() click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 138
def hash_table_style?
  align_hash_cop_config &&
    align_hash_cop_config['EnforcedHashRocketStyle'] == 'table'
end
offense(op, with_space, right_operand) { |msg| ... } click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 96
def offense(op, with_space, right_operand)
  msg = offense_message(op, with_space, right_operand)
  yield msg if msg
end
offense_message(op, with_space, right_operand) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 101
def offense_message(op, with_space, right_operand)
  if op.is?('**')
    'Space around operator `**` detected.' unless with_space.is?('**')
  elsif with_space.source !~ /^\s.*\s$/
    "Surrounding space missing for operator `#{op.source}`."
  elsif excess_leading_space?(op, with_space) ||
        excess_trailing_space?(right_operand, with_space)
    "Operator `#{op.source}` should be surrounded by a single space."
  end
end
operator_with_regular_syntax?(send_node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 82
def operator_with_regular_syntax?(send_node)
  send_node.operator_method? &&
    !IRREGULAR_METHODS.include?(send_node.method_name)
end
regular_operator?(send_node) click to toggle source
# File lib/rubocop/cop/layout/space_around_operators.rb, line 77
def regular_operator?(send_node)
  !send_node.unary_operation? && !send_node.dot? &&
    operator_with_regular_syntax?(send_node)
end