Sha256: 37bb3623bfe202869d1b69c72e65bb4d102edac222a26bba7e97e67291a6ad3d

Contents?: true

Size: 922 Bytes

Versions: 3

Compression:

Stored size: 922 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop looks for trivial reader/writer methods, that could
      # have been created with the attr_* family of functions automatically.
      class TrivialAccessors < Cop
        MSG = 'Use attr_%s to define trivial %s methods.'

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

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

          super
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/style/trivial_accessors.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/trivial_accessors.rb
rubocop-0.9.0 lib/rubocop/cop/style/trivial_accessors.rb