1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Merge pull request #2031 from pry/warning-module

Add Pry::Warning
This commit is contained in:
Kyrylo Silin 2019-05-04 17:51:14 +03:00 committed by GitHub
commit e1ac530b02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 14 deletions

View file

@ -25,6 +25,7 @@ require 'pry/exception_handler'
require 'pry/system_command_handler'
require 'pry/control_d_handler'
require 'pry/command_state'
require 'pry/warning'
Pry::Commands = Pry::CommandSet.new unless defined?(Pry::Commands)

View file

@ -288,11 +288,7 @@ class Pry
end
def _pry_
loc = caller_locations(1..1).first
warn(
"#{loc.path}:#{loc.lineno}: warning: _pry_ is deprecated, use " \
"pry_instance instead"
)
Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead')
pry_instance
end

View file

@ -132,19 +132,17 @@ class Pry
# @deprecated Use a `Pry::Prompt` instance directly
def [](key)
key = key.to_s
loc = caller_locations(1..1).first
if %w[name description].include?(key)
warn(
"#{loc.path}:#{loc.lineno}: warning: `Pry::Prompt[:#{@name}][:#{key}]` " \
"is deprecated. Use `#{self.class}##{key}` instead"
Pry::Warning.warn(
"`Pry::Prompt[:#{@name}][:#{key}]` is deprecated. " \
"Use `#{self.class}##{key}` instead"
)
public_send(key)
elsif key.to_s == 'value'
warn(
"#{loc.path}:#{loc.lineno}: warning: `#{self.class}[:#{@name}][:value]` " \
"is deprecated. Use `#{self.class}#prompt_procs` instead or an " \
"instance of `#{self.class}` directly"
Pry::Warning.warn(
"`#{self.class}[:#{@name}][:value]` is deprecated. Use " \
"`#{self.class}#prompt_procs` instead or an instance of " \
"`#{self.class}` directly"
)
@prompt_procs
end

25
lib/pry/warning.rb Normal file
View file

@ -0,0 +1,25 @@
class Pry
# @api private
# @since ?.?.?
module Warning
# Prints a warning message with exact file and line location, similar to how
# Ruby's -W prints warnings.
#
# @param [String] message
# @return [void]
def self.warn(message)
if Kernel.respond_to?(:caller_locations)
location = caller_locations(1..1).first
path = location.path
lineno = location.lineno
else
# Ruby 1.9.3 support.
frame = caller.first.split(':') # rubocop:disable Performance/Caller
path = frame.first
lineno = frame[1]
end
Kernel.warn("#{path}:#{lineno}: warning: #{message}")
end
end
end

10
spec/warning_spec.rb Normal file
View file

@ -0,0 +1,10 @@
RSpec.describe Pry::Warning do
describe "#warn" do
it "prints a warning with file and line" do
expect(Kernel).to receive(:warn).with(
"#{__FILE__}:#{__LINE__ + 2}: warning: foo bar"
)
described_class.warn('foo bar')
end
end
end