Class: LoadTracer::StaticChecker

Inherits:
LoadTracer show all
Defined in:
lib/load_tracer/static_checker.rb

Constant Summary

Constants inherited from LoadTracer

LOAD_METHODS, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LoadTracer

#report, trace, #tracer

Constructor Details

#initialize(path) ⇒ StaticChecker

Returns a new instance of StaticChecker.



7
8
9
# File 'lib/load_tracer/static_checker.rb', line 7

def initialize(path)
  @path = path
end

Class Method Details

.parse_file(path) ⇒ Object



3
4
5
# File 'lib/load_tracer/static_checker.rb', line 3

def self.parse_file(path)
  new(path).trace
end

Instance Method Details

#trace(format: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/load_tracer/static_checker.rb', line 11

def trace(format: nil)
  @dependencies = Hash.new { |hash, key| hash[key] = [] }
  @reverse_dependencies = Hash.new { |hash, key| hash[key] = [] }
  @load_checked_features = Hash.new
  @not_found_features = []

  traverse(path: @path)

  report(format: format, dependencies: @dependencies, reverse_dependencies: @reverse_dependencies)
end

#traverse(path:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/load_tracer/static_checker.rb', line 22

def traverse(path:)
  return if @load_checked_features[path]
  return if File.extname(path) != '.rb'

  @load_checked_features[path] = true
  ast = RubyVM::AbstractSyntaxTree.parse_file(path)
  features = search_load_features(ast: ast)

  features.each do |feature|
    feature_path = find_path(feature) || find_path(File.expand_path(feature, File.dirname(path)))

    if feature_path.nil?
      @not_found_features << feature
      next
    end

    traverse(path: feature_path)

    @dependencies[path] << feature_path
    @reverse_dependencies[feature_path] << path
  end
end