class RuboCop::Cop::Style::StabbyLambdaParentheses
Check for parentheses around stabby lambda arguments. There are two different styles. Defaults to `require_parentheses`.
@example
# require_parentheses - bad ->a,b,c { a + b + c } # require_parentheses - good ->(a,b,c) { a + b + c} # require_no_parentheses - bad ->(a,b,c) { a + b + c } # require_no_parentheses - good ->a,b,c { a + b + c}
Constants
- ARROW
- MSG_NO_REQUIRE
- MSG_REQUIRE
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 37 def autocorrect(node) if style == :require_parentheses missing_parentheses_corrector(node) elsif style == :require_no_parentheses unwanted_parentheses_corrector(node) end end
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 29 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
Private Instance Methods
arrow_form?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 81 def arrow_form?(node) node.loc.selector.source == ARROW end
message(_node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 55 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 47 def missing_parentheses?(node) style == :require_parentheses && !parentheses?(node) end
missing_parentheses_corrector(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 59 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
parentheses?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 85 def parentheses?(node) node.parent.arguments.loc.begin end
redundant_parentheses?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 51 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 77 def stabby_lambda_with_args?(node) node.command?(:lambda) && arrow_form?(node) && node.parent.arguments? end
unwanted_parentheses_corrector(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 68 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