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

Merge pull request #1993 from pry/rubocop-fixes

Various Rubocop fixes
This commit is contained in:
Kyrylo Silin 2019-03-23 17:48:05 +02:00 committed by GitHub
commit 6622549184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 97 additions and 99 deletions

View file

@ -18,10 +18,6 @@ Lint/ShadowedException:
Exclude:
- 'lib/pry/method.rb'
# Offense count: 32
Lint/UnderscorePrefixedVariableName:
Enabled: false
# Offense count: 1
# Configuration parameters: CheckForMethodsWithNoSideEffects.
Lint/Void:
@ -186,45 +182,3 @@ Style/EvalWithLocation:
- 'spec/commands/edit_spec.rb'
- 'spec/commands/watch_expression_spec.rb'
- 'spec/method_spec.rb'
# Offense count: 3
# Configuration parameters: EnforcedStyle.
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
Exclude:
- 'lib/pry/commands/ls/local_vars.rb'
- 'lib/pry/config/behavior.rb'
- 'lib/pry/pry_class.rb'
# Offense count: 2
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'spec/commands/edit_spec.rb'
# Offense count: 7
Style/MethodMissingSuper:
Exclude:
- 'lib/pry/code.rb'
- 'lib/pry/config/behavior.rb'
- 'lib/pry/method.rb'
- 'lib/pry/method/disowned.rb'
- 'lib/pry/output.rb'
- 'lib/pry/plugins.rb'
- 'lib/pry/wrapped_module.rb'
# Offense count: 4
Style/MissingRespondToMissing:
Exclude:
- 'lib/pry/method.rb'
- 'lib/pry/method/disowned.rb'
- 'lib/pry/plugins.rb'
- 'lib/pry/wrapped_module.rb'
# Offense count: 9
# Cop supports --auto-correct.
Style/PerlBackrefs:
Exclude:
- 'lib/pry/input_completer.rb'
- 'lib/pry/last_exception.rb'
- 'lib/pry/method.rb'

View file

@ -331,14 +331,18 @@ class Pry
end
# Forward any missing methods to the output of `#to_s`.
def method_missing(name, *args, &block)
to_s.send(name, *args, &block)
def method_missing(method_name, *args, &block)
if (string = to_s).respond_to?(method_name)
string.__send__(method_name, *args, &block)
else
super
end
end
undef =~
# Check whether String responds to missing methods.
def respond_to_missing?(name, include_all = false)
''.respond_to?(name, include_all)
def respond_to_missing?(method_name, include_private = false)
''.respond_to?(method_name, include_private) || super
end
if RUBY_VERSION.start_with?('1.9')

View file

@ -143,10 +143,10 @@ class Pry
def check_for_juxtaposed_replay(replay_sequence)
if replay_sequence =~ /\Ahist(?:ory)?\b/
# Create *fresh* instance of Options for parsing of "hist" command.
_slop = slop
_slop.parse replay_sequence.split(' ')[1..-1]
slop_instance = slop
slop_instance.parse(replay_sequence.split(' ')[1..-1])
if _slop.present?(:r)
if slop_instance.present?(:r)
replay_sequence = replay_sequence.split("\n").join('; ')
index = opts[:r]
index = index.min if index.min == index.max || index.max.nil?

View file

@ -33,7 +33,11 @@ class Pry
colorized_lhs = color(:local_var, lhs)
color_escape_padding = colorized_lhs.size - lhs.size
pad = desired_width + color_escape_padding
Kernel.format("%-#{pad}s = %s", color(:local_var, colorized_lhs), rhs)
Kernel.format(
"%-#{pad}<name>s = %<value>s",
name: color(:local_var, colorized_lhs),
value: rhs
)
end
end
end

View file

