Class: RSpecLetAnalyzer::Visitors::PrismVisitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rspec_let_analyzer/visitors/prism_visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_nesting_depth, track_factories) ⇒ PrismVisitor

Returns a new instance of PrismVisitor.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 10

def initialize(max_nesting_depth, track_factories)
  @root_lets = 0
  @total_its = 0
  @redefinitions = 0
  @depth = 0
  @let_names_by_depth = {}
  @max_nesting_depth = max_nesting_depth
  @nesting_counts = Array.new(max_nesting_depth, 0) if max_nesting_depth
  @track_factories = track_factories
  @factory_usage = Hash.new(0) if track_factories
  @before_creates = 0
  @in_before_block = false
end

Instance Attribute Details

#before_createsObject (readonly)

Returns the value of attribute before_creates.



8
9
10
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 8

def before_creates
  @before_creates
end

#factory_usageObject (readonly)

Returns the value of attribute factory_usage.



8
9
10
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 8

def factory_usage
  @factory_usage
end

#nesting_countsObject (readonly)

Returns the value of attribute nesting_counts.



8
9
10
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 8

def nesting_counts
  @nesting_counts
end

#redefinitionsObject (readonly)

Returns the value of attribute redefinitions.



8
9
10
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 8

def redefinitions
  @redefinitions
end

#root_letsObject (readonly)

Returns the value of attribute root_lets.



8
9
10
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 8

def root_lets
  @root_lets
end

#total_itsObject (readonly)

Returns the value of attribute total_its.



8
9
10
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 8

def total_its
  @total_its
end

Instance Method Details

#visit_call_node(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rspec_let_analyzer/visitors/prism_visitor.rb', line 24

def visit_call_node(node)
  method_name = node.name

  case method_name
  when :describe, :context, :shared_examples, :shared_context
    @depth += 1
    super
    @depth -= 1
    # Clean up let names at this depth when exiting
    @let_names_by_depth.delete(@depth + 1)
  when :before
    # Check if this is before or before(:each)
    arg = node.arguments&.arguments&.first
    is_before_each = arg.nil? || (arg.is_a?(Prism::SymbolNode) && arg.unescaped == 'each')

    if is_before_each
      @in_before_block = true
      super
      @in_before_block = false
    else
      super
    end
  when :let, :let!
    # Get the let variable name
    let_name = extract_let_name(node)

    if let_name
      # Check if this let redefines one from an outer scope
      @redefinitions += 1 if redefinition?(let_name)

      # Track this let name at current depth
      @let_names_by_depth[@depth] ||= []
      @let_names_by_depth[@depth] << let_name
    end

    # Count by nesting depth
    if @depth == 1
      @root_lets += 1
    elsif @max_nesting_depth
      # Track nesting counts: depth 2 -> index 0, depth 3 -> index 1, etc.
      nesting_index = @depth - 2
      if nesting_index < @max_nesting_depth - 1
        @nesting_counts[nesting_index] += 1
      else
        # Everything at max depth or beyond goes into the last bucket
        @nesting_counts[@max_nesting_depth - 1] += 1
      end
    end
    super
  when :it, :specify
    @total_its += 1
    super
  when :create, :build, :build_stubbed
    # Track if create is inside a before block
    if @in_before_block && method_name == :create
      @before_creates += 1
    end

    # Track FactoryBot usage
    if @track_factories
      factory_name = extract_factory_name(node)
      @factory_usage[factory_name] += 1 if factory_name
    end
    super
  else
    super
  end
end