Class: EagerEye::Detectors::DelegationNPlusOne

Inherits:
Base
  • Object
show all
Defined in:
lib/eager_eye/detectors/delegation_n_plus_one.rb

Constant Summary collapse

ITERATION_METHODS =
%i[
  each map collect select find_all reject filter filter_map flat_map
  find_each find_in_batches in_batches array!
].freeze
PRELOAD_METHODS =
%i[includes preload eager_load].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

default_severity

Class Method Details

.detector_nameObject



12
13
14
# File 'lib/eager_eye/detectors/delegation_n_plus_one.rb', line 12

def self.detector_name
  :delegation_n_plus_one
end

Instance Method Details

#detect(ast, file_path, delegation_maps = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eager_eye/detectors/delegation_n_plus_one.rb', line 16

def detect(ast, file_path, delegation_maps = {})
  return [] unless ast

  issues = []
  local_delegates = collect_local_delegates(ast)

  traverse_ast(ast) do |node|
    next unless iteration_block?(node)

    block_var = extract_block_variable(node)
    block_body = node.children[2]
    next unless block_var && block_body

    collection_node = node.children[0]
    delegates = build_delegates(infer_model_name(collection_node), delegation_maps, local_delegates)
    next if delegates.empty?

    included = extract_included_associations(collection_node)
    find_delegated_calls(block_body, block_var, delegates, included, file_path, issues)
  end

  issues
end