Class: RuboCop::Cop::Dry::UnusedImport

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/dry/unused_import.rb

Constant Summary collapse

MSG =
'Imported dependency `%<name>s` is not used in the class.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object

search for method calls



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/dry/unused_import.rb', line 16

def on_send(node)
  # search for method calls with specific pattern `include <included_const_name>[<...>]`
  possible_injector_include?(node) do |included_const_name|
    next if included_const_name.to_s != injector_name

    injector_call_node = node.arguments.first
    class_node = node.parent

    imported = extract_imported_methods(injector_call_node)
    break if imported.empty?

    unused = filter_unused_imported_methods(class_node, imported.dup)
    unused.each do |name, import_node|
      add_offense(import_node, message: format(MSG, name:))
    end
  end
end