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
119 changes: 44 additions & 75 deletions lib/prism/node_find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,17 @@ 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
when Proc, Method, UnboundMethod
if defined?(::RubyVM)
RubyVMCallableFind.new.find(callable)
elsif callable.lambda?
LineLambdaFind.new.find(callable)
else
LineProcFind.new.find(callable)
end
when Method, UnboundMethod
if rubyvm
RubyVMCallableFind.new.find(callable)
else
LineMethodFind.new.find(callable)
LineCallableFind.new.find(callable)
end
when Thread::Backtrace::Location
if rubyvm
if defined?(::RubyVM)
RubyVMBacktraceLocationFind.new.find(callable)
else
LineBacktraceLocationFind.new.find(callable)
Expand Down Expand Up @@ -91,77 +83,54 @@ def find(location)
end
end

# Finds the AST node for a Method or UnboundMethod using best-effort line
# Finds the AST node for a Method, UnboundMethod, or Proc using best-effort line
# matching. Used on non-CRuby implementations.
class LineMethodFind < Find
class LineCallableFind < Find
# Find the node for the given method by matching on name and line.
#--
#: (Method | UnboundMethod callable) -> Node?
def find(callable)
return unless (source_location = callable.source_location)
return unless (result = parse_file(source_location[0]))

name = callable.name
start_line = source_location[1]

result.value.find do |node|
case node
when DefNode
node.name == name && node.location.start_line == start_line
when CallNode
node.block.is_a?(BlockNode) && node.location.start_line == start_line
else
false
end
end
end
end

# Finds the AST node for a lambda using best-effort line matching. Used
# on non-CRuby implementations.
class LineLambdaFind < Find
# Find the node for the given lambda by matching on line.
#--
#: (Proc callable) -> Node?
#: (Method | UnboundMethod | Proc callable) -> Node?
def find(callable)
return unless (source_location = callable.source_location)
return unless (result = parse_file(source_location[0]))

start_line = source_location[1]

result.value.find do |node|
case node
when LambdaNode
node.location.start_line == start_line
when CallNode
node.block.is_a?(BlockNode) && node.location.start_line == start_line
else
false
case callable
when Method, UnboundMethod
name = callable.name

result.value.find do |node|
case node
when DefNode
node.name == name && node.location.start_line == start_line
when CallNode
node.block.is_a?(BlockNode) && node.location.start_line == start_line
else
false
end
end
end
end
end

# Finds the AST node for a non-lambda Proc using best-effort line
# matching. Used on non-CRuby implementations.
class LineProcFind < Find
# Find the node for the given proc by matching on line.
#--
#: (Proc callable) -> Node?
def find(callable)
return unless (source_location = callable.source_location)
return unless (result = parse_file(source_location[0]))

start_line = source_location[1]

result.value.find do |node|
case node
when ForNode
node.location.start_line == start_line
when CallNode
node.block.is_a?(BlockNode) && node.location.start_line == start_line
when Proc
if callable.lambda?
result.value.find do |node|
case node
when LambdaNode
node.location.start_line == start_line
when CallNode
node.block.is_a?(BlockNode) && node.location.start_line == start_line
else
false
end
end
else
false
result.value.find do |node|
case node
when ForNode
node.location.start_line == start_line
when CallNode
node.block.is_a?(BlockNode) && node.location.start_line == start_line
else
false
end
end
end
end
end
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.

24 changes: 4 additions & 20 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.

30 changes: 6 additions & 24 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::LineCallableFind.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::LineCallableFind.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::LineCallableFind.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::LineCallableFind.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::LineCallableFind.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::LineCallableFind.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