lib/spoom/deadcode/plugins/base.rb in spoom-1.3.2 vs lib/spoom/deadcode/plugins/base.rb in spoom-1.3.3

- old
+ new

@@ -123,185 +123,198 @@ end end end end + sig { returns(Index) } + attr_reader :index + + sig { params(index: Index).void } + def initialize(index) + @index = index + end + # Indexing event methods # Called when an accessor is defined. # # Will be called when the indexer processes a `attr_reader`, `attr_writer` or `attr_accessor` node. # Note that when this method is called, the definition for the node has already been added to the index. # It is still possible to ignore it from the plugin: # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_define_accessor(indexer, definition) - # definition.ignored! if definition.name == "foo" + # def on_define_accessor(definition) + # @index.ignore(definition) if symbol_def.name == "foo" # end # end # ~~~ - sig { params(indexer: Indexer, definition: Definition).void } - def on_define_accessor(indexer, definition) + sig { params(definition: Model::Attr).void } + def on_define_accessor(definition) # no-op end # Do not override this method, use `on_define_accessor` instead. - sig { params(indexer: Indexer, definition: Definition).void } - def internal_on_define_accessor(indexer, definition) - on_define_accessor(indexer, definition) + sig { params(definition: Model::Attr).void } + def internal_on_define_accessor(definition) + on_define_accessor(definition) end # Called when a class is defined. # # Will be called when the indexer processes a `class` node. # Note that when this method is called, the definition for the node has already been added to the index. # It is still possible to ignore it from the plugin: # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_define_class(indexer, definition) - # definition.ignored! if definition.name == "Foo" + # def on_define_class(definition) + # @index.ignore(definition) if definition.name == "Foo" # end # end # ~~~ - sig { params(indexer: Indexer, definition: Definition).void } - def on_define_class(indexer, definition) + sig { params(definition: Model::Class).void } + def on_define_class(definition) # no-op end # Do not override this method, use `on_define_class` instead. - sig { params(indexer: Indexer, definition: Definition).void } - def internal_on_define_class(indexer, definition) + sig { params(definition: Model::Class).void } + def internal_on_define_class(definition) if ignored_class_name?(definition.name) - definition.ignored! - elsif ignored_subclass?(indexer.nesting_class_superclass_name) - definition.ignored! + @index.ignore(definition) + elsif ignored_subclass?(definition) + @index.ignore(definition) end - on_define_class(indexer, definition) + on_define_class(definition) end # Called when a constant is defined. # # Will be called when the indexer processes a `CONST =` node. # Note that when this method is called, the definition for the node has already been added to the index. # It is still possible to ignore it from the plugin: # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_define_constant(indexer, definition) - # definition.ignored! if definition.name == "FOO" + # def on_define_constant(definition) + # @index.ignore(definition) if definition.name == "FOO" # end # end # ~~~ - sig { params(indexer: Indexer, definition: Definition).void } - def on_define_constant(indexer, definition) + sig { params(definition: Model::Constant).void } + def on_define_constant(definition) # no-op end # Do not override this method, use `on_define_constant` instead. - sig { params(indexer: Indexer, definition: Definition).void } - def internal_on_define_constant(indexer, definition) - definition.ignored! if ignored_constant_name?(definition.name) + sig { params(definition: Model::Constant).void } + def internal_on_define_constant(definition) + @index.ignore(definition) if ignored_constant_name?(definition.name) - on_define_constant(indexer, definition) + on_define_constant(definition) end # Called when a method is defined. # # Will be called when the indexer processes a `def` or `defs` node. # Note that when this method is called, the definition for the node has already been added to the index. # It is still possible to ignore it from the plugin: # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_define_method(indexer, definition) - # super # So the `ignore_method_names` DSL is still applied - # - # definition.ignored! if definition.name == "foo" + # def on_define_method(definition) + # @index.ignore(definition) if definition.name == "foo" # end # end # ~~~ - sig { params(indexer: Indexer, definition: Definition).void } - def on_define_method(indexer, definition) + sig { params(definition: Model::Method).void } + def on_define_method(definition) # no-op end # Do not override this method, use `on_define_method` instead. - sig { params(indexer: Indexer, definition: Definition).void } - def internal_on_define_method(indexer, definition) - definition.ignored! if ignored_method_name?(definition.name) + sig { params(definition: Model::Method).void } + def internal_on_define_method(definition) + @index.ignore(definition) if ignored_method_name?(definition.name) - on_define_method(indexer, definition) + on_define_method(definition) end # Called when a module is defined. # # Will be called when the indexer processes a `module` node. # Note that when this method is called, the definition for the node has already been added to the index. # It is still possible to ignore it from the plugin: # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_define_module(indexer, definition) - # definition.ignored! if definition.name == "Foo" + # def on_define_module(definition) + # @index.ignore(definition) if definition.name == "Foo" # end # end # ~~~ - sig { params(indexer: Indexer, definition: Definition).void } - def on_define_module(indexer, definition) + sig { params(definition: Model::Module).void } + def on_define_module(definition) # no-op end # Do not override this method, use `on_define_module` instead. - sig { params(indexer: Indexer, definition: Definition).void } - def internal_on_define_module(indexer, definition) - definition.ignored! if ignored_module_name?(definition.name) + sig { params(definition: Model::Module).void } + def internal_on_define_module(definition) + @index.ignore(definition) if ignored_module_name?(definition.name) - on_define_module(indexer, definition) + on_define_module(definition) end # Called when a send is being processed # # ~~~rb # class MyPlugin < Spoom::Deadcode::Plugins::Base - # def on_send(indexer, send) + # def on_send(send) # return unless send.name == "dsl_method" # return if send.args.empty? # # method_name = send.args.first.slice.delete_prefix(":") - # indexer.reference_method(method_name, send.node) + # @index.reference_method(method_name, send.node, send.loc) # end # end # ~~~ - sig { params(indexer: Indexer, send: Send).void } - def on_send(indexer, send) + sig { params(send: Send).void } + def on_send(send) # no-op end - # Do not override this method, use `on_send` instead. - sig { params(indexer: Indexer, send: Send).void } - def internal_on_send(indexer, send) - on_send(indexer, send) - end - private # DSL support + sig { params(definition: Model::Namespace, superclass_name: String).returns(T::Boolean) } + def subclass_of?(definition, superclass_name) + superclass_symbol = @index.model.symbols[superclass_name] + return false unless superclass_symbol + + @index.model.symbols_hierarchy.edge?(definition.symbol, superclass_symbol) + end + sig { params(name: T.nilable(String)).returns(T::Boolean) } def ignored_class_name?(name) return false unless name ignored_name?(name, :@ignored_class_names, :@ignored_class_patterns) end - sig { params(superclass_name: T.nilable(String)).returns(T::Boolean) } - def ignored_subclass?(superclass_name) - return false unless superclass_name + sig { params(definition: Model::Class).returns(T::Boolean) } + def ignored_subclass?(definition) + superclass_name = definition.superclass_name + return true if superclass_name && ignored_name?( + superclass_name, + :@ignored_subclasses_of_names, + :@ignored_subclasses_of_patterns, + ) - ignored_name?(superclass_name, :@ignored_subclasses_of_names, :@ignored_subclasses_of_patterns) + names(:@ignored_subclasses_of_names).any? { |superclass_name| subclass_of?(definition, superclass_name) } end sig { params(name: String).returns(T::Boolean) } def ignored_constant_name?(name) ignored_name?(name, :@ignored_constant_names, :@ignored_constant_patterns)