class Leftovers::FileCollector

Constants

CONSTANT_NAME_RE
LEFTOVERS_ALLOW_RE
LEFTOVERS_CALL_RE
LEFTOVERS_TEST_RE
METHOD_NAME_RE
NAME_RE
NON_ALNUM_METHOD_NAME_RE

Attributes

calls[R]
definitions[R]

Public Class Methods

new(ruby, file) click to toggle source
# File lib/leftovers/file_collector.rb, line 10
def initialize(ruby, file) # rubocop:disable Lint/MissingSuper
  @calls = []
  @definitions = []
  @allow_lines = Set.new.compare_by_identity
  @test_lines = Set.new.compare_by_identity
  @ruby = ruby
  @file = file
end

Public Instance Methods

collect() click to toggle source
# File lib/leftovers/file_collector.rb, line 43
def collect
  ast, comments = Leftovers::Parser.parse_with_comments(@ruby, @file.relative_path)
  process_comments(comments)
  process(ast)
rescue ::Parser::SyntaxError => e
  Leftovers.warn "\e[31m#{filename}:#{e.diagnostic.location.line}:#{e.diagnostic.location.column} SyntaxError: #{e.message}\e[0m" # rubocop:disable Layout/LineLength
end
filename() click to toggle source
# File lib/leftovers/file_collector.rb, line 19
def filename
  @filename ||= @file.relative_path
end
on_alias(node) click to toggle source

grab calls to `alias new_method original_method`

Calls superclass method
# File lib/leftovers/file_collector.rb, line 165
def on_alias(node)
  super
  new_method, original_method = node.children
  add_definition(new_method, name: new_method.children.first, loc: new_method.loc.expression)
  add_call(original_method.children.first)
