class RuboCop::Cop::Layout::SpaceInLambdaLiteral
Checks for spaces between `->` and opening parameter parenthesis (`(`) 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
- MSG_REQUIRE_NO_SPACE
- MSG_REQUIRE_SPACE
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/layout/space_in_lambda_literal.rb, line 30 def on_send(node) return unless arrow_lambda_with_args?(node) if style == :require_space && !space_after_arrow?(node) lambda_node = range_of_offense(node) add_offense(lambda_node, message: MSG_REQUIRE_SPACE) do |corrector| corrector.insert_before(lambda_arguments(node), ' ') end elsif style == :require_no_space && space_after_arrow?(node) space = space_after_arrow(node) add_offense(space, message: MSG_REQUIRE_NO_SPACE) do |corrector| corrector.remove(space) end end end
Private Instance Methods
arrow_lambda_with_args?(node)
click to toggle source
# File lib/rubocop/cop/layout/space_in_lambda_literal.rb, line 50 def arrow_lambda_with_args?(node) node.lambda_literal? && node.parent.arguments? end
lambda_arguments(node)
click to toggle source
# File lib/rubocop/cop/layout/space_in_lambda_literal.rb, line 72 def lambda_arguments(node) node.parent.children[1] end
range_of_offense(node)
click to toggle source
# File lib/rubocop/cop/layout/space_in_lambda_literal.rb, line 65 def range_of_offense(node) range_between( node.parent.loc.expression.begin_pos, node.parent.arguments.loc.expression.end_pos ) end
space_after_arrow(lambda_node)
click to toggle source
# File lib/rubocop/cop/layout/space_in_lambda_literal.rb, line 58 def space_after_arrow(lambda_node) arrow = lambda_node.parent.children[0].source_range parentheses = lambda_node.parent.children[1].source_range arrow.end.join(parentheses.begin) end
space_after_arrow?(lambda_node)
click to toggle source
# File lib/rubocop/cop/layout/space_in_lambda_literal.rb, line 54 def space_after_arrow?(lambda_node) !space_after_arrow(lambda_node).empty? end