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 44 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 28 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/rubocop/cop/style/stabby_lambda_parentheses.rb, line 102 def args?(node) args = node_args(node) args.children.count > 0 end
arrow_form?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 93 def arrow_form?(node) node.loc.selector.source == ARROW end
arrow_lambda_with_args?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 84 def arrow_lambda_with_args?(node) lambda_node?(node) && arrow_form?(node) && args?(node) end
lambda_node?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 88 def lambda_node?(node) receiver, call = *node receiver.nil? && call == :lambda end
missing_parentheses(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 54 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/rubocop/cop/style/stabby_lambda_parentheses.rb, line 66 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/rubocop/cop/style/stabby_lambda_parentheses.rb, line 97 def node_args(node) _call, args, _body = *node.parent args end
parentheses?(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 107 def parentheses?(node) args = node_args(node) args.loc.begin end
unwanted_parentheses(node)
click to toggle source
# File lib/rubocop/cop/style/stabby_lambda_parentheses.rb, line 60 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/rubocop/cop/style/stabby_lambda_parentheses.rb, line 75 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