class RuboCop::Cop::Style::SpaceAroundOperators
Checks that operators have space around them, except for ** which should not have surrounding space.
Public Instance Methods
on_binary(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 51 def on_binary(node) _, right, = *node return if right.nil? check_operator(node.loc.operator, right.source_range) end
Also aliased as: on_or, on_and, on_lvasgn, on_masgn, on_ivasgn, on_cvasgn, on_gvasgn, on_class, on_or_asgn, on_and_asgn
on_if(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 24 def on_if(node) return unless ternary?(node) _, if_branch, else_branch = *node check_operator(node.loc.question, if_branch.source_range) check_operator(node.loc.colon, else_branch.source_range) end
on_pair(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 13 def on_pair(node) return unless node.loc.operator.is?('=>') align_hash_config = config.for_cop('Style/AlignHash') return if align_hash_config['EnforcedHashRocketStyle'] == 'table' && !any_pairs_on_the_same_line?(node.parent) _, right = *node check_operator(node.loc.operator, right.source_range) end
on_resbody(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 32 def on_resbody(node) if node.loc.assoc _, variable, = *node check_operator(node.loc.assoc, variable.source_range) end end
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 39 def on_send(node) if node.loc.operator # aref assignment, attribute assignment on_special_asgn(node) elsif !node.unary_operation? && !called_with_dot?(node) op = node.method_name if op != :[] && op != :! && op != :[]= && operator?(op) _, _, right, = *node check_operator(node.loc.selector, right.source_range) end end end
on_special_asgn(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 57 def on_special_asgn(node) return unless node.loc.operator _, _, right, = *node check_operator(node.loc.operator, right.source_range) end
Also aliased as: on_casgn, on_op_asgn
Private Instance Methods
autocorrect(range)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 113 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
called_with_dot?(node)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 78 def called_with_dot?(node) node.loc.dot end
check_operator(op, right_operand)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 82 def check_operator(op, right_operand) with_space = range_with_surrounding_space(op) return if with_space.source.start_with?("\n") if op.is?('**') unless with_space.is?('**') add_offense(with_space, op, 'Space around operator `**` detected.') end elsif with_space.source !~ /^\s.*\s$/ add_offense(with_space, op, 'Surrounding space missing for ' "operator `#{op.source}`.") elsif excess_leading_space?(op, with_space) add_offense(with_space, op, "Operator `#{op.source}` should be " 'surrounded by a single space.') elsif excess_trailing_space?(right_operand, with_space) add_offense(with_space, op, "Operator `#{op.source}` should be " 'surrounded by a single space.') end end
excess_leading_space?(op, with_space)
click to toggle source
# File lib/rubocop/cop/style/space_around_operators.rb, line 103 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/style/space_around_operators.rb, line 108 def excess_trailing_space?(right_operand, with_space) with_space.source =~ / $/ && (!allow_for_alignment? || !aligned_with_something?(right_operand)) end