Class: RuboCop::Cop::DryAutoInject::DependencyOrder
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::DryAutoInject::DependencyOrder
- Extended by:
- AutoCorrector
- Includes:
- Mixin
- Defined in:
- lib/rubocop/cop/dry_auto_inject/dependency_order.rb
Overview
Enforces a configurable order for dry-auto_inject ‘Import` deps.
Non-aliased deps are emitted first, then aliased deps. Within each section, deps are grouped by matching the configured ‘Order` patterns and sorted alphabetically inside a group. Each pattern can be:
- `'*'` — catch-all (at most one; implicitly appended)
- `'prefix.*'` — prefix wildcard (matches `prefix` and `prefix.*`)
- `'/regex/flags'` — regex matched against the dep path
- otherwise — exact path match
If ‘Order` is empty or omits `’*‘`, a catch-all `’*‘` group is appended implicitly so unmatched deps still have a home.
Specific patterns take priority over the ‘*` catch-all regardless of their position in `Order`.
Constant Summary collapse
- MSG =
"Dependencies are not in the configured order."
Instance Method Summary collapse
-
#initialize(config = nil, options = nil) ⇒ DependencyOrder
constructor
A new instance of DependencyOrder.
- #on_send(node) ⇒ Object
Constructor Details
#initialize(config = nil, options = nil) ⇒ DependencyOrder
Returns a new instance of DependencyOrder.
49 50 51 52 |
# File 'lib/rubocop/cop/dry_auto_inject/dependency_order.rb', line 49 def initialize(config = nil, = nil) super @parsed_order = build_parsed_order end |
Instance Method Details
#on_send(node) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rubocop/cop/dry_auto_inject/dependency_order.rb', line 54 def on_send(node) return unless injector_call?(node) deps = parse_injector_deps(node) return if deps.nil? order = parsed_order current = deps[:non_aliased] + deps[:aliased] sorted = [deps[:non_aliased], deps[:aliased]].flat_map { |group| group_and_sort(group, order) { |d| d[:path] } } return if current == sorted add_offense(node, message: MSG) do |corrector| replace_injector_content(corrector, node, sorted) end end |