Class: Rubocop::Cop::TrivialAccessors

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

Constant Summary collapse

MSG =
'Use attr_%s to define trivial %s methods.'

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?, #ignore_node, inherited, #initialize, #inspect, #name, #on_comment

Constructor Details

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

Instance Method Details

#on_def(node) ⇒ Object



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

def on_def(node)
  method_name, args, body = *node

  kind = if body.type == :ivar
           'reader'
         elsif args.children.size == 1 && body.type == :ivasgn &&
             body.children[1].type == :lvar && method_name != :initialize
           'writer'
         end
  if kind
    add_offence(:convention, node.loc.keyword.line,
                sprintf(MSG, kind, kind))
  end

  super
end