Class: RuboCop::Cop::DryAutoInject::RedundantAlias

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Mixin
Defined in:
lib/rubocop/cop/dry_auto_inject/redundant_alias.rb

Overview

Flags dry-auto_inject imports whose hash key matches the last segment of the path, since dry-auto_inject already derives the dependency name from that segment.

Examples:

# bad
include Import[foo: 'some.path.foo']

# good
include Import['some.path.foo']

Constant Summary collapse

MSG =
"Redundant alias `%<alias_name>s:` — dependency key is derived from " \
"the last segment of `'%<path>s'`."

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/dry_auto_inject/redundant_alias.rb', line 26

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

  deps = parse_injector_deps(node)
  return if deps.nil?

  redundant = deps[:aliased].select { |d| d[:alias] == d[:path].split(".").last }
  return if redundant.empty?

  canonical = promote_redundant_aliases(deps)

  redundant.each_with_index do |dep, i|
    add_offense(
      dep[:node],
      message: format(MSG, alias_name: dep[:alias], path: dep[:path])
    ) do |corrector|
      next unless i.zero?

      replace_injector_content(corrector, node, canonical)
    end
  end
end