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

rubocop: fix offences of the Style/MethodMissingSuper cop

This commit is contained in:
Kyrylo Silin 2019-03-23 15:50:57 +02:00
parent 082691eb90
commit 0c0b171486
10 changed files with 57 additions and 39 deletions

View file

@ -197,14 +197,3 @@ Style/FormatStringToken:
Style/GlobalVars: Style/GlobalVars:
Exclude: Exclude:
- 'spec/commands/edit_spec.rb' - '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'

View file

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

View file

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

View file

@ -493,7 +493,11 @@ class Pry
# Delegate any unknown calls to the wrapped method. # Delegate any unknown calls to the wrapped method.
def method_missing(method_name, *args, &block) 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 end
def respond_to_missing?(method_name, include_private = false) def respond_to_missing?(method_name, include_private = false)

View file

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

View file

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

View file

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

View file

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

View file

@ -146,7 +146,11 @@ class Pry
# Forward method invocations to the wrapped module # Forward method invocations to the wrapped module
def method_missing(method_name, *args, &block) 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 end
def respond_to_missing?(method_name, include_private = false) def respond_to_missing?(method_name, include_private = false)

View file

@ -501,9 +501,17 @@ RSpec.describe Pry::Code do
end end
describe "#method_missing" do describe "#method_missing" do
it "forwards any missing methods to the output of '#to_s'" do context "when a String responds to the given method" do
expect(subject).to receive_message_chain(:to_s, :send) it "forwards the method to a String instance" do
subject.abcdefg 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
end end