Sha256: 60b12bf5c0a2a2afc7c16508a76bb2f0175178baa51c6bec842e289429c0d89c

Contents?: true

Size: 1.75 KB

Versions: 49

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Naming
      # This cop makes sure that accessor methods are named properly. Applies
      # to both instance and class methods.
      #
      # NOTE: Offenses are only registered for methods with the expected
      # arity. Getters (`get_attribute`) must have no arguments to be
      # registered, and setters (`set_attribute(value)`) must have exactly
      # one.
      #
      # @example
      #   # bad
      #   def set_attribute(value)
      #   end
      #
      #   # good
      #   def attribute=(value)
      #   end
      #
      #   # bad
      #   def get_attribute
      #   end
      #
      #   # good
      #   def attribute
      #   end
      #
      #   # accepted, incorrect arity for getter
      #   def get_value(attr)
      #   end
      #
      #   # accepted, incorrect arity for setter
      #   def set_value
      #   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

49 entries across 49 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/naming/accessor_method_name.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.29.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.29.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.28.2 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.28.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.28.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.27.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.26.1 lib/rubocop/cop/naming/accessor_method_name.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.26.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.25.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.25.0 lib/rubocop/cop/naming/accessor_method_name.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.24.1 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.24.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.23.0 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.22.3 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.22.2 lib/rubocop/cop/naming/accessor_method_name.rb
rubocop-1.22.1 lib/rubocop/cop/naming/accessor_method_name.rb