@ -317,18 +317,20 @@ class Pry
q.text inspect[1..-1].gsub(INSPECT_REGEXP, "default=<")
end
def method_missing(name, *args, &block)
key = name.to_s
if key[-1] == ASSIGNMENT
short_key = key[0..-2]
self[short_key] = args[0]
elsif key?(key)
self[key]
elsif @default.respond_to?(name)
value = @default.public_send(name, *args, &block)
self[key] = __dup(value)
# rubocop:disable Style/MethodMissingSuper
def method_missing(method_name, *args, &block)
name = method_name.to_s
if name[-1] == ASSIGNMENT
short_name = name[0..-2]
self[short_name] = args[0]
elsif key?(name)
self[name]
elsif @default.respond_to?(method_name)
value = @default.public_send(method_name, *args, &block)
self[name] = __dup(value)
end
end
# rubocop:enable Style/MethodMissingSuper
def respond_to_missing?(key, include_all = false)
key = key.to_s.chomp(ASSIGNMENT)
@ -338,7 +340,7 @@ class Pry
private
def __clip_inspect(obj)
format("#{obj.class}:0x%x", obj.object_id)
format("#{obj.class}:0x%<id>x", id: obj.object_id)
end
def __try_convert_to_hash(obj)

View file

@ -147,7 +147,7 @@ class Pry
end
select_message(path, receiver, message, candidates)
when GLOBALVARIABLE_REGEXP # global
regmessage = Regexp.new(Regexp.quote($1))
regmessage = Regexp.new(Regexp.quote(Regexp.last_match(1)))
candidates = global_variables.collect(&:to_s).grep(regmessage)
when VARIABLE_REGEXP # variable
receiver = Regexp.last_match(1)

View file

@ -49,7 +49,7 @@ class Pry
def bt_source_location_for(index)
backtrace[index] =~ /(.*):(\d+)/
[$1, $2.to_i]
[::Regexp.last_match(1), ::Regexp.last_match(2).to_i]
end
def inc_bt_index

View file

@ -41,16 +41,16 @@ class Pry
if name.nil?
nil
elsif name.to_s =~ /(.+)\#(\S+)\Z/
context = $1
meth_name = $2
context = Regexp.last_match(1)
meth_name = Regexp.last_match(2)
from_module(target.eval(context), meth_name, target)
elsif name.to_s =~ /(.+)(\[\])\Z/
context = $1
meth_name = $2
context = Regexp.last_match(1)
meth_name = Regexp.last_match(2)
from_obj(target.eval(context), meth_name, target)
elsif name.to_s =~ /(.+)(\.|::)(\S+)\Z/
context = $1
meth_name = $3
context = Regexp.last_match(1)
meth_name = Regexp.last_match(3)
from_obj(target.eval(context), meth_name, target)
elsif options[:instance]
from_module(target.eval("self"), name, target)
@ -493,7 +493,15 @@ class Pry
# Delegate any unknown calls to the wrapped method.
def method_missing(method_name, *args, &block)
@method.send(method_name, *args, &block)
if @method.respond_to?(method_name)
@method.__send__(method_name, *args, &block)
else
super
end
end
def respond_to_missing?(method_name, include_private = false)
@method.respond_to?(method_name) || super
end
def comment

View file

@ -46,13 +46,19 @@ class Pry
end
# Raise a more useful error message instead of trying to forward to nil.
def method_missing(meth_name, *args, &block)
if method(:name).respond_to?(meth_name)
raise "Cannot call '#{meth_name}' on an undef'd method."
# rubocop:disable Style/MethodMissingSuper
def method_missing(method_name, *args, &block)
if method(:name).respond_to?(method_name)
raise "Cannot call '#{method_name}' on an undef'd method."
end
Object.instance_method(:method_missing).bind(self)
.call(meth_name, *args, &block)
method = Object.instance_method(:method_missing).bind(self)
method.call(method_name, *args, &block)
end
# rubocop:enable Style/MethodMissingSuper
def respond_to_missing?(method_name, include_private = false)
!method(:name).respond_to?(method_name) || super
end
end
end

View file

@ -33,8 +33,12 @@ class Pry
@boxed_io.respond_to?(:tty?) && @boxed_io.tty?
end
def method_missing(name, *args, &block)
@boxed_io.__send__(name, *args, &block)
def method_missing(method_name, *args, &block)
if @boxed_io.respond_to?(method_name)
@boxed_io.__send__(method_name, *args, &block)
else
super
end
end
def respond_to_missing?(m, include_all = false)

