From b895f14a5a86d680fb77133e63542e5cda2954f0 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 12:12:22 +0200 Subject: [PATCH 1/6] rubocop: fix offences of the Style/PerlBackrefs cop --- .rubocop_todo.yml | 8 -------- lib/pry/input_completer.rb | 2 +- lib/pry/last_exception.rb | 2 +- lib/pry/method.rb | 12 ++++++------ 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 81669d3f..7af86db9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -220,11 +220,3 @@ Style/MissingRespondToMissing: - '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' diff --git a/lib/pry/input_completer.rb b/lib/pry/input_completer.rb index 5052d5b6..4b2caa8b 100644 --- a/lib/pry/input_completer.rb +++ b/lib/pry/input_completer.rb @@ -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) diff --git a/lib/pry/last_exception.rb b/lib/pry/last_exception.rb index 749b4d1b..0d90434a 100644 --- a/lib/pry/last_exception.rb +++ b/lib/pry/last_exception.rb @@ -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 diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 052db349..49e0612e 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -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) From 0cbaf6d10d1c8456e5690f339065c35b16531edf Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 12:18:50 +0200 Subject: [PATCH 2/6] rubocop: fix offences of the Lint/UnderscorePrefixedVariableName cop --- .rubocop_todo.yml | 4 ---- lib/pry/commands/hist.rb | 6 +++--- lib/pry/repl_file_loader.rb | 4 ++-- spec/pry_spec.rb | 8 ++++---- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7af86db9..bd1f162b 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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: diff --git a/lib/pry/commands/hist.rb b/lib/pry/commands/hist.rb index c6feb73c..69a2331b 100644 --- a/lib/pry/commands/hist.rb +++ b/lib/pry/commands/hist.rb @@ -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? diff --git a/lib/pry/repl_file_loader.rb b/lib/pry/repl_file_loader.rb index ce76dcce..57942119 100644 --- a/lib/pry/repl_file_loader.rb +++ b/lib/pry/repl_file_loader.rb @@ -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 diff --git a/spec/pry_spec.rb b/spec/pry_spec.rb index 17398357..da73c275 100644 --- a/spec/pry_spec.rb +++ b/spec/pry_spec.rb @@ -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 From 082691eb90b33a9deff808af1c09198975e6c106 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 13:56:00 +0200 Subject: [PATCH 3/6] rubocop: fix offences of the Style/MissingRespondToMissing cop --- .rubocop_todo.yml | 8 -------- lib/pry/method.rb | 4 ++++ lib/pry/method/disowned.rb | 4 ++++ lib/pry/plugins.rb | 4 ++++ lib/pry/wrapped_module.rb | 4 ++-- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index bd1f162b..652dcad2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -208,11 +208,3 @@ Style/MethodMissingSuper: - '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' diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 49e0612e..bd3b2a5b 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -496,6 +496,10 @@ class Pry @method.send(method_name, *args, &block) end + def respond_to_missing?(method_name, include_private = false) + @method.respond_to?(method_name) || super + end + def comment Pry::Code.from_file(source_file).comment_describing(source_line) end diff --git a/lib/pry/method/disowned.rb b/lib/pry/method/disowned.rb index 0e7e24a6..de32ee91 100644 --- a/lib/pry/method/disowned.rb +++ b/lib/pry/method/disowned.rb @@ -54,6 +54,10 @@ class Pry Object.instance_method(:method_missing).bind(self) .call(meth_name, *args, &block) end + + def respond_to_missing?(method_name, include_private = false) + !method(:name).respond_to?(method_name) || super + end end end end diff --git a/lib/pry/plugins.rb b/lib/pry/plugins.rb index 7ae25e28..07afec63 100644 --- a/lib/pry/plugins.rb +++ b/lib/pry/plugins.rb @@ -11,6 +11,10 @@ class Pry def method_missing(*_args) warn "Warning: The plugin '#{@name}' was not found! (no gem found)" end + + def respond_to_missing?(*) + false + end end class Plugin diff --git a/lib/pry/wrapped_module.rb b/lib/pry/wrapped_module.rb index ae9770a1..cba13458 100644 --- a/lib/pry/wrapped_module.rb +++ b/lib/pry/wrapped_module.rb @@ -149,8 +149,8 @@ class Pry wrapped.send(method_name, *args, &block) 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 From 0c0b171486cbcea87f0e6bce833a2cbdfde46686 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 15:50:57 +0200 Subject: [PATCH 4/6] rubocop: fix offences of the Style/MethodMissingSuper cop --- .rubocop_todo.yml | 11 ----------- lib/pry/code.rb | 12 ++++++++---- lib/pry/config/behavior.rb | 22 ++++++++++++---------- lib/pry/method.rb | 6 +++++- lib/pry/method/disowned.rb | 12 +++++++----- lib/pry/output.rb | 8 ++++++-- lib/pry/plugins.rb | 3 ++- lib/pry/pry_instance.rb | 2 +- lib/pry/wrapped_module.rb | 6 +++++- spec/code_spec.rb | 14 +++++++++++--- 10 files changed, 57 insertions(+), 39 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 652dcad2..d8b9ce6e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -197,14 +197,3 @@ Style/FormatStringToken: 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' diff --git a/lib/pry/code.rb b/lib/pry/code.rb index eb95787f..ccd7ca59 100644 --- a/lib/pry/code.rb +++ b/lib/pry/code.rb @@ -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') diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index 00ce9457..c0452f89 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -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) diff --git a/lib/pry/method.rb b/lib/pry/method.rb index bd3b2a5b..e10fbe4f 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -493,7 +493,11 @@ 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) diff --git a/lib/pry/method/disowned.rb b/lib/pry/method/disowned.rb index de32ee91..411bdd95 100644 --- a/lib/pry/method/disowned.rb +++ b/lib/pry/method/disowned.rb @@ -46,14 +46,16 @@ 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 diff --git a/lib/pry/output.rb b/lib/pry/output.rb index 9d7b6e90..c6c3eada 100644 --- a/lib/pry/output.rb +++ b/lib/pry/output.rb @@ -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) diff --git a/lib/pry/plugins.rb b/lib/pry/plugins.rb index 07afec63..22a7f2dd 100644 --- a/lib/pry/plugins.rb +++ b/lib/pry/plugins.rb @@ -8,8 +8,9 @@ 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?(*) diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index c26c6695..37688271 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -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" diff --git a/lib/pry/wrapped_module.rb b/lib/pry/wrapped_module.rb index cba13458..8b03d6d9 100644 --- a/lib/pry/wrapped_module.rb +++ b/lib/pry/wrapped_module.rb @@ -146,7 +146,11 @@ 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_missing?(method_name, include_private = false) diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 074d8f0d..0d2c2d4d 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -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 From 4ea52b4a12e8a25b4625ab2232e83ee164c190e6 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 15:53:09 +0200 Subject: [PATCH 5/6] rubocop: fix offences of the Style/GlobalVars cop --- .rubocop_todo.yml | 6 ------ spec/commands/edit_spec.rb | 3 --- 2 files changed, 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d8b9ce6e..f86cb75d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -191,9 +191,3 @@ Style/FormatStringToken: - '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' diff --git a/spec/commands/edit_spec.rb b/spec/commands/edit_spec.rb index 82da5cef..c71f52de 100644 --- a/spec/commands/edit_spec.rb +++ b/spec/commands/edit_spec.rb @@ -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 From 62516f859677553beef9b407e74d807329690629 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 17:41:39 +0200 Subject: [PATCH 6/6] rubocop: fix offences of the Style/FormatStringToken cop --- .rubocop_todo.yml | 9 --------- lib/pry/commands/ls/local_vars.rb | 6 +++++- lib/pry/config/behavior.rb | 2 +- lib/pry/pry_class.rb | 4 +++- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f86cb75d..e33da4ea 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -182,12 +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' diff --git a/lib/pry/commands/ls/local_vars.rb b/lib/pry/commands/ls/local_vars.rb index abdd6e7f..2b8f4374 100644 --- a/lib/pry/commands/ls/local_vars.rb +++ b/lib/pry/commands/ls/local_vars.rb @@ -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}s = %s", + name: color(:local_var, colorized_lhs), + value: rhs + ) end end end diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index c0452f89..8bf1ef7b 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -340,7 +340,7 @@ class Pry private def __clip_inspect(obj) - format("#{obj.class}:0x%x", obj.object_id) + format("#{obj.class}:0x%x", id: obj.object_id) end def __try_convert_to_hash(obj) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 64a70ba5..68440bf0 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -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%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"