Sha256: a22b78b3d4d2466bbb05c8742cc27a15869b6966fb2bcb4d2e25b7a33bc95774

Contents?: true

Size: 1.14 KB

Versions: 17

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop makes sure that accessor methods are named properly.
      #
      # @example
      #   # bad
      #   def set_attribute(value) ...
      #
      #   # good
      #   def attribute=(value)
      #
      #   # bad
      #   def get_attribute ...
      #
      #   # good
      #   def attribute ...
      class AccessorMethodName < Cop
        include OnMethodDef

        private

        def on_method_def(node, method_name, args, _body)
          if bad_reader_name?(method_name.to_s, args)
            add_offense(node, :name,
                        'Do not prefix reader method names with `get_`.')
          elsif bad_writer_name?(method_name.to_s, args)
            add_offense(node, :name,
                        'Do not prefix writer method names with `set_`.')
          end
        end

        def bad_reader_name?(method_name, args)
          method_name.start_with?('get_') && args.to_a.empty?
        end

        def bad_writer_name?(method_name, args)
          method_name.start_with?('set_') && args.to_a.one?
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.49.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.49.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.48.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.48.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.47.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.47.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.46.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.45.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.44.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.44.0 lib/rubocop/cop/style/accessor_method_name.rb