Class: RubyLint::FileLoader

Inherits:
Iterator show all
Defined in:
lib/ruby-lint/file_loader.rb

Overview

FileLoader iterates over an AST and given a constant node will try to find the corresponding filepath using FileScanner.

Options

The following options must be set when creating an instance of this class:

  • :directories: the directories to scan for files.
  • :ignore_paths: a list of paths to ignore when scanning for files.

Instance Attribute Summary collapse

Attributes inherited from Iterator

#arity_cache, #arity_cache Hash containing the amount of arguments for

Instance Method Summary collapse

Methods inherited from Iterator

#execute_callback, #initialize, #iterate, #skip_child_nodes!

Constructor Details

This class inherits a constructor from RubyLint::Iterator

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments



27
28
29
# File 'lib/ruby-lint/file_loader.rb', line 27

def comments
  @comments
end

#file_scannerRubyLint::FileScanner (readonly)



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
# File 'lib/ruby-lint/file_loader.rb', line 26

class FileLoader < Iterator
  attr_reader :file_scanner, :parser, :nodes, :comments, :paths

  ##
  # Called after a new instance of this class is created.
  #
  def after_initialize
    @file_scanner = FileScanner.new(@directories, @ignore_paths)
    @parser       = Parser.new
    @nodes        = []
    @paths        = Set.new
  end

  ##
  # @param [RubyLint::AST::Node] node
  #
  def on_const(node)
    const_path = ConstantPath.new(node)

    files      = file_scanner.scan(const_path.to_s)
    last_name  = const_path.constant_segments.last.last

    paths << node.file

    files.each do |path|
      next if paths.include?(path)

      paths << path

      process_file(last_name, path)
    end
  end

  private

  ##
  # @param [String] constant_name
  # @param [String] path
  #
  def process_file(constant_name, path)
    code = File.read(path)

    return unless code.include?(constant_name)

    ast, comments = parser.parse(code, path)

    iterate(ast)

    nodes << [ast, comments]
  end
end

#nodesArray (readonly)

Returns A list of extra nodes (and their comments) a VM instance should process before processing the file being analyzed.

Returns:

  • (Array)

    A list of extra nodes (and their comments) a VM instance should process before processing the file being analyzed.



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
# File 'lib/ruby-lint/file_loader.rb', line 26

class FileLoader < Iterator
  attr_reader :file_scanner, :parser, :nodes, :comments, :paths

  ##
  # Called after a new instance of this class is created.
  #
  def after_initialize
    @file_scanner = FileScanner.new(@directories, @ignore_paths)
    @parser       = Parser.new
    @nodes        = []
    @paths        = Set.new
  end

  ##
  # @param [RubyLint::AST::Node] node
  #
  def on_const(node)
    const_path = ConstantPath.new(node)

    files      = file_scanner.scan(const_path.to_s)
    last_name  = const_path.constant_segments.last.last

    paths << node.file

    files.each do |path|
      next if paths.include?(path)

      paths << path

      process_file(last_name, path)
    end
  end

  private

  ##
  # @param [String] constant_name
  # @param [String] path
  #
  def process_file(constant_name, path)
    code = File.read(path)

    return unless code.include?(constant_name)

    ast, comments = parser.parse(code, path)

    iterate(ast)

    nodes << [ast, comments]
  end
end

#parserRubyLint::Parser (readonly)

Returns:



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
# File 'lib/ruby-lint/file_loader.rb', line 26

class FileLoader < Iterator
  attr_reader :file_scanner, :parser, :nodes, :comments, :paths

  ##
  # Called after a new instance of this class is created.
  #
  def after_initialize
    @file_scanner = FileScanner.new(@directories, @ignore_paths)
    @parser       = Parser.new
    @nodes        = []
    @paths        = Set.new
  end

  ##
  # @param [RubyLint::AST::Node] node
  #
  def on_const(node)
    const_path = ConstantPath.new(node)

    files      = file_scanner.scan(const_path.to_s)
    last_name  = const_path.constant_segments.last.last

    paths << node.file

    files.each do |path|
      next if paths.include?(path)

      paths << path

      process_file(last_name, path)
    end
  end

  private

  ##
  # @param [String] constant_name
  # @param [String] path
  #
  def process_file(constant_name, path)
    code = File.read(path)

    return unless code.include?(constant_name)

    ast, comments = parser.parse(code, path)

    iterate(ast)

    nodes << [ast, comments]
  end
end

#pathsSet (readonly)

Returns:

  • (Set)


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
# File 'lib/ruby-lint/file_loader.rb', line 26

class FileLoader < Iterator
  attr_reader :file_scanner, :parser, :nodes, :comments, :paths

  ##
  # Called after a new instance of this class is created.
  #
  def after_initialize
    @file_scanner = FileScanner.new(@directories, @ignore_paths)
    @parser       = Parser.new
    @nodes        = []
    @paths        = Set.new
  end

  ##
  # @param [RubyLint::AST::Node] node
  #
  def on_const(node)
    const_path = ConstantPath.new(node)

    files      = file_scanner.scan(const_path.to_s)
    last_name  = const_path.constant_segments.last.last

    paths << node.file

    files.each do |path|
      next if paths.include?(path)

      paths << path

      process_file(last_name, path)
    end
  end

  private

  ##
  # @param [String] constant_name
  # @param [String] path
  #
  def process_file(constant_name, path)
    code = File.read(path)

    return unless code.include?(constant_name)

    ast, comments = parser.parse(code, path)

    iterate(ast)

    nodes << [ast, comments]
  end
end

Instance Method Details

#after_initializeObject

Called after a new instance of this class is created.



32
33
34
35
36
37
# File 'lib/ruby-lint/file_loader.rb', line 32

def after_initialize
  @file_scanner = FileScanner.new(@directories, @ignore_paths)
  @parser       = Parser.new
  @nodes        = []
  @paths        = Set.new
end

#on_const(node) ⇒ Object

Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby-lint/file_loader.rb', line 42

def on_const(node)
  const_path = ConstantPath.new(node)

  files      = file_scanner.scan(const_path.to_s)
  last_name  = const_path.constant_segments.last.last

  paths << node.file

  files.each do |path|
    next if paths.include?(path)

    paths << path

    process_file(last_name, path)
  end
end

#process_file(constant_name, path) ⇒ Object (private)

Parameters:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby-lint/file_loader.rb', line 65

def process_file(constant_name, path)
  code = File.read(path)

  return unless code.include?(constant_name)

  ast, comments = parser.parse(code, path)

  iterate(ast)

  nodes << [ast, comments]
end