From 978993e8806c8d3e19aaf0b5fd679b6eeb80136c Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Wed, 11 Jan 2023 18:30:34 -0500 Subject: [PATCH] Enable Style/EvalWithLocation Ref: ad39d6b Ensure that all evals include file and line number to identify their source. Two of the evals reported by this cop were unneccesary and replaced with non-eval alternatives: xml is set as a local variable in Template::Handlers::Builder#call, and instance_eval isn't needed to get the current binding. There are additionally 27 offenses in test directories, but since it seems less important to fix those they are currently ignored. --- .rubocop.yml | 5 +++++ actionview/lib/action_view/helpers/atom_feed_helper.rb | 2 +- activerecord/lib/active_record/association_relation.rb | 2 +- activesupport/lib/active_support/testing/stream.rb | 2 +- railties/lib/rails/generators/migration.rb | 3 +-- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 76f7f8c7054e2..fa00f06148e61 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -239,6 +239,11 @@ Lint/UselessAssignment: Lint/DeprecatedClassMethods: Enabled: true +Style/EvalWithLocation: + Enabled: true + Exclude: + - '**/test/**/*' + Style/ParenthesesAroundCondition: Enabled: true diff --git a/actionview/lib/action_view/helpers/atom_feed_helper.rb b/actionview/lib/action_view/helpers/atom_feed_helper.rb index aa205a3d3cfb0..fa13cf31c1ee8 100644 --- a/actionview/lib/action_view/helpers/atom_feed_helper.rb +++ b/actionview/lib/action_view/helpers/atom_feed_helper.rb @@ -102,7 +102,7 @@ def atom_feed(options = {}, &block) options[:schema_date] = "2005" # The Atom spec copyright date end - xml = options.delete(:xml) || eval("xml", block.binding) + xml = options.delete(:xml) || block.binding.local_variable_get(:xml) xml.instruct! if options[:instruct] options[:instruct].each do |target, attrs| diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb index 7bcefe71adbf1..3f71fa834579f 100644 --- a/activerecord/lib/active_record/association_relation.rb +++ b/activerecord/lib/active_record/association_relation.rb @@ -16,7 +16,7 @@ def ==(other) end %w(insert insert_all insert! insert_all! upsert upsert_all).each do |method| - class_eval <<~RUBY + class_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{method}(attributes, **kwargs) if @association.reflection.through_reflection? raise ArgumentError, "Bulk insert or upsert is currently not supported for has_many through association" diff --git a/activesupport/lib/active_support/testing/stream.rb b/activesupport/lib/active_support/testing/stream.rb index 55017d3535989..0b8e0298a0d01 100644 --- a/activesupport/lib/active_support/testing/stream.rb +++ b/activesupport/lib/active_support/testing/stream.rb @@ -23,7 +23,7 @@ def quietly(&block) def capture(stream) stream = stream.to_s captured_stream = Tempfile.new(stream) - stream_io = eval("$#{stream}") + stream_io = eval("$#{stream}", binding, __FILE__, __LINE__) origin_stream = stream_io.dup stream_io.reopen(captured_stream) diff --git a/railties/lib/rails/generators/migration.rb b/railties/lib/rails/generators/migration.rb index fce42207b2b33..1dfaa56c5e0e0 100644 --- a/railties/lib/rails/generators/migration.rb +++ b/railties/lib/rails/generators/migration.rb @@ -57,13 +57,12 @@ def migration_template(source, destination, config = {}) source = File.expand_path(find_in_source_paths(source.to_s)) set_migration_assigns!(destination) - context = instance_eval("binding") dir, base = File.split(destination) numbered_destination = File.join(dir, ["%migration_number%", base].join("_")) file = create_migration numbered_destination, nil, config do - ERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer").result(context) + ERB.new(::File.binread(source), trim_mode: "-", eoutvar: "@output_buffer").result(binding) end Rails::Generators.add_generated_file(file) end