Skip to content

Commit

Permalink
Pass index to enhancement's constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Oct 22, 2024
1 parent 1b2b113 commit 41eac73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/declaration_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down
11 changes: 7 additions & 4 deletions lib/ruby_indexer/lib/ruby_indexer/enhancement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
22 changes: 11 additions & 11 deletions lib/ruby_indexer/test/enhancements_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 41eac73

Please sign in to comment.