Sha256: 765a034977b60efd9294cb5fb794b05b5eaa119f363c89f7c8fc188e331dc38a

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 KB

Contents

# encoding: utf-8
# 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

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/accessor_method_name.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.43.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.42.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.41.2 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.41.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.41.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.40.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.39.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.38.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.37.2 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.37.1 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.37.0 lib/rubocop/cop/style/accessor_method_name.rb
rubocop-0.36.0 lib/rubocop/cop/style/accessor_method_name.rb