Sha256: e2fcb6065a8956899c9ac0b59b1602f052201741ba6df3967085c3acee0d7d9c

Contents?: true

Size: 1.23 KB

Versions: 60

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Naming
      # This cop makes sure that accessor methods are named properly.
      #
      # @example
      #   # bad
      #   def set_attribute(value)
      #   end
      #
      #   # good
      #   def attribute=(value)
      #   end
      #
      #   # bad
      #   def get_attribute
      #   end
      #
      #   # good
      #   def attribute
      #   end
      class AccessorMethodName < Cop
        MSG_READER = 'Do not prefix reader method names with `get_`.'
        MSG_WRITER = 'Do not prefix writer method names with `set_`.'

        def on_def(node)
          return unless bad_reader_name?(node) || bad_writer_name?(node)

          add_offense(node, location: :name)
        end
        alias on_defs on_def

        private

        def message(node)
          if bad_reader_name?(node)
            MSG_READER
          elsif bad_writer_name?(node)
            MSG_WRITER
          end
        end

        def bad_reader_name?(node)
          node.method_name.to_s.start_with?('get_') && !node.arguments?
        end

        def bad_writer_name?(node)
          node.method_name.to_s.start_with?('set_') && node.arguments.one?
        end
      end
    end
  end
end

Version data entries

60 entries across 41 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.88.0 lib/rubocop/cop/naming/accessor_method_name.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.87.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.87.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.86.0 lib/rubocop/cop/naming/accessor_method_name.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/naming/accessor_method_name.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/naming/accessor_method_name.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.85.1 lib/rubocop/cop/naming/accessor_method_name.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.85.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.84.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.83.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.82.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.81.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.80.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.80.0 lib/rubocop/cop/naming/accessor_method_name.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/naming/accessor_method_name.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/naming/accessor_method_name.rb