Sha256: b58e828b7c48b5612c1aac4541897f3be294069a989a6fb52c13638eecb932ae

Contents?: true

Size: 1.28 KB

Versions: 26

Compression:

Stored size: 1.28 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 < Base
        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)

          message = message(node)

          add_offense(node.loc.name, message: message)
        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

26 entries across 26 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/accessor_method_name.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/accessor_method_name.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/accessor_method_name.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/accessor_method_name.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/accessor_method_name.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.5.2 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.5.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.5.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.4.2 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.4.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.4.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.3.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.3.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.2.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.1.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.0.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.93.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.93.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-0.92.0 lib/rubocop/cop/naming/accessor_method_name.rb