end
on_and_asgn(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 113
def on_and_asgn(node)
  collect_op_asgn(node)
  super
end
on_block_pass(node) click to toggle source

grab e.g. :to_s in each(&:to_s)

Calls superclass method
# File lib/leftovers/file_collector.rb, line 140
def on_block_pass(node)
  super
  add_call(node.children.first.to_sym) if node.children.first.string_or_symbol?
end
on_casgn(node) click to toggle source

grab Constant = Class.new or CONSTANT = 'string'.freeze

Calls superclass method
# File lib/leftovers/file_collector.rb, line 158
def on_casgn(node)
  super
  add_definition(node)
  collect_dynamic(node)
end
on_class(node) click to toggle source

grab class Constant or module Constant

# File lib/leftovers/file_collector.rb, line 146
def on_class(node)
  # don't call super so we don't process the class name
  # !!! (# wtf does this mean dana? what would happen instead?)
  process_all(node.children.drop(1))

  node = node.children.first

  add_definition(node)
end
Also aliased as: on_module
on_const(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 134
def on_const(node)
  super
  add_call(node.name)
end
on_csend(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 129
def on_csend(node)
  super
  collect_send(node)
end
on_cvar(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 103
def on_cvar(node)
  add_call(node.name)
  super
end
on_cvasgn(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 88
def on_cvasgn(node)
  collect_variable_assign(node)
  super
end
on_def(node) click to toggle source

grab method definitions

Calls superclass method
# File lib/leftovers/file_collector.rb, line 73
def on_def(node)
  add_definition(node)
  super
end
on_gvar(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 98
def on_gvar(node)
  add_call(node.name)
  super
end
on_gvasgn(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 83
def on_gvasgn(node)
  collect_variable_assign(node)
  super
end
on_ivar(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 93
def on_ivar(node)
  add_call(node.name)
  super
end
on_ivasgn(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 78
def on_ivasgn(node)
  collect_variable_assign(node)
  super
end
on_module(node)
Alias for: on_class
on_op_asgn(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 108
def on_op_asgn(node)
  collect_op_asgn(node)
  super
end
on_or_asgn(node) click to toggle source
Calls superclass method
# File lib/leftovers/file_collector.rb, line 118
def on_or_asgn(node)
  collect_op_asgn(node)
  super
end
on_send(node) click to toggle source

grab method calls

Calls superclass method
# File lib/leftovers/file_collector.rb, line 124
def on_send(node)
  super
  collect_send(node)
end
process_comments(comments) click to toggle source
# File lib/leftovers/file_collector.rb, line 61
def process_comments(comments) # rubocop:disable Metrics/AbcSize
  comments.each do |comment|
    @allow_lines << comment.loc.line if comment.text.match?(LEFTOVERS_ALLOW_RE)
    @test_lines << comment.loc.line if comment.text.match?(LEFTOVERS_TEST_RE)

    next unless (match = comment.text.match(LEFTOVERS_CALL_RE))

    match[1].scan(NAME_RE).each { |s| add_call(s.to_sym) }
  end
end
squash!() click to toggle source
# File lib/leftovers/file_collector.rb, line 33
def squash!
  calls.flatten!
  calls.compact!
  calls.uniq!
  definitions.flatten!
  definitions.compact!
  definitions.uniq!
  definitions.reject! { |v| v == :keep }
end
to_h() click to toggle source
# File lib/leftovers/file_collector.rb, line 23
def to_h
  squash!

  {
    test?: @file.test?,
    calls: calls,
    definitions: definitions
  }
end

Private Instance Methods

add_call(name) click to toggle source
# File lib/leftovers/file_collector.rb, line 190
def add_call(name)
  calls << name
end
add_definition(node, name: node.name, loc: node.loc.name) click to toggle source
# File lib/leftovers/file_collector.rb, line 183
def add_definition(node, name: node.name, loc: node.loc.name)
  return if @allow_lines.include?(loc.line)
  return if Leftovers.config.keep === node

  definitions << Leftovers::Definition.new(name, location: loc, test: test_node?(node, loc))
end
collect_dynamic(node) click to toggle source
# File lib/leftovers/file_collector.rb, line 228
def collect_dynamic(node) # rubocop:disable Metrics/AbcSize
  node.keep_line = @allow_lines.include?(node.loc.line)
  node.test_line = test_line?(node.loc) unless node.keep_line?

  Leftovers.config.dynamic.process(node, self)
rescue StandardError => e
  raise ::Leftovers::Error, "#{e.class}: #{e.message}\nwhen processing #{node} at #{filename}:#{node.loc.line}:#{node.loc.column}", e.backtrace # rubocop:disable Layout/LineLength
end
collect_op_asgn(node) click to toggle source
# File lib/leftovers/file_collector.rb, line 218
def collect_op_asgn(node)
  node = node.children.first
  # :nocov: # don't need else, it's exhaustive for callers
  case node.type
  # :nocov:
  when :send then collect_send_op_asgn(node)
  when :ivasgn, :gvasgn, :cvasgn then collect_var_op_asgn(node)
  end
end
collect_send(node) click to toggle source
# File lib/leftovers/file_collector.rb, line 194
def collect_send(node)
  add_call(node.name)
  collect_dynamic(node)
end
collect_send_op_asgn(node) click to toggle source
# File lib/leftovers/file_collector.rb, line 206
def collect_send_op_asgn(node)
  name = node.children[1]

  add_call(:"#{name}=")
end
collect_var_op_asgn(node) click to toggle source

just collects the call, super will collect the definition

# File lib/leftovers/file_collector.rb, line 200
def collect_var_op_asgn(node)
  name = node.children.first

  add_call(name)
end
collect_variable_assign(node) click to toggle source
# File lib/leftovers/file_collector.rb, line 212
def collect_variable_assign(node)
  add_definition(node)

  collect_dynamic(node)
end
test_line?(loc) click to toggle source
# File lib/leftovers/file_collector.rb, line 174
def test_line?(loc)
  @file.test? ||
    @test_lines.include?(loc.line)
end
test_node?(node, loc) click to toggle source
# File lib/leftovers/file_collector.rb, line 179
def test_node?(node, loc)
  test_line?(loc) || ::Leftovers.config.test_only === node
end