From 1a20b22984216492fb60fb0d6d1dbac6ce92cfc9 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sat, 23 Mar 2019 19:06:27 +0200 Subject: [PATCH] rubocop: fix offences of the Style/EvalWithLocation cop --- .rubocop_todo.yml | 9 -------- lib/pry/core_extensions.rb | 2 +- lib/pry/input_completer.rb | 26 +++++++++++++-------- spec/commands/edit_spec.rb | 13 ++++++----- spec/commands/watch_expression_spec.rb | 31 +++++++++++++------------- spec/method_spec.rb | 10 +++++++-- 6 files changed, 49 insertions(+), 42 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e33da4ea..b89d02e7 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -173,12 +173,3 @@ Style/DoubleNegation: - 'lib/pry/pager.rb' - 'lib/pry/slop/option.rb' - 'lib/pry/wrapped_module.rb' - -# Offense count: 27 -Style/EvalWithLocation: - Exclude: - - 'lib/pry/core_extensions.rb' - - 'lib/pry/input_completer.rb' - - 'spec/commands/edit_spec.rb' - - 'spec/commands/watch_expression_spec.rb' - - 'spec/method_spec.rb' diff --git a/lib/pry/core_extensions.rb b/lib/pry/core_extensions.rb index 049f8bbb..ffe1d221 100644 --- a/lib/pry/core_extensions.rb +++ b/lib/pry/core_extensions.rb @@ -84,7 +84,7 @@ class Object return class_eval { binding } if Pry::Helpers::Platform.jruby? && name.nil? # class_eval sets both self and the default definee to this class. - return class_eval("binding") + return class_eval("binding", __FILE__, __LINE__) end unless self.class.method_defined?(:__pry__) diff --git a/lib/pry/input_completer.rb b/lib/pry/input_completer.rb index 4b2caa8b..5e879fd3 100644 --- a/lib/pry/input_completer.rb +++ b/lib/pry/input_completer.rb @@ -115,8 +115,12 @@ class Pry receiver = Regexp.last_match(1) message = Regexp.quote(Regexp.last_match(2)) begin - candidates = eval("#{receiver}.constants.collect(&:to_s)", bind) - candidates |= eval("#{receiver}.methods.collect(&:to_s)", bind) + candidates = eval( + "#{receiver}.constants.collect(&:to_s)", bind, __FILE__, __LINE__ + ) + candidates |= eval( + "#{receiver}.methods.collect(&:to_s)", bind, __FILE__, __LINE__ + ) rescue Pry::RescuableException candidates = [] end @@ -153,15 +157,17 @@ class Pry receiver = Regexp.last_match(1) message = Regexp.quote(Regexp.last_match(2)) - gv = eval("global_variables", bind).collect(&:to_s) - lv = eval("local_variables", bind).collect(&:to_s) - cv = eval("self.class.constants", bind).collect(&:to_s) + gv = eval("global_variables", bind, __FILE__, __LINE__).collect(&:to_s) + lv = eval("local_variables", bind, __FILE__, __LINE__).collect(&:to_s) + cv = eval("self.class.constants", bind, __FILE__, __LINE__).collect(&:to_s) if (gv | lv | cv).include?(receiver) || /^[A-Z]/ =~ receiver && /\./ !~ receiver # foo.func and foo is local var. OR # Foo::Bar.func begin - candidates = eval("#{receiver}.methods", bind).collect(&:to_s) + candidates = eval( + "#{receiver}.methods", bind, __FILE__, __LINE__ + ).collect(&:to_s) rescue Pry::RescuableException candidates = [] end @@ -195,11 +201,13 @@ class Pry candidates = eval( "methods | private_methods | local_variables | " \ "self.class.constants | instance_variables", - bind + bind, __FILE__, __LINE__ - 2 ).collect(&:to_s) - if eval("respond_to?(:class_variables)", bind) - candidates += eval("class_variables", bind).collect(&:to_s) + if eval("respond_to?(:class_variables)", bind, __FILE__, __LINE__) + candidates += eval( + "class_variables", bind, __FILE__, __LINE__ + ).collect(&:to_s) end candidates = (candidates | ReservedWords | custom_completions) diff --git a/spec/commands/edit_spec.rb b/spec/commands/edit_spec.rb index c71f52de..7b2f57d3 100644 --- a/spec/commands/edit_spec.rb +++ b/spec/commands/edit_spec.rb @@ -773,11 +773,13 @@ describe "edit" do before do @t = pry_tester class BinkyWink - eval %( + # rubocop:disable Style/EvalWithLocation + eval <<-RUBY def m1 binding end - ) + RUBY + # rubocop:enable Style/EvalWithLocation def m2 _foo = :jeremy_jones @@ -821,10 +823,9 @@ describe "edit" do before do @t = pry_tester class TrinkyDink - eval %( - def m - end - ) + # rubocop:disable Style/EvalWithLocation + eval('def m; end') + # rubocop:enable Style/EvalWithLocation end end diff --git a/spec/commands/watch_expression_spec.rb b/spec/commands/watch_expression_spec.rb index 5b2692aa..04f26d10 100644 --- a/spec/commands/watch_expression_spec.rb +++ b/spec/commands/watch_expression_spec.rb @@ -3,8 +3,8 @@ describe "watch expression" do # 1) Create an instance of pry that can use for multiple calls # 2) Exercise the after_eval hook # 3) Return the output - def eval(expr) - output = @tester.eval expr + def watch_eval(expr) + output = @tester.eval(expr) @tester.pry.hooks.exec_hook :after_eval, nil, @tester.pry output end @@ -12,27 +12,28 @@ describe "watch expression" do before do @tester = pry_tester @tester.pry.hooks.clear_event_hooks(:after_eval) - eval "watch --delete" + watch_eval('watch --delete') end it "registers the after_eval hook" do - eval 'watch 1+1' + watch_eval('watch 1+1') + watch_eval('') expect(@tester.pry.hooks.hook_exists?(:after_eval, :watch_expression)).to eq(true) end it "prints no watched expressions" do - expect(eval('watch')).to match(/No watched expressions/) + expect(watch_eval('watch')).to match(/No watched expressions/) end it "watches an expression" do - eval "watch 1+1" - expect(eval('watch')).to match(/=> 2/) + watch_eval 'watch 1+1' + expect(watch_eval('watch')).to match(/=> 2/) end it "watches a local variable" do - eval 'foo = :bar' - eval 'watch foo' - expect(eval('watch')).to match(/=> :bar/) + watch_eval('foo = :bar') + watch_eval('watch foo') + expect(watch_eval('watch')).to match(/=> :bar/) end it "prints when an expression changes" do @@ -100,17 +101,17 @@ describe "watch expression" do describe "deleting expressions" do before do - eval 'watch :keeper' - eval 'watch :delete' - eval 'watch -d 2' + watch_eval('watch :keeper') + watch_eval('watch :delete') + watch_eval('watch -d 2') end it "keeps keeper" do - expect(eval('watch')).to match(/keeper/) + expect(watch_eval('watch')).to match(/keeper/) end it "deletes delete" do - expect(eval('watch')).not_to match(/delete/) + expect(watch_eval('watch')).not_to match(/delete/) end end end diff --git a/spec/method_spec.rb b/spec/method_spec.rb index 9e11f2d7..2d5a19f6 100644 --- a/spec/method_spec.rb +++ b/spec/method_spec.rb @@ -638,13 +638,19 @@ describe Pry::Method do # keyword args are only on >= Ruby 2.1 if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.1") it 'should print the name of keyword args, with :? after the arg name' do - eval %{def @class.keyword(keyword_arg: "") end} + eval <<-RUBY, binding, __FILE__, __LINE__ + 1 + def @class.keyword(keyword_arg: '') + end + RUBY signature = Pry::Method.new(@class.method(:keyword)).signature expect(signature).to eq("keyword(keyword_arg:?)") end it 'should print the name of keyword args, with : after the arg name' do - eval %{def @class.required_keyword(required_key:) end} + eval <<-RUBY, binding, __FILE__, __LINE__ + 1 + def @class.required_keyword(required_key:) + end + RUBY signature = Pry::Method.new(@class.method(:required_keyword)).signature expect(signature).to eq("required_keyword(required_key:)") end