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/EvalWithLocation cop

This commit is contained in:
Kyrylo Silin 2019-03-23 19:06:27 +02:00
parent 6622549184
commit 1a20b22984
6 changed files with 49 additions and 42 deletions

View file

@ -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'

View file

@ -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__)

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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