Sha256: 64e75290661be1419722dd31bb8a40994e0c92bf7ab928945bf10af890d73ab8

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require 'rubocop'
require 'rubocop/ast/node'
require 'rubocop/cop/cop'

module RuboCop
  module Cop
    module Ditty
      # This cop enforces the use of `Service.method` instead of
      # `Service.instance.method`. Calling the singleton instance has been
      # deprecated for services.
      #
      # @example
      #   # bad
      #   Ditty::Services::Logger.instance.info 'This is a log message'
      #
      #   # good
      #   Ditty::Services::Logger.info 'This is a log message'
      class CallServicesDirectly < RuboCop::Cop::Cop
        MSG = 'Do not use `.instance` on services. Call the method directly instead'

        def_node_matcher :service_instance_call?, <<-PATTERN
          (send (const (const (const ... :Ditty) :Services) _) :instance)
        PATTERN

        def on_send(node)
          return unless service_instance_call?(node)

          add_offense(node)
        end

        def autocorrect(node)
          lambda do |corrector|
            internal = node.children.first.source
            corrector.replace(node.loc.expression, internal)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ditty-0.8.0 lib/rubocop/cop/ditty/call_services_directly.rb