class RuboCop::Cop::Style::MethodDefParentheses
This cops checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 28 def autocorrect(node) lambda do |corrector| if node.args_type? # offense is registered on args node when parentheses are unwanted corrector.replace(node.loc.begin, ' ') corrector.remove(node.loc.end) else args_expr = args_node(node).source_range args_with_space = range_with_surrounding_space(args_expr, :left) just_space = Parser::Source::Range.new(args_expr.source_buffer, args_with_space.begin_pos, args_expr.begin_pos) corrector.replace(just_space, '(') corrector.insert_after(args_expr, ')') end end end
on_method_def(node, _method_name, args, _body)
click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 12 def on_method_def(node, _method_name, args, _body) if style == :require_parentheses || (style == :require_no_parentheses_except_multiline && args.multiline?) if arguments?(args) && !parentheses?(args) missing_parentheses(node, args) else correct_style_detected end elsif parentheses?(args) unwanted_parentheses(args) else correct_style_detected end end
Private Instance Methods
args_node(def_node)
click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 61 def args_node(def_node) if def_node.type == :def _method_name, args, _body = *def_node else _scope, _method_name, args, _body = *def_node end args end
arguments?(args)
click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 70 def arguments?(args) !args.children.empty? end
missing_parentheses(node, args)
click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 48 def missing_parentheses(node, args) add_offense(node, args.source_range, 'Use def with parentheses when there are parameters.') do unexpected_style_detected(:require_no_parentheses) end end
unwanted_parentheses(args)
click to toggle source
# File lib/rubocop/cop/style/method_def_parentheses.rb, line 55 def unwanted_parentheses(args) add_offense(args, :expression, 'Use def without parentheses.') do unexpected_style_detected(:require_parentheses) end end