Class: Rubocop::Cop::DefWithParentheses

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

Constant Summary collapse

MSG =
"Omit the parentheses in defs when the method doesn't accept " +
'any arguments.'

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#on_def(node) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubocop/cop/def_parentheses.rb', line 9

def on_def(node)
  start_line = node.loc.keyword.line
  end_line = node.loc.end.line

  return if start_line == end_line

  _, args = *node
  if args.children == [] && args.loc.begin
    add_offence(:convention, node.loc.line, MSG)
  end

  super
end

#on_defs(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/def_parentheses.rb', line 23

def on_defs(node)
  start_line = node.loc.keyword.line
  end_line = node.loc.end.line

  return if start_line == end_line

  _, _, args = *node
  if args.children == [] && args.loc.begin
    add_offence(:convention, node.loc.line, MSG)
  end

  super
end