Class: ImportGraph::Parser::Require

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Util

#build_match_object, #cleanup_absolute_path

Constructor Details

#initialize(file_trees) ⇒ Require

Returns a new instance of Require.



13
14
15
# File 'lib/import_graph/parser/require.rb', line 13

def initialize(file_trees)
  @file_trees = file_trees
end

Instance Method Details

#parseObject



17
18
19
20
21
# File 'lib/import_graph/parser/require.rb', line 17

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

#parse_file(file_path) ⇒ Object



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

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

    matches.add(build_match_object(:require, file_path, match)) unless match.nil?
  end
  matches
end