class RuboCop::Cop::Style::StabbyLambdaParentheses
->a,b,c { a + b + c}
# good
->(a,b,c) { a + b + c }
# bad
@example EnforcedStyle: require_no_parentheses
->(a,b,c) { a + b + c}
# good
->a,b,c { a + b + c }
# bad
@example EnforcedStyle: require_parentheses (default)
There are two different styles. Defaults to ‘require_parentheses`.
Check for parentheses around stabby lambda arguments.
def autocorrect(node)
def autocorrect(node) if style == :require_parentheses missing_parentheses_corrector(node) elsif style == :require_no_parentheses unwanted_parentheses_corrector(node) end end
def message(_node)
def message(_node) style == :require_parentheses ? MSG_REQUIRE : MSG_NO_REQUIRE end
def missing_parentheses?(node)
def missing_parentheses?(node) style == :require_parentheses && !parentheses?(node) end
def missing_parentheses_corrector(node)
def missing_parentheses_corrector(node) lambda do |corrector| args_loc = node.parent.arguments.source_range corrector.insert_before(args_loc, '(') corrector.insert_after(args_loc, ')') end end
def on_send(node)
def on_send(node) return unless stabby_lambda_with_args?(node) return unless redundant_parentheses?(node) || missing_parentheses?(node) add_offense(node.parent.arguments) end
def parentheses?(node)
def parentheses?(node) node.parent.arguments.loc.begin end
def redundant_parentheses?(node)
def redundant_parentheses?(node) style == :require_no_parentheses && parentheses?(node) end
def stabby_lambda_with_args?(node)
def stabby_lambda_with_args?(node) node.stabby_lambda? && node.parent.arguments? end
def unwanted_parentheses_corrector(node)
def unwanted_parentheses_corrector(node) lambda do |corrector| args_loc = node.parent.arguments.loc corrector.replace(args_loc.begin, '') corrector.remove(args_loc.end) end end