Class: DepsGrapher::AstProcessor

Inherits:
Parser::AST::Processor
  • Object
show all
Includes:
Logging
Defined in:
lib/deps_grapher/ast_processor.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#error, #info, #verbose, #warn

Constructor Details

#initialize(file_path, graph, event_processors, advanced_const_resolver, ignore_errors) ⇒ AstProcessor

Returns a new instance of AstProcessor.



28
29
30
31
32
33
34
35
36
# File 'lib/deps_grapher/ast_processor.rb', line 28

def initialize(file_path, graph, event_processors, advanced_const_resolver, ignore_errors)
  super()
  @file_path = file_path
  @target = File.basename(file_path, ".*").camelize
  @graph = graph
  @event_processors = event_processors
  @advanced_const_resolver = advanced_const_resolver
  @ignore_errors = ignore_errors
end

Class Attribute Details

.depthObject



23
24
25
# File 'lib/deps_grapher/ast_processor.rb', line 23

def depth
  @depth ||= 0
end

Class Method Details

.event_processedObject



17
18
19
# File 'lib/deps_grapher/ast_processor.rb', line 17

def event_processed
  @event_processed ||= Set.new
end

.processedObject



13
14
15
# File 'lib/deps_grapher/ast_processor.rb', line 13

def processed
  @processed ||= Set.new
end

Instance Method Details

#callObject



38
39
40
41
42
43
44
45
46
# File 'lib/deps_grapher/ast_processor.rb', line 38

def call
  return if self.class.processed.include?(@file_path)

  log do
    source_buffer = Parser::Source::Buffer.new(@file_path)
    parser = Prism::Translation::Parser.new
    process parser.parse(source_buffer.read)
  end
end

#on_const(ast_node) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/deps_grapher/ast_processor.rb', line 67

def on_const(ast_node)
  const_name = extract_const_name ast_node

  process_recursively! const_name, ast_node do
    Event.add name: :const_found, const_name: _1, location: _2
  end

  super
end

#on_module(ast_node) ⇒ Object Also known as: on_class



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/deps_grapher/ast_processor.rb', line 48

def on_module(ast_node)
  const_node = ast_node.children[0]
  name = extract_const_name const_node

  @namespace_stack ||= []
  @namespace_stack.push name

  if name == @target
    fully_qualified_class_name = @namespace_stack.join "::"
    @current_node = Node.add fully_qualified_class_name, @file_path
    self.class.processed << @file_path
  end

  super

  @namespace_stack.pop
end

#on_send(ast_node) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/deps_grapher/ast_processor.rb', line 77

def on_send(ast_node)
  if @current_node
    receiver_node, method_name, = *ast_node

    const_name = call_advanced_const_resolver ast_node
    const_name ||= extract_const_name receiver_node

    process_recursively! const_name, receiver_node do
      Event.add name: :method_found, const_name: _1, location: _2, method: method_name
    end
  end

  super
end