Class: Rubocop::Cop::Style::DefWithoutParentheses

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/def_parentheses.rb

Overview

This cop checks for missing parentheses in the definition of a method, that takes arguments. Both instance and class/singleton methods are checked.

Constant Summary collapse

MSG =
'Use def with parentheses when there are arguments.'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#autocorrect(node) ⇒ Object



63
64
65
66
67
68
# File 'lib/rubocop/cop/style/def_parentheses.rb', line 63

def autocorrect(node)
  @corrections << lambda do |corrector|
    corrector.insert_before(node.loc.expression, '(')
    corrector.insert_after(node.loc.expression, ')')
  end
end

#on_def(node) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rubocop/cop/style/def_parentheses.rb', line 47

def on_def(node)
  _, args = *node

  if args.children.size > 0 && args.loc.begin.nil?
    convention(args, :expression)
  end
end

#on_defs(node) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rubocop/cop/style/def_parentheses.rb', line 55

def on_defs(node)
  _, _, args = *node

  if args.children.size > 0 && args.loc.begin.nil?
    convention(args, :expression)
  end
end