Skip to content

Commit 3eb7d75

Browse files
committed
WIP
1 parent 3385fc3 commit 3eb7d75

File tree

4 files changed

+80
-75
lines changed

4 files changed

+80
-75
lines changed

lib/spoom/deadcode/index.rb

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def initialize(model)
3131
@graph = Saturn::Graph.new #: Saturn::Graph
3232
@definitions = {} #: Hash[String, Array[Definition]]
3333
@references = {} #: Hash[String, Array[Model::Reference]]
34-
@ignored = Set.new #: Set[Model::SymbolDef]
34+
@ignored = Set.new #: Set[String]
3535
end
3636

3737
# Indexing
@@ -103,26 +103,26 @@ def reference_method(name, location)
103103
(@references[name] ||= []) << Model::Reference.method(name, location)
104104
end
105105

106-
#: (Model::SymbolDef symbol_def) -> void
107-
def ignore(symbol_def)
108-
@ignored << symbol_def
106+
#: (Saturn::Definition definition) -> void
107+
def ignore(definition)
108+
@ignored << definition.name
109109
end
110110

111111
#: (Array[Plugins::Base] plugins) -> void
112112
def apply_plugins!(plugins)
113-
@model.symbols.each do |_full_name, symbol|
114-
symbol.definitions.each do |symbol_def|
115-
case symbol_def
116-
when Model::Class
117-
plugins.each { |plugin| plugin.internal_on_define_class(symbol_def) }
118-
when Model::Module
119-
plugins.each { |plugin| plugin.internal_on_define_module(symbol_def) }
120-
when Model::Constant
121-
plugins.each { |plugin| plugin.internal_on_define_constant(symbol_def) }
122-
when Model::Method
123-
plugins.each { |plugin| plugin.internal_on_define_method(symbol_def) }
124-
when Model::Attr
125-
plugins.each { |plugin| plugin.internal_on_define_accessor(symbol_def) }
113+
@graph.declarations.each do |declaration|
114+
declaration.definitions.each do |definition|
115+
case definition
116+
when Saturn::ClassDefinition
117+
plugins.each { |plugin| plugin.internal_on_define_class(definition) }
118+
when Saturn::ModuleDefinition
119+
plugins.each { |plugin| plugin.internal_on_define_module(definition) }
120+
when Saturn::ConstantDefinition
121+
plugins.each { |plugin| plugin.internal_on_define_constant(definition) }
122+
when Saturn::MethodDefinition
123+
plugins.each { |plugin| plugin.internal_on_define_method(definition) }
124+
when Saturn::AttrAccessorDefinition, Saturn::AttrReaderDefinition, Saturn::AttrWriterDefinition
125+
plugins.each { |plugin| plugin.internal_on_define_accessor(definition) }
126126
end
127127
end
128128
end
@@ -133,112 +133,113 @@ def apply_plugins!(plugins)
133133
# To be called once all the files have been indexed and all the definitions and references discovered.
134134
#: -> void
135135
def finalize!
136+
@graph.unresolved_references.each do |ref|
137+
case ref
138+
when Saturn::UnresolvedConstantReference
139+
reference_constant(
140+
ref.name.split("::").last,
141+
saturn_location_to_spoom_location(ref.location),
142+
)
143+
when Saturn::UnresolvedMethodReference
144+
reference_method(
145+
ref.name,
146+
saturn_location_to_spoom_location(ref.location),
147+
)
148+
end
149+
end
150+
136151
@graph.declarations.each do |declaration|
137152
declaration.definitions.each do |definition|
138153
case definition
139154
when Saturn::ClassDefinition
155+
name = definition.name
140156
d = Definition.new(
141157
kind: Definition::Kind::Class,
142-
name: declaration_name(declaration.name),
158+
name: name,
143159
full_name: declaration.name,
144160
location: saturn_location_to_spoom_location(definition.location),
145161
)
146-
d.ignored! if @ignored.include?(definition)
147-
d.alive! if @references.key?(d.name)
162+
d.ignored! if @ignored.include?(name)
163+
d.alive! if @references.key?(name)
148164
define(d)
149165
when Saturn::ModuleDefinition
166+
name = definition.name
150167
d = Definition.new(
151168
kind: Definition::Kind::Module,
152-
name: declaration_name(declaration.name),
169+
name: name,
153170
full_name: declaration.name,
154171
location: saturn_location_to_spoom_location(definition.location),
155172
)
156-
d.ignored! if @ignored.include?(definition)
157-
d.alive! if @references.key?(d.name)
173+
d.ignored! if @ignored.include?(name)
174+
d.alive! if @references.key?(name)
158175
define(d)
159176
when Saturn::ConstantDefinition
177+
name = definition.name
160178
d = Definition.new(
161179
kind: Definition::Kind::Constant,
162-
name: declaration_name(declaration.name),
180+
name: name,
163181
full_name: declaration.name,
164182
location: saturn_location_to_spoom_location(definition.location),
165183
)
166-
d.ignored! if @ignored.include?(definition)
167-
d.alive! if @references.key?(d.name)
184+
d.ignored! if @ignored.include?(name)
185+
d.alive! if @references.key?(name)
168186
define(d)
169187
when Saturn::MethodDefinition
188+
name = definition.name
170189
d = Definition.new(
171190
kind: Definition::Kind::Method,
172-
name: declaration_name(declaration.name),
191+
name: name,
173192
full_name: declaration.name,
174193
location: saturn_location_to_spoom_location(definition.location),
175194
)
176-
d.ignored! if @ignored.include?(definition)
177-
d.alive! if @references.key?(d.name)
195+
d.ignored! if @ignored.include?(name)
196+
d.alive! if @references.key?(name)
178197
define(d)
179198
when Saturn::AttrAccessorDefinition
180-
d = if declaration.name.end_with?("=")
199+
name = definition.name
200+
d = if name.end_with?("=")
181201
Definition.new(
182202
kind: Definition::Kind::AttrWriter,
183-
name: declaration_name(declaration.name),
203+
name: name,
184204
full_name: declaration.name,
185205
location: saturn_location_to_spoom_location(definition.location),
186206
)
187207
else
188208
Definition.new(
189209
kind: Definition::Kind::AttrReader,
190-
name: declaration_name(declaration.name),
210+
name: name,
191211
full_name: declaration.name,
192212
location: saturn_location_to_spoom_location(definition.location),
193213
)
194214
end
195-
d.ignored! if @ignored.include?(definition)
196-
d.alive! if @references.key?(d.name)
215+
d.ignored! if @ignored.include?(name)
216+
d.alive! if @references.key?(name)
197217
define(d)
198218
when Saturn::AttrReaderDefinition
219+
name = definition.name
199220
d = Definition.new(
200221
kind: Definition::Kind::AttrReader,
201-
name: declaration_name(declaration.name),
222+
name: name,
202223
full_name: declaration.name,
203224
location: saturn_location_to_spoom_location(definition.location),
204225
)
205-
d.ignored! if @ignored.include?(definition)
206-
d.alive! if @references.key?(d.name)
226+
d.ignored! if @ignored.include?(name)
227+
d.alive! if @references.key?(name)
207228
define(d)
208229
when Saturn::AttrWriterDefinition
230+
name = definition.name
209231
d = Definition.new(
210232
kind: Definition::Kind::AttrWriter,
211-
name: declaration_name(declaration.name),
233+
name: name,
212234
full_name: declaration.name,
213235
location: saturn_location_to_spoom_location(definition.location),
214236
)
215-
d.ignored! if @ignored.include?(definition)
216-
d.alive! if @references.key?(d.name)
237+
d.ignored! if @ignored.include?(name)
238+
d.alive! if @references.key?(name)
217239
define(d)
218240
end
219241
end
220242
end
221-
222-
@graph.unresolved_references.each do |ref|
223-
case ref
224-
when Saturn::UnresolvedConstantReference
225-
reference_constant(
226-
declaration_name(ref.name),
227-
saturn_location_to_spoom_location(ref.location),
228-
)
229-
when Saturn::UnresolvedMethodReference
230-
reference_method(
231-
ref.name,
232-
saturn_location_to_spoom_location(ref.location),
233-
)
234-
end
235-
end
236-
end
237-
238-
# TODO
239-
#: (String name) -> String
240-
def declaration_name(name)
241-
name.split(/(::|#)/).last #: as !nil
242243
end
243244

244245
#: (Saturn::Location location) -> Location
@@ -247,8 +248,8 @@ def saturn_location_to_spoom_location(location)
247248
location.path,
248249
start_line: location.start_line,
249250
end_line: location.end_line,
250-
start_column: location.start_column,
251-
end_column: location.end_column,
251+
start_column: location.start_column - 1,
252+
end_column: location.end_column - 1,
252253
)
253254
end
254255

lib/spoom/deadcode/plugins/base.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ def initialize(index)
142142
# end
143143
# end
144144
# ~~~
145-
#: (Model::Attr definition) -> void
145+
#: (Saturn::AttrAccessorDefinition | Saturn::AttrReaderDefinition | Saturn::AttrWriterDefinition definition) -> void
146146
def on_define_accessor(definition)
147147
# no-op
148148
end
149149

150150
# Do not override this method, use `on_define_accessor` instead.
151-
#: (Model::Attr definition) -> void
151+
#: (Saturn::AttrAccessorDefinition | Saturn::AttrReaderDefinition | Saturn::AttrWriterDefinition definition) -> void
152152
def internal_on_define_accessor(definition)
153153
on_define_accessor(definition)
154154
end
@@ -166,13 +166,13 @@ def internal_on_define_accessor(definition)
166166
# end
167167
# end
168168
# ~~~
169-
#: (Model::Class definition) -> void
169+
#: (Saturn::ClassDefinition definition) -> void
170170
def on_define_class(definition)
171171
# no-op
172172
end
173173

174174
# Do not override this method, use `on_define_class` instead.
175-
#: (Model::Class definition) -> void
175+
#: (Saturn::ClassDefinition definition) -> void
176176
def internal_on_define_class(definition)
177177
if ignored_class_name?(definition.name)
178178
@index.ignore(definition)
@@ -196,13 +196,13 @@ def internal_on_define_class(definition)
196196
# end
197197
# end
198198
# ~~~
199-
#: (Model::Constant definition) -> void
199+
#: (Saturn::ConstantDefinition definition) -> void
200200
def on_define_constant(definition)
201201
# no-op
202202
end
203203

204204
# Do not override this method, use `on_define_constant` instead.
205-
#: (Model::Constant definition) -> void
205+
#: (Saturn::ConstantDefinition definition) -> void
206206
def internal_on_define_constant(definition)
207207
@index.ignore(definition) if ignored_constant_name?(definition.name)
208208

@@ -222,13 +222,13 @@ def internal_on_define_constant(definition)
222222
# end
223223
# end
224224
# ~~~
225-
#: (Model::Method definition) -> void
225+
#: (Saturn::MethodDefinition definition) -> void
226226
def on_define_method(definition)
227227
# no-op
228228
end
229229

230230
# Do not override this method, use `on_define_method` instead.
231-
#: (Model::Method definition) -> void
231+
#: (Saturn::MethodDefinition definition) -> void
232232
def internal_on_define_method(definition)
233233
@index.ignore(definition) if ignored_method_name?(definition.name)
234234

@@ -248,13 +248,13 @@ def internal_on_define_method(definition)
248248
# end
249249
# end
250250
# ~~~
251-
#: (Model::Module definition) -> void
251+
#: (Saturn::ModuleDefinition definition) -> void
252252
def on_define_module(definition)
253253
# no-op
254254
end
255255

256256
# Do not override this method, use `on_define_module` instead.
257-
#: (Model::Module definition) -> void
257+
#: (Saturn::ModuleDefinition definition) -> void
258258
def internal_on_define_module(definition)
259259
@index.ignore(definition) if ignored_module_name?(definition.name)
260260

@@ -298,7 +298,7 @@ def ignored_class_name?(name)
298298
ignored_name?(name, :@ignored_class_names, :@ignored_class_patterns)
299299
end
300300

301-
#: (Model::Class definition) -> bool
301+
#: (Saturn::ClassDefinition definition) -> bool
302302
def ignored_subclass?(definition)
303303
superclass_name = definition.superclass_name
304304
return true if superclass_name && ignored_name?(

sorbet/rbi/gems/[email protected]

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/spoom/deadcode/remover_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,14 +1617,15 @@ def remove(ruby_string, def_name)
16171617

16181618
model = Model.new
16191619
index = Index.new(model)
1620-
index.index_ruby(ruby_string, file: file)
1620+
index.index_files([context.absolute_path_to(file)])
16211621
index.finalize!
16221622

16231623
definitions = definitions_for_name(index, def_name)
16241624
assert_equal(1, definitions.size) # We only support one def by name in these tests
16251625
definition = T.must(definitions.first)
16261626

16271627
remover = Remover.new(context)
1628+
definition.location.instance_variable_set(:@file, file)
16281629
new_source = remover.remove_location(definition.kind, definition.location)
16291630

16301631
context.destroy!

0 commit comments

Comments
 (0)