class RubbyCop::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/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 45 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/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 29 def on_send(node) return unless arrow_lambda_with_args?(node) if style == :require_parentheses if parentheses?(node) correct_style_detected else missing_parentheses(node) end elsif parentheses?(node) unwanted_parentheses(node) else correct_style_detected end end
Private Instance Methods
args?(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 103 def args?(node) !node_args(node).children.empty? end
arrow_form?(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 94 def arrow_form?(node) node.loc.selector.source == ARROW end
arrow_lambda_with_args?(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 85 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/style/stabby_lambda_parentheses.rb, line 89 def lambda_node?(node) receiver, call = *node !receiver && call == :lambda end
missing_parentheses(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 55 def missing_parentheses(node) add_offense(node_args(node), :expression, MSG_REQUIRE) do opposite_style_detected end end
missing_parentheses_corrector(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 67 def missing_parentheses_corrector(node) lambda do |corrector| args_loc = node_args(node).source_range corrector.insert_before(args_loc, '(') corrector.insert_after(args_loc, ')') end end
node_args(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 98 def node_args(node) _call, args, _body = *node.parent args end
parentheses?(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 107 def parentheses?(node) node_args(node).loc.begin end
unwanted_parentheses(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 61 def unwanted_parentheses(node) add_offense(node_args(node), :expression, MSG_NO_REQUIRE) do opposite_style_detected end end
unwanted_parentheses_corrector(node)
click to toggle source
# File lib/rubbycop/cop/style/stabby_lambda_parentheses.rb, line 76 def unwanted_parentheses_corrector(node) lambda do |corrector| args_loc = node_args(node).loc corrector.replace(args_loc.begin, '') corrector.remove(args_loc.end) end end