class RuboCop::Cop::Style::SpaceAroundOperators
which should not have surrounding space.
Checks that operators have space around them, except for **
def autocorrect(range)
def autocorrect(range) @corrections << lambda do |corrector| case range.source when /\*\*/ corrector.replace(range, '**') else corrector.replace(range, " #{range.source.strip} ") end end end
def called_with_dot?(node)
def called_with_dot?(node) node.loc.dot end
def check(node)
def check(node) check_operator(node.loc.operator) if node.loc.operator end
def check_operator(op)
def check_operator(op) with_space = range_with_surrounding_space(op) 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}'.") end end
def on_if(node)
def on_if(node) return unless node.loc.respond_to?(:question) check_operator(node.loc.question) check_operator(node.loc.colon) end
def on_pair(node)
def on_pair(node) check(node) if node.loc.operator.is?('=>') end
def on_resbody(node)
def on_resbody(node) check_operator(node.loc.assoc) if node.loc.assoc end
def on_send(node)
def on_send(node) if node.loc.operator check(node) elsif !unary_operation?(node) && !called_with_dot?(node) op = node.loc.selector check_operator(op) if operator?(op) end end
def operator?(range)
def operator?(range) range.source !~ /^\[|\w/ end
def unary_operation?(node)
def unary_operation?(node) whole, selector = node.loc.expression, node.loc.selector return unless selector operator?(selector) && whole.begin_pos == selector.begin_pos end