class RubbyCop::Cop::Layout::SpaceInLambdaLiteral

This cop checks for spaces between -> and opening parameter brace in lambda literals.

@example

EnforcedStyle: require_no_space (default)

  @bad
  a = -> (x, y) { x + y }

  @good
  a = ->(x, y) { x + y }

@example

EnforcedStyle: require_space

  @bad
  a = ->(x, y) { x + y }

  @good
  a = -> (x, y) { x + y }

Constants

ARROW
MSG_REQUIRE_NO_SPACE
MSG_REQUIRE_SPACE

Public Instance Methods

autocorrect(lambda_node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 46
def autocorrect(lambda_node)
  children = lambda_node.parent.children
  lambda do |corrector|
    if style == :require_space
      corrector.insert_before(children[1].source_range, ' ')
    else
      space_range = range_between(children[0].source_range.end_pos,
                                  children[1].source_range.begin_pos)
      corrector.remove(space_range)
    end
  end
end
on_send(node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 37
def on_send(node)
  return unless arrow_lambda_with_args?(node)
  if style == :require_space && !space_after_arrow?(node)
    add_offense(node, node.parent.loc.expression, MSG_REQUIRE_SPACE)
  elsif style == :require_no_space && space_after_arrow?(node)
    add_offense(node, node.parent.loc.expression, MSG_REQUIRE_NO_SPACE)
  end
end

Private Instance Methods

args?(lambda_node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 74
def args?(lambda_node)
  _call, args, _body = *lambda_node.parent
  !args.children.empty?
end
arrow_form?(lambda_node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 70
def arrow_form?(lambda_node)
  lambda_node.loc.selector.source == ARROW
end
arrow_lambda_with_args?(node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 61
def arrow_lambda_with_args?(node)
  lambda_node?(node) && arrow_form?(node) && args?(node)
end
lambda_node?(node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 65
def lambda_node?(node)
  receiver, call = *node
  receiver.nil? && call == :lambda
end
space_after_arrow?(lambda_node) click to toggle source
# File lib/rubbycop/cop/layout/space_in_lambda_literal.rb, line 79
def space_after_arrow?(lambda_node)
  arrow = lambda_node.parent.children[0]
  parentheses = lambda_node.parent.children[1]
  parentheses.source_range.begin_pos - arrow.source_range.end_pos > 0
end