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

Remove source location #123

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
7 changes: 3 additions & 4 deletions lib/pry-rescue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require File.expand_path('../pry-rescue/commands', __FILE__)
require File.expand_path('../pry-rescue/rack', __FILE__)
require File.expand_path('../pry-rescue/peek.rb', __FILE__)
require File.expand_path('../pry-rescue/source_location.rb', __FILE__)

if ENV['PRY_RESCUE_RAILS']
require File.expand_path('../pry-rescue/rails', __FILE__)
Expand Down Expand Up @@ -104,7 +103,7 @@ def in_exception_context?
# @param [Exception] e The raised exception
def phantom_load_raise?(e)
bindings = e.instance_variable_get(:@rescue_bindings)
bindings.any? && SourceLocation.call(bindings.first)[0] == __FILE__
bindings.any? && bindings.first.source_location[0] == __FILE__
end

# When using pry-stack-explorer we want to start the rescue session outside of gems
Expand All @@ -114,7 +113,7 @@ def phantom_load_raise?(e)
# @return [Fixnum] The offset of the first binding of user code
def initial_frame(bindings)
bindings.each_with_index do |binding, i|
return i if user_path?(SourceLocation.call(binding)[0])
return i if user_path?(binding.source_location[0])
end

0
Expand Down Expand Up @@ -171,7 +170,7 @@ def stdlib_path?(file)
def without_bindings_below_raise(bindings)
return bindings if bindings.size <= 1
bindings.drop_while do |b|
SourceLocation.call(b)[0] == File.expand_path("../pry-rescue/core_ext.rb", __FILE__)
b.source_location[0] == File.expand_path("../pry-rescue/core_ext.rb", __FILE__)
end.drop_while do |b|
Interception == b.eval("self")
end
Expand Down
22 changes: 12 additions & 10 deletions lib/pry-rescue/source_location.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
class PryRescue
module SourceLocation
DEPRECATION_TIME = Time.new(2021,4,1)

WithRuby2_5 = ->(b){ [b.eval("__FILE__"), b.eval("__LINE__")] }
WithRuby2_6 = ->(b){ b.source_location }
if binding.respond_to?(:source_location)
raise 'source_location exists by default in Ruby 2.6 and greater, no need to required it manually'
else
class PryRescue
module SourceLocation
def self.call(b)
[b.eval("__FILE__"), b.eval("__LINE__")]
end
end
end

define_singleton_method(
:call,
(RUBY_VERSION < "2.6.0") ? WithRuby2_5 : WithRuby2_6
)
Binding.define_method :source_location do
PryRescue::SourceLocation.call(self)
end
end
4 changes: 1 addition & 3 deletions spec/pry_rescue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
describe "PryRescue.load" do
before :all do
if !binding.respond_to?(:source_location)
Binding.define_method :source_location do
PryRescue::SourceLocation.call(self)
end
require 'pry-rescue/source_location'
end
end

Expand Down
30 changes: 18 additions & 12 deletions spec/source_location_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
describe PryRescue::SourceLocation do
describe ".call" do
subject{ described_class.call(binding) }
describe 'PryRescue::SourceLocation' do
if RUBY_VERSION < "2.6.0"
require 'pry-rescue/source_location'

it "matches [file, line]" do
is_expected.to match([__FILE__, be_between(2,30)])
end
end
subject { binding.source_location }

it "will be removed when Ruby 2.5 is EOL" do
time = Time.now

if time >= described_class::DEPRECATION_TIME
it 'matches [file, line]' do
is_expected.to match([__FILE__, be_between(2, 30)])
end
else
it 'will be removed when Ruby 2.5 is EOL' do
expect(
defined?(PryRescue::SourceLocation)
).to be false
).to be_falsey
end

it 'should raise an error when people upgrade' do
expect {
require 'pry-rescue/source_location'
}.to raise_error(RuntimeError, /source_location exists by default in Ruby 2.6/)

expect(defined?(PryRescue::SourceLocation)).to be_falsey
end
end
end