From 41eac73492c82677b750ec5188a40443f68e1a54 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 22 Oct 2024 20:37:05 +0100 Subject: [PATCH] Pass index to enhancement's constructor --- .../lib/ruby_indexer/declaration_listener.rb | 4 ++-- .../lib/ruby_indexer/enhancement.rb | 11 ++++++---- lib/ruby_indexer/test/enhancements_test.rb | 22 +++++++++---------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb b/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb index f5fdf4ac8..9643e89ea 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb @@ -315,7 +315,7 @@ def on_call_node_enter(node) end @enhancements.each do |enhancement| - enhancement.on_call_node_enter(@index, @owner_stack.last, node, @file_path, @code_units_cache) + enhancement.on_call_node_enter(@owner_stack.last, node, @file_path, @code_units_cache) rescue StandardError => e @indexing_errors << <<~MSG Indexing error in #{@file_path} with '#{enhancement.class.name}' on call node enter enhancement: #{e.message} @@ -336,7 +336,7 @@ def on_call_node_leave(node) end @enhancements.each do |enhancement| - enhancement.on_call_node_leave(@index, @owner_stack.last, node, @file_path, @code_units_cache) + enhancement.on_call_node_leave(@owner_stack.last, node, @file_path, @code_units_cache) rescue StandardError => e @indexing_errors << <<~MSG Indexing error in #{@file_path} with '#{enhancement.class.name}' on call node leave enhancement: #{e.message} diff --git a/lib/ruby_indexer/lib/ruby_indexer/enhancement.rb b/lib/ruby_indexer/lib/ruby_indexer/enhancement.rb index f0f11d390..a654b87e8 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/enhancement.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/enhancement.rb @@ -8,12 +8,16 @@ class Enhancement abstract! + sig { params(index: Index).void } + def initialize(index) + @index = index + end + # The `on_extend` indexing enhancement is invoked whenever an extend is encountered in the code. It can be used to # register for an included callback, similar to what `ActiveSupport::Concern` does in order to auto-extend the # `ClassMethods` modules sig do overridable.params( - index: Index, owner: T.nilable(Entry::Namespace), node: Prism::CallNode, file_path: String, @@ -23,11 +27,10 @@ class Enhancement ), ).void end - def on_call_node_enter(index, owner, node, file_path, code_units_cache); end + def on_call_node_enter(owner, node, file_path, code_units_cache); end sig do overridable.params( - index: Index, owner: T.nilable(Entry::Namespace), node: Prism::CallNode, file_path: String, @@ -37,6 +40,6 @@ def on_call_node_enter(index, owner, node, file_path, code_units_cache); end ), ).void end - def on_call_node_leave(index, owner, node, file_path, code_units_cache); end + def on_call_node_leave(owner, node, file_path, code_units_cache); end end end diff --git a/lib/ruby_indexer/test/enhancements_test.rb b/lib/ruby_indexer/test/enhancements_test.rb index e5dd106bd..029bf81b8 100644 --- a/lib/ruby_indexer/test/enhancements_test.rb +++ b/lib/ruby_indexer/test/enhancements_test.rb @@ -7,7 +7,7 @@ module RubyIndexer class EnhancementTest < TestCase def test_enhancing_indexing_included_hook enhancement_class = Class.new(Enhancement) do - def on_call_node_enter(index, owner, node, file_path, code_units_cache) + def on_call_node_enter(owner, node, file_path, code_units_cache) return unless owner return unless node.name == :extend @@ -22,7 +22,7 @@ def on_call_node_enter(index, owner, node, file_path, code_units_cache) module_name = node.full_name next unless module_name == "ActiveSupport::Concern" - index.register_included_hook(owner.name) do |index, base| + @index.register_included_hook(owner.name) do |index, base| class_methods_name = "#{owner.name}::ClassMethods" if index.indexed?(class_methods_name) @@ -31,7 +31,7 @@ def on_call_node_enter(index, owner, node, file_path, code_units_cache) end end - index.add(Entry::Method.new( + @index.add(Entry::Method.new( "new_method", file_path, location, @@ -48,7 +48,7 @@ def on_call_node_enter(index, owner, node, file_path, code_units_cache) end end - @index.register_enhancement(enhancement_class.new) + @index.register_enhancement(enhancement_class.new(@index)) index(<<~RUBY) module ActiveSupport module Concern @@ -97,7 +97,7 @@ class User < ActiveRecord::Base def test_enhancing_indexing_configuration_dsl enhancement_class = Class.new(Enhancement) do - def on_call_node_enter(index, owner, node, file_path, code_units_cache) + def on_call_node_enter(owner, node, file_path, code_units_cache) return unless owner name = node.name @@ -111,7 +111,7 @@ def on_call_node_enter(index, owner, node, file_path, code_units_cache) location = Location.from_prism_location(association_name.location, code_units_cache) - index.add(Entry::Method.new( + @index.add(Entry::Method.new( T.must(association_name.value), file_path, location, @@ -124,7 +124,7 @@ def on_call_node_enter(index, owner, node, file_path, code_units_cache) end end - @index.register_enhancement(enhancement_class.new) + @index.register_enhancement(enhancement_class.new(@index)) index(<<~RUBY) module ActiveSupport module Concern @@ -158,7 +158,7 @@ class User < ActiveRecord::Base def test_error_handling_in_on_call_node_enter_enhancement enhancement_class = Class.new(Enhancement) do - def on_call_node_enter(index, owner, node, file_path, code_units_cache) + def on_call_node_enter(owner, node, file_path, code_units_cache) raise "Error" end @@ -169,7 +169,7 @@ def name end end - @index.register_enhancement(enhancement_class.new) + @index.register_enhancement(enhancement_class.new(@index)) _stdout, stderr = capture_io do index(<<~RUBY) @@ -193,7 +193,7 @@ def self.extended(base) def test_error_handling_in_on_call_node_leave_enhancement enhancement_class = Class.new(Enhancement) do - def on_call_node_leave(index, owner, node, file_path, code_units_cache) + def on_call_node_leave(owner, node, file_path, code_units_cache) raise "Error" end @@ -204,7 +204,7 @@ def name end end - @index.register_enhancement(enhancement_class.new) + @index.register_enhancement(enhancement_class.new(@index)) _stdout, stderr = capture_io do index(<<~RUBY)