Class: Leftovers::FileCollector::NodeProcessor

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

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Error

Constant Summary

Constants included from Autoloader

Autoloader::ALL_CAPS_NAMES

Instance Method Summary collapse

Methods included from Autoloader

class_from_path, dir_path_from_class, glob_children, included, root

Constructor Details

#initialize(collector) ⇒ NodeProcessor

rubocop:disable Lint/MissingSuper # there isn’t one to call



18
19
20
# File 'lib/leftovers/file_collector/node_processor.rb', line 18

def initialize(collector) # rubocop:disable Lint/MissingSuper # there isn't one to call
  @collector = collector
end

Instance Method Details

#on_alias(node) ⇒ Object

grab calls to ‘alias new_method original_method`



139
140
141
142
143
144
145
146
# File 'lib/leftovers/file_collector/node_processor.rb', line 139

def on_alias(node)
  super
  new_method, original_method = node.children
  @collector.add_definition(
    new_method, name: new_method.children.first, loc: new_method.loc.expression
  )
  @collector.calls << original_method.children.first
end

#on_and_asgn(node) ⇒ Object



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

def on_and_asgn(node)
  @collector.collect_op_asgn(node)
  super
end

#on_array(node) ⇒ Object



93
94
95
96
# File 'lib/leftovers/file_collector/node_processor.rb', line 93

def on_array(node)
  super
  @collector.collect_commented_dynamic(node)
end

#on_block(node) ⇒ Object

why are block args the parent of send/csend



104
105
106
107
108
# File 'lib/leftovers/file_collector/node_processor.rb', line 104

def on_block(node)
  node.first.parent = node

  super
end

#on_block_pass(node) ⇒ Object

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



111
112
113
114
115
116
# File 'lib/leftovers/file_collector/node_processor.rb', line 111

def on_block_pass(node)
  super
  return unless node.first&.sym?

  @collector.calls << node.first.to_sym
end

#on_casgn(node) ⇒ Object

grab Constant = Class.new or CONSTANT = ‘string’.freeze or Constant ||= Class.new



131
132
133
134
135
136
# File 'lib/leftovers/file_collector/node_processor.rb', line 131

def on_casgn(node)
  super

  @collector.add_definition(node)
  @collector.collect_dynamic(node)
end

#on_class(node) ⇒ Object Also known as: on_module

grab class Constant or module Constant



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

def on_class(node)
  # don't call super so we don't interpret the class name as being called by its definition
  process_all(node.children.drop(1))

  node = node.children.first

  @collector.add_definition(node)
end

#on_const(node) ⇒ Object



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

def on_const(node)
  super
  @collector.calls << node.name
end

#on_csend(node) ⇒ Object



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

def on_csend(node)
  super
  @collector.collect_send(node)
end

#on_cvar(node) ⇒ Object



58
59
60
61
# File 'lib/leftovers/file_collector/node_processor.rb', line 58

def on_cvar(node)
  @collector.calls << node.name
  super
end

#on_cvasgn(node) ⇒ Object



43
44
45
46
# File 'lib/leftovers/file_collector/node_processor.rb', line 43

def on_cvasgn(node)
  @collector.collect_variable_assign(node)
  super
end

#on_def(node) ⇒ Object



22
23
24
25
26
# File 'lib/leftovers/file_collector/node_processor.rb', line 22

def on_def(node)
  node.privacy = @collector.default_method_privacy
  @collector.add_definition(node)
  super
end

#on_defs(node) ⇒ Object



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

def on_defs(node)
  @collector.add_definition(node)
  super
end

#on_gvar(node) ⇒ Object



53
54
55
56
# File 'lib/leftovers/file_collector/node_processor.rb', line 53

def on_gvar(node)
  @collector.calls << node.name
  super
end

#on_gvasgn(node) ⇒ Object



38
39
40
41
# File 'lib/leftovers/file_collector/node_processor.rb', line 38

def on_gvasgn(node)
  @collector.collect_variable_assign(node)
  super
end

#on_hash(node) ⇒ Object



98
99
100
101
# File 'lib/leftovers/file_collector/node_processor.rb', line 98

def on_hash(node)
  super
  @collector.collect_commented_dynamic(node)
end

#on_ivar(node) ⇒ Object



48
49
50
51
# File 'lib/leftovers/file_collector/node_processor.rb', line 48

def on_ivar(node)
  @collector.calls << node.name
  super
end

#on_ivasgn(node) ⇒ Object



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

def on_ivasgn(node)
  @collector.collect_variable_assign(node)
  super
end

#on_op_asgn(node) ⇒ Object



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

def on_op_asgn(node)
  @collector.collect_op_asgn(node)
  super
end

#on_or_asgn(node) ⇒ Object



73
74
75
76
# File 'lib/leftovers/file_collector/node_processor.rb', line 73

def on_or_asgn(node)
  @collector.collect_op_asgn(node)
  super
end

#on_send(node) ⇒ Object



78
79
80
81
# File 'lib/leftovers/file_collector/node_processor.rb', line 78

def on_send(node)
  super
  @collector.collect_send(node)
end

#process(ast) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/leftovers/file_collector/node_processor.rb', line 10

def process(ast)
  super
rescue FileCollector::Error
  raise
rescue StandardError => e
  raise Error.new(e.message, ast)
end