Class: Spoom::Deadcode::Index

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/spoom/deadcode/index.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Index

Returns a new instance of Index.



29
30
31
32
33
34
# File 'lib/spoom/deadcode/index.rb', line 29

def initialize(model)
  @model = model
  @definitions = T.let({}, T::Hash[String, T::Array[Definition]])
  @references = T.let({}, T::Hash[String, T::Array[Model::Reference]])
  @ignored = T.let(Set.new, T::Set[Model::SymbolDef])
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



23
24
25
# File 'lib/spoom/deadcode/index.rb', line 23

def definitions
  @definitions
end

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/spoom/deadcode/index.rb', line 20

def model
  @model
end

#referencesObject (readonly)

Returns the value of attribute references.



26
27
28
# File 'lib/spoom/deadcode/index.rb', line 26

def references
  @references
end

Instance Method Details

#all_definitionsObject



219
220
221
# File 'lib/spoom/deadcode/index.rb', line 219

def all_definitions
  @definitions.values.flatten
end

#all_referencesObject



224
225
226
# File 'lib/spoom/deadcode/index.rb', line 224

def all_references
  @references.values.flatten
end

#apply_plugins!(plugins) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spoom/deadcode/index.rb', line 99

def apply_plugins!(plugins)
  @model.symbols.each do |_full_name, symbol|
    symbol.definitions.each do |symbol_def|
      case symbol_def
      when Model::Class
        plugins.each { |plugin| plugin.internal_on_define_class(symbol_def) }
      when Model::Module
        plugins.each { |plugin| plugin.internal_on_define_module(symbol_def) }
      when Model::Constant
        plugins.each { |plugin| plugin.internal_on_define_constant(symbol_def) }
      when Model::Method
        plugins.each { |plugin| plugin.internal_on_define_method(symbol_def) }
      when Model::Attr
        plugins.each { |plugin| plugin.internal_on_define_accessor(symbol_def) }
      end
    end
  end
end

#define(definition) ⇒ Object



79
80
81
# File 'lib/spoom/deadcode/index.rb', line 79

def define(definition)
  (@definitions[definition.name] ||= []) << definition
end

#definitions_for_name(name) ⇒ Object



214
215
216
# File 'lib/spoom/deadcode/index.rb', line 214

def definitions_for_name(name)
  @definitions[name] || []
end

#finalize!Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/spoom/deadcode/index.rb', line 122

def finalize!
  @model.symbols.each do |_full_name, symbol|
    symbol.definitions.each do |symbol_def|
      case symbol_def
      when Model::Class
        definition = Definition.new(
          kind: Definition::Kind::Class,
          name: symbol.name,
          full_name: symbol.full_name,
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      when Model::Module
        definition = Definition.new(
          kind: Definition::Kind::Module,
          name: symbol.name,
          full_name: symbol.full_name,
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      when Model::Constant
        definition = Definition.new(
          kind: Definition::Kind::Constant,
          name: symbol.name,
          full_name: symbol.full_name,
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      when Model::Method
        definition = Definition.new(
          kind: Definition::Kind::Method,
          name: symbol.name,
          full_name: symbol.full_name,
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      when Model::AttrAccessor
        definition = Definition.new(
          kind: Definition::Kind::AttrReader,
          name: symbol.name,
          full_name: symbol.full_name,
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)

        definition = Definition.new(
          kind: Definition::Kind::AttrWriter,
          name: "#{symbol.name}=",
          full_name: "#{symbol.full_name}=",
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      when Model::AttrReader
        definition = Definition.new(
          kind: Definition::Kind::AttrReader,
          name: symbol.name,
          full_name: symbol.full_name,
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      when Model::AttrWriter
        definition = Definition.new(
          kind: Definition::Kind::AttrWriter,
          name: "#{symbol.name}=",
          full_name: "#{symbol.full_name}=",
          location: symbol_def.location,
        )
        definition.ignored! if @ignored.include?(symbol_def)
        definition.alive! if @references.key?(symbol.name)
        define(definition)
      end
    end
  end
end

#ignore(symbol_def) ⇒ Object



94
95
96
# File 'lib/spoom/deadcode/index.rb', line 94

def ignore(symbol_def)
  @ignored << symbol_def
end

#index_erb(erb, file:, plugins: []) ⇒ Object



50
51
52
# File 'lib/spoom/deadcode/index.rb', line 50

def index_erb(erb, file:, plugins: [])
  index_ruby(Deadcode::ERB.new(erb).src, file: file, plugins: plugins)
end

#index_file(file, plugins: []) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/spoom/deadcode/index.rb', line 39

def index_file(file, plugins: [])
  if file.end_with?(".erb")
    erb = File.read(file)
    index_erb(erb, file: file, plugins: plugins)
  else
    rb = File.read(file)
    index_ruby(rb, file: file, plugins: plugins)
  end
end

#index_ruby(rb, file:, plugins: []) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/spoom/deadcode/index.rb', line 55

def index_ruby(rb, file:, plugins: [])
  node = Spoom.parse_ruby(rb, file: file)

  # Index definitions
  model_builder = Model::Builder.new(@model, file)
  model_builder.visit(node)

  # Index references
  refs_visitor = Model::ReferencesVisitor.new(file)
  refs_visitor.visit(node)
  refs_visitor.references.each do |ref|
    (@references[ref.name] ||= []) << ref
  end

  # Index references and sends
  indexer = Indexer.new(file, self, plugins: plugins)
  indexer.visit(node)
rescue ParseError => e
  raise e
rescue => e
  raise Error.new("Error while indexing #{file} (#{e.message})", parent: e)
end

#reference_constant(name, location) ⇒ Object



84
85
86
# File 'lib/spoom/deadcode/index.rb', line 84

def reference_constant(name, location)
  (@references[name] ||= []) << Model::Reference.constant(name, location)
end

#reference_method(name, location) ⇒ Object



89
90
91
# File 'lib/spoom/deadcode/index.rb', line 89

def reference_method(name, location)
  (@references[name] ||= []) << Model::Reference.method(name, location)
end