From 659b09e67c69068170f5b45d6561ddf8120d8835 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 27 Jul 2023 12:12:34 +0100 Subject: [PATCH] Return nil if is not an actual file path --- lib/irb/cmd/edit.rb | 2 +- lib/irb/cmd/show_source.rb | 2 +- lib/irb/source_finder.rb | 9 ++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/irb/cmd/edit.rb b/lib/irb/cmd/edit.rb index 669b96b3f..523a1dac5 100644 --- a/lib/irb/cmd/edit.rb +++ b/lib/irb/cmd/edit.rb @@ -37,7 +37,7 @@ def execute(*args) # in this case, we should just ignore the error end - if source && File.exist?(source.file) + if source path = source.file else puts "Can not find file: #{path}" diff --git a/lib/irb/cmd/show_source.rb b/lib/irb/cmd/show_source.rb index 5db1d151b..10463ebf0 100644 --- a/lib/irb/cmd/show_source.rb +++ b/lib/irb/cmd/show_source.rb @@ -29,7 +29,7 @@ def execute(str = nil) source = SourceFinder.new(@irb_context).find_source(str) - if source && File.exist?(source.file) + if source show_source(source) else puts "Error: Couldn't locate a definition for #{str}" diff --git a/lib/irb/source_finder.rb b/lib/irb/source_finder.rb index b20039b81..688ee9fd3 100644 --- a/lib/irb/source_finder.rb +++ b/lib/irb/source_finder.rb @@ -34,16 +34,15 @@ def find_source(signature) method = Regexp.last_match[:method] file, line = receiver.method(method).source_location if receiver.respond_to?(method, true) end - if file && line - Source.new(file: file, first_line: line, last_line: find_end(file, line, @irb_context)) + if file && line && File.exist?(file) + Source.new(file: file, first_line: line, last_line: find_end(file, line)) end end private - def find_end(file, first_line, irb_context) - return first_line unless File.exist?(file) - lex = RubyLex.new(irb_context) + def find_end(file, first_line) + lex = RubyLex.new(@irb_context) lines = File.read(file).lines[(first_line - 1)..-1] tokens = RubyLex.ripper_lex_without_warning(lines.join) prev_tokens = []