Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support on_call_node_leave events in index enhancement #2754

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 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,9 +315,11 @@ def on_call_node_enter(node)
end

@enhancements.each do |enhancement|
enhancement.on_call_node(@index, @owner_stack.last, node, @file_path, @code_units_cache)
enhancement.on_call_node_enter(@index, @owner_stack.last, node, @file_path, @code_units_cache)
rescue StandardError => e
@indexing_errors << "Indexing error in #{@file_path} with '#{enhancement.class.name}' enhancement: #{e.message}"
@indexing_errors << <<~MSG
Indexing error in #{@file_path} with '#{enhancement.class.name}' on call node enter enhancement: #{e.message}
MSG
end
end

Expand All @@ -332,6 +334,14 @@ def on_call_node_leave(node)
@visibility_stack.pop
end
end

@enhancements.each do |enhancement|
enhancement.on_call_node_leave(@index, @owner_stack.last, node, @file_path, @code_units_cache)
st0012 marked this conversation as resolved.
Show resolved Hide resolved
rescue StandardError => e
@indexing_errors << <<~MSG
Indexing error in #{@file_path} with '#{enhancement.class.name}' on call node leave enhancement: #{e.message}
MSG
end
end

sig { params(node: Prism::DefNode).void }
Expand Down
16 changes: 15 additions & 1 deletion lib/ruby_indexer/lib/ruby_indexer/enhancement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ module Enhancement
),
).void
end
def on_call_node(index, owner, node, file_path, code_units_cache); end
st0012 marked this conversation as resolved.
Show resolved Hide resolved
def on_call_node_enter(index, owner, node, file_path, code_units_cache); end

sig do
abstract.params(
index: Index,
owner: T.nilable(Entry::Namespace),
node: Prism::CallNode,
file_path: String,
code_units_cache: T.any(
T.proc.params(arg0: Integer).returns(Integer),
Prism::CodeUnitsCache,
),
).void
end
def on_call_node_leave(index, owner, node, file_path, code_units_cache); end
end
end
66 changes: 61 additions & 5 deletions lib/ruby_indexer/test/enhancements_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_enhancing_indexing_included_hook
enhancement_class = Class.new do
include Enhancement

def on_call_node(index, owner, node, file_path, code_units_cache)
def on_call_node_enter(index, owner, node, file_path, code_units_cache)
return unless owner
return unless node.name == :extend

Expand Down Expand Up @@ -48,6 +48,10 @@ def on_call_node(index, owner, node, file_path, code_units_cache)
# Do nothing
end
end

def on_call_node_leave(index, owner, node, file_path, code_units_cache)
# Do nothing
end
end

@index.register_enhancement(enhancement_class.new)
Expand Down Expand Up @@ -101,7 +105,7 @@ def test_enhancing_indexing_configuration_dsl
enhancement_class = Class.new do
include Enhancement

def on_call_node(index, owner, node, file_path, code_units_cache)
def on_call_node_enter(index, owner, node, file_path, code_units_cache)
return unless owner

name = node.name
Expand All @@ -126,6 +130,10 @@ def on_call_node(index, owner, node, file_path, code_units_cache)
owner,
))
end

def on_call_node_leave(index, owner, node, file_path, code_units_cache)
# Do nothing
end
end

@index.register_enhancement(enhancement_class.new)
Expand Down Expand Up @@ -160,14 +168,18 @@ class User < ActiveRecord::Base
assert_entry("posts", Entry::Method, "/fake/path/foo.rb:23-11:23-17")
end

def test_error_handling_in_enhancement
def test_error_handling_in_on_call_node_enter_enhancement
enhancement_class = Class.new do
include Enhancement

def on_call_node(index, owner, node, file_path, code_units_cache)
def on_call_node_enter(index, owner, node, file_path, code_units_cache)
raise "Error"
end

def on_call_node_leave(index, owner, node, file_path, code_units_cache)
# Do nothing
end

class << self
def name
"TestEnhancement"
Expand All @@ -189,7 +201,51 @@ def self.extended(base)
RUBY
end

assert_match(%r{Indexing error in /fake/path/foo\.rb with 'TestEnhancement' enhancement}, stderr)
assert_match(
%r{Indexing error in /fake/path/foo\.rb with 'TestEnhancement' on call node enter enhancement},
stderr,
)
# The module should still be indexed
assert_entry("ActiveSupport::Concern", Entry::Module, "/fake/path/foo.rb:1-2:5-5")
end

def test_error_handling_in_on_call_node_leave_enhancement
enhancement_class = Class.new do
include Enhancement

def on_call_node_enter(index, owner, node, file_path, code_units_cache)
# Do nothing
end

def on_call_node_leave(index, owner, node, file_path, code_units_cache)
raise "Error"
end

class << self
def name
"TestEnhancement"
end
end
end

@index.register_enhancement(enhancement_class.new)

_stdout, stderr = capture_io do
index(<<~RUBY)
module ActiveSupport
module Concern
def self.extended(base)
base.class_eval("def new_method(a); end")
end
end
end
RUBY
end

assert_match(
%r{Indexing error in /fake/path/foo\.rb with 'TestEnhancement' on call node leave enhancement},
stderr,
)
# The module should still be indexed
assert_entry("ActiveSupport::Concern", Entry::Module, "/fake/path/foo.rb:1-2:5-5")
end
Expand Down
Loading