class RuboCop::Cop::Style::StabbyLambdaParentheses

Check for parentheses around stabby lambda arguments. There are two different styles. Defaults to `require_parentheses`.

@example EnforcedStyle: require_parentheses (default)

# bad
->a,b,c { a + b + c }

# good
->(a,b,c) { a + b + c}

@example EnforcedStyle: require_no_parentheses

# bad
->(a,b,c) { a + b + c }

# good
->a,b,c { a + b + c}

Constants

MSG_NO_REQUIRE
MSG_REQUIRE

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 28
def on_send(node)
  return unless stabby_lambda_with_args?(node)
  return unless redundant_parentheses?(node) || missing_parentheses?(node)

  arguments = node.block_node.arguments

  add_offense(arguments) do |corrector|
    case style
    when :require_parentheses
      missing_parentheses_corrector(corrector, arguments)
    when :require_no_parentheses
      unwanted_parentheses_corrector(corrector, arguments)
    end
  end
end

Private Instance Methods

message(_node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 54
def message(_node)
  style == :require_parentheses ? MSG_REQUIRE : MSG_NO_REQUIRE
end
missing_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 46
def missing_parentheses?(node)
  style == :require_parentheses && !parentheses?(node)
end
missing_parentheses_corrector(corrector, node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 58
def missing_parentheses_corrector(corrector, node)
  corrector.wrap(node, '(', ')')
end
parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 73
def parentheses?(node)
  node.block_node.arguments.loc.begin
end
redundant_parentheses?(node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 50
def redundant_parentheses?(node)
  style == :require_no_parentheses && parentheses?(node)
end
stabby_lambda_with_args?(node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 69
def stabby_lambda_with_args?(node)
  node.lambda_literal? && node.block_node.arguments?
end
unwanted_parentheses_corrector(corrector, node) click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 62
def unwanted_parentheses_corrector(corrector, node)
  args_loc = node.loc

  corrector.replace(args_loc.begin, '')
  corrector.remove(args_loc.end)
end