View file

@ -8,8 +8,13 @@ class Pry
@name = name
end
def method_missing(*_args)
def method_missing(*)
warn "Warning: The plugin '#{@name}' was not found! (no gem found)"
super
end
def respond_to_missing?(*)
false
end
end

View file

@ -250,8 +250,10 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
elsif Pry.config.prompt_safe_contexts.any? { |v| v === obj } &&
obj.inspect.length <= max
obj.inspect
elsif id
format("#<#{obj.class}:0x%<id>x>", id: obj.object_id << 1)
else
id ? format("#<#{obj.class}:0x%x>", (obj.object_id << 1)) : "#<#{obj.class}>"
"#<#{obj.class}>"
end
rescue RescuableException
"unknown"

View file

@ -567,7 +567,7 @@ class Pry
unless @prompt_warn
@prompt_warn = true
output.warn(
warn(
"warning: setting prompt with help of " \
"`Pry.config.prompt = [proc {}, proc {}]` is deprecated. " \
"Use Pry::Prompt API instead"

View file

@ -34,8 +34,8 @@ class Pry
# @param [Pry] pry_instance the Pry instance to make non-interactive.
def non_interactive_mode(pry_instance, content)
pry_instance.print = proc {}
pry_instance.exception_handler = proc do |o, _e, _p_|
_p_.run_command "cat --ex"
pry_instance.exception_handler = proc do |o, _e, p|
p.run_command "cat --ex"
o.puts "...exception encountered, going interactive!"
interactive_mode(pry_instance)
end

View file

@ -146,11 +146,15 @@ class Pry
# Forward method invocations to the wrapped module
def method_missing(method_name, *args, &block)
wrapped.send(method_name, *args, &block)
if wrapped.respond_to?(method_name)
wrapped.send(method_name, *args, &block)
else
super
end
end
def respond_to?(method_name, include_all = false)
super || wrapped.respond_to?(method_name, include_all)
def respond_to_missing?(method_name, include_private = false)
wrapped.respond_to?(method_name, include_private) || super
end
# Retrieve the source location of a module. Return value is in same

View file

@ -501,9 +501,17 @@ RSpec.describe Pry::Code do
end
describe "#method_missing" do
it "forwards any missing methods to the output of '#to_s'" do
expect(subject).to receive_message_chain(:to_s, :send)
subject.abcdefg
context "when a String responds to the given method" do
it "forwards the method to a String instance" do
expect(subject.upcase).to eq('')
end
end
context "when a String does not respond to the given method" do
it "raises NoMethodError" do
expect { subject.abcdefg }
.to raise_error(NoMethodError, /undefined method `abcdefg'/)
end
end
end

View file

@ -740,14 +740,11 @@ describe "edit" do
end
it "should change the alias, but not the original, without breaking super" do
$x = :bebe
pry_eval 'edit -p X#c'
expect(Pry::Method.from_str("X#c").alias?).to eq true
expect(X.new.b).to eq :kinda
expect(X.new.c).to eq :kindaaa
$x = nil
end
end

View file

@ -437,10 +437,10 @@ describe Pry do
describe "Pry.binding_for" do
it 'should return TOPLEVEL_BINDING if parameter self is main' do
_main_ = -> { TOPLEVEL_BINDING.eval('self') }
expect(Pry.binding_for(_main_.call).is_a?(Binding)).to eq true
expect(Pry.binding_for(_main_.call)).to eq TOPLEVEL_BINDING
expect(Pry.binding_for(_main_.call)).to eq Pry.binding_for(_main_.call)
main = -> { TOPLEVEL_BINDING.eval('self') }
expect(Pry.binding_for(main.call).is_a?(Binding)).to eq true
expect(Pry.binding_for(main.call)).to eq TOPLEVEL_BINDING
expect(Pry.binding_for(main.call)).to eq Pry.binding_for(main.call)
end
end
end