Class: Leftovers::FileCollector

Inherits:
Object
  • Object
show all
Includes:
Autoloader
Defined in:
lib/leftovers/file_collector.rb,
lib/leftovers/file_collector/node_processor.rb,
lib/leftovers/file_collector/comments_processor.rb,
lib/leftovers/file_collector/node_processor/error.rb

Defined Under Namespace

Modules: CommentsProcessor Classes: Error, NodeProcessor

Constant Summary

Constants included from Autoloader

Autoloader::ALL_CAPS_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Autoloader

class_from_path, dir_path_from_class, glob_children, included, root

Constructor Details

#initialize(ruby, file) ⇒ FileCollector

Returns a new instance of FileCollector.



14
15
16
17
18
19
20
21
22
23
# File 'lib/leftovers/file_collector.rb', line 14

def initialize(ruby, file)
  @calls = []
  @definition_collection = DefinitionCollection.new
  @allow_lines = ::Set.new.compare_by_identity
  @test_lines = ::Set.new.compare_by_identity
  @dynamic_lines = {}
  @ruby = ruby
  @file = file
  @default_method_privacy = :public
end

Instance Attribute Details

#allow_linesObject (readonly)

Returns the value of attribute allow_lines.



11
12
13
# File 'lib/leftovers/file_collector.rb', line 11

def allow_lines
  @allow_lines
end

#callsObject (readonly)

Returns the value of attribute calls.



11
12
13
# File 'lib/leftovers/file_collector.rb', line 11

def calls
  @calls
end

#default_method_privacyObject

Returns the value of attribute default_method_privacy.



12
13
14
# File 'lib/leftovers/file_collector.rb', line 12

def default_method_privacy
  @default_method_privacy
end

#dynamic_linesObject (readonly)

Returns the value of attribute dynamic_lines.



11
12
13
# File 'lib/leftovers/file_collector.rb', line 11

def dynamic_lines
  @dynamic_lines
end

#test_linesObject (readonly)

Returns the value of attribute test_lines.



11
12
13
# File 'lib/leftovers/file_collector.rb', line 11

def test_lines
  @test_lines
end

Instance Method Details

#add_definition(node, name: node.name, loc: node.loc.name) ⇒ Object



84
85
86
# File 'lib/leftovers/file_collector.rb', line 84

def add_definition(node, name: node.name, loc: node.loc.name)
  @definition_collection.add(node, name: name, loc: loc)
end

#add_definition_node(definition_node) ⇒ Object



88
89
90
# File 'lib/leftovers/file_collector.rb', line 88

def add_definition_node(definition_node)
  @definition_collection.add_definition_node(definition_node)
end

#add_definition_set(definition_node_set) ⇒ Object



76
77
78
# File 'lib/leftovers/file_collector.rb', line 76

def add_definition_set(definition_node_set)
  @definition_collection.add_definition_set(definition_node_set)
end

#collect(ruby = @ruby, line = 1) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/leftovers/file_collector.rb', line 40

def collect(ruby = @ruby, line = 1) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  ast, comments = Parser.parse_with_comments(ruby, @file.relative_path, line)
  CommentsProcessor.process(comments, self)
  NodeProcessor.new(self).process(ast)
rescue ::Parser::SyntaxError => e
  raise Error, <<~MESSAGE.chomp, e.backtrace
    SyntaxError: #{e.message}
      when processing #{filename}:#{e.diagnostic.location.line}:#{e.diagnostic.location.column}
  MESSAGE
rescue NodeProcessor::Error => e
  raise Error, <<~MESSAGE.chomp, e.backtrace
    #{e.cause.class}: #{e.message}
      when processing #{e.node} at #{filename}:#{e.node.loc.line}:#{e.node.loc.column}
  MESSAGE
rescue Error
  raise
rescue ::StandardError => e
  raise Error, <<~MESSAGE.chomp, e.backtrace
    #{e.class}: #{e.message}
      when processing #{filename}
  MESSAGE
end

#collect_commented_dynamic(node) ⇒ Object



121
122
123
124
125
# File 'lib/leftovers/file_collector.rb', line 121

def collect_commented_dynamic(node)
  @dynamic_lines[node.loc.line]&.each do |fake_method_name|
    collect_dynamic(build_send_wrapper_for(node, fake_method_name))
  end
end

#collect_dynamic(node) ⇒ Object



127
128
129
# File 'lib/leftovers/file_collector.rb', line 127

def collect_dynamic(node)
  ::Leftovers.config.dynamic.process(nil, node, node, self)
end

#collect_op_asgn(node) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/leftovers/file_collector.rb', line 106

def collect_op_asgn(node)
  node = node.first
  case node.type
  # just collects the :call=, super will collect the :call
  when :send, :csend then calls << :"#{node.name}="
  # just collects the call, super will collect the definition
  when :ivasgn, :gvasgn, :cvasgn, :casgn then calls << node.name
  when :lvasgn then nil # we don't check local variable use, rubocop already covers this

  # :nocov:
  else raise UnexpectedCase, "Unhandled value #{node.type.inspect}"
    # :nocov:
  end
end

#collect_send(node) ⇒ Object



96
97
98
99
# File 'lib/leftovers/file_collector.rb', line 96

def collect_send(node)
  calls << node.name
  collect_dynamic(node)
end

#collect_subfile(string, location) ⇒ Object



63
64
65
66
# File 'lib/leftovers/file_collector.rb', line 63

def collect_subfile(string, location)
  string = (' ' * location.column) + string # match indentation
  collect(string, location.line)
end

#collect_variable_assign(node) ⇒ Object



101
102
103
104
# File 'lib/leftovers/file_collector.rb', line 101

def collect_variable_assign(node)
  add_definition(node)
  collect_dynamic(node)
end

#definitionsObject



92
93
94
# File 'lib/leftovers/file_collector.rb', line 92

def definitions
  @definitions ||= @definition_collection.to_definitions(self)
end

#filenameObject



25
26
27
# File 'lib/leftovers/file_collector.rb', line 25

def filename
  @filename ||= @file.relative_path
end

#keep_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/leftovers/file_collector.rb', line 72

def keep_line?(line)
  @allow_lines.include?(line)
end

#set_privacy(node, to) ⇒ Object



80
81
82
# File 'lib/leftovers/file_collector.rb', line 80

def set_privacy(node, to)
  @definition_collection.set_privacy(node, to)
end

#squash!(list) ⇒ Object



33
34
35
36
37
38
# File 'lib/leftovers/file_collector.rb', line 33

def squash!(list)
  list.flatten!
  list.compact!
  list.uniq!
  list
end

#test_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/leftovers/file_collector.rb', line 68

def test_line?(line)
  @file.test? || @test_lines.include?(line)
end

#to_hObject



29
30
31
# File 'lib/leftovers/file_collector.rb', line 29

def to_h
  { test: @file.test?, calls: squash!(calls), definitions: squash!(definitions) }
end