Class: ImportGraph::Parser::RequireRelative

Inherits:
Object
  • Object
show all
Includes:
Util, RuboCop::AST
Defined in:
lib/import_graph/parser/require_relative.rb

Overview

Parser that tracks all the require_relative calls in the files

Constant Summary collapse

REQUIRE_RELATIVE_PATTERN =
NodePattern.new <<~PATTERN
  (send nil? :require_relative (str $_))
PATTERN

Instance Method Summary collapse

Methods included from Util

#build_match_object, #cleanup_absolute_path

Constructor Details

#initialize(file_trees) ⇒ RequireRelative

Returns a new instance of RequireRelative.



10
11
12
# File 'lib/import_graph/parser/require_relative.rb', line 10

def initialize(file_trees)
  @file_trees = file_trees
end

Instance Method Details

#parseObject



18
19
20
21
22
# File 'lib/import_graph/parser/require_relative.rb', line 18

def parse
  matches = Set.new
  @file_trees.keys.each { |file_path| matches.merge(parse_file(file_path)) }
  matches
end

#parse_file(file_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/import_graph/parser/require_relative.rb', line 24

def parse_file(file_path)
  matches = Set.new
  @file_trees[file_path].each_node do |node|
    match = REQUIRE_RELATIVE_PATTERN.match node
    next if match.nil?

    match_obj = { method: :require_relative, from: file_path, to: match }
    matches.add build_match_object(:require_relative, file_path, match) unless match.nil?
  end
  matches
end