Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions lib/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def self.load(source, serialized, freeze = false)
# an exact match. On other implementations, it falls back to best-effort
# matching by source location line number.
#--
#: (Method | UnboundMethod | Proc | Thread::Backtrace::Location callable, ?rubyvm: bool) -> Node?
def self.find(callable, rubyvm: !!defined?(RubyVM))
NodeFind.find(callable, rubyvm)
#: (Method | UnboundMethod | Proc | Thread::Backtrace::Location callable) -> Node?
def self.find(callable)
NodeFind.find(callable)
end

# @rbs!
Expand Down
10 changes: 5 additions & 5 deletions lib/prism/node_find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ module Prism
module NodeFind # :nodoc:
# Find the node for the given callable or backtrace location.
#--
#: (Method | UnboundMethod | Proc | Thread::Backtrace::Location callable, bool rubyvm) -> Node?
def self.find(callable, rubyvm)
#: (Method | UnboundMethod | Proc | Thread::Backtrace::Location callable) -> Node?
def self.find(callable)
case callable
when Proc
if rubyvm
if defined?(::RubyVM)
RubyVMCallableFind.new.find(callable)
elsif callable.lambda?
LineLambdaFind.new.find(callable)
else
LineProcFind.new.find(callable)
end
when Method, UnboundMethod

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer these to stay as separate classes, as the point is that you only have to switch on the callable in one place.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It also makes the diff a lot smaller.

if rubyvm
if defined?(::RubyVM)
RubyVMCallableFind.new.find(callable)
else
LineMethodFind.new.find(callable)
end
when Thread::Backtrace::Location
if rubyvm
if defined?(::RubyVM)
RubyVMBacktraceLocationFind.new.find(callable)
else
LineBacktraceLocationFind.new.find(callable)
Expand Down
4 changes: 2 additions & 2 deletions rbi/generated/prism.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions rbi/generated/prism/node_find.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sig/generated/prism.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sig/generated/prism/node_find.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/prism/ruby/find_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,42 +143,42 @@ def test_multiple_methods_on_same_line
assert_def_node Prism.find(Fixtures::MultipleOnLine.method(:second)), :second
end

# === Fallback (line-based) tests via rubyvm: false ===
# === Fallback (line-based) tests ===

def test_fallback_simple_method
assert_def_node Prism.find(Fixtures::Methods.instance_method(:simple_method), rubyvm: false), :simple_method
assert_def_node NodeFind::LineMethodFind.new.find(Fixtures::Methods.instance_method(:simple_method)), :simple_method
end

def test_fallback_singleton_method
assert_def_node Prism.find(Fixtures::Methods.method(:singleton_method_fixture), rubyvm: false), :singleton_method_fixture
assert_def_node NodeFind::LineMethodFind.new.find(Fixtures::Methods.method(:singleton_method_fixture)), :singleton_method_fixture
end

def test_fallback_lambda
node = Prism.find(Fixtures::Procs::SIMPLE_LAMBDA, rubyvm: false)
node = NodeFind::LineLambdaFind.new.find(Fixtures::Procs::SIMPLE_LAMBDA)
assert_instance_of LambdaNode, node
end

def test_fallback_proc
node = Prism.find(Fixtures::Procs::SIMPLE_PROC, rubyvm: false)
node = NodeFind::LineProcFind.new.find(Fixtures::Procs::SIMPLE_PROC)
assert_instance_of CallNode, node
assert node.block.is_a?(BlockNode)
end

def test_fallback_define_method
node = Prism.find(Fixtures::DefineMethod.instance_method(:dynamic), rubyvm: false)
node = NodeFind::LineMethodFind.new.find(Fixtures::DefineMethod.instance_method(:dynamic))
assert_instance_of CallNode, node
assert node.block.is_a?(BlockNode)
end

def test_fallback_for_loop
node = Prism.find(Fixtures::ForLoop::FOR_PROC, rubyvm: false)
node = NodeFind::LineProcFind.new.find(Fixtures::ForLoop::FOR_PROC)
assert_instance_of ForNode, node
end

def test_fallback_backtrace_location
location = zero_division_location
assert_not_nil location
node = Prism.find(location, rubyvm: false)
node = NodeFind::LineBacktraceLocationFind.new.find(location)
assert_not_nil node
assert_equal location.lineno, node.location.start_line
end
Expand Down
Loading