Sha256: ead7876d24a8c601fb5e393a2870662fcd4485bfb39d1bd5584a979934c0c38a

Contents?: true

Size: 1.25 KB

Versions: 6791

Compression:

Stored size: 1.25 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_`.'.freeze
        MSG_WRITER = 'Do not prefix writer method names with `set_`.'.freeze

        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

6,791 entries across 6,785 versions & 25 rubygems

Version Path
ory-kratos-client-0.7.6.alpha3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
ory-kratos-client-0.7.6.alpha1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
ory-kratos-client-0.7.5.alpha2 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
mx-platform-ruby-0.4.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
mx-platform-ruby-0.3.2 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
mx-platform-ruby-0.3.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.3.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
mfk_openapi_ruby_client-1.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.2.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.1.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.0.3 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.0.2 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.0.1 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
passbase-1.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.68.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.68.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.67.2 lib/rubocop/cop/naming/accessor_method_name.rb