From 10aa0ccf5776296f8cc0ce10bdcb7c5dcadb8d0d Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 19 Mar 2021 02:07:04 +0900 Subject: [PATCH] Fix an incorrect auto-correct for `Style/EvalWithLocation` Fixes https://github.com/testdouble/standard/issues/275. This PR fixes the following incorrect auto-correct for `Style/EvalWithLocation` when using `#instance_eval` with a string argument in parentheses. ```console % cat example.rb instance_eval('@foo = foo') % rubocop -A --only Style/EvalWithLocation (snip) Inspecting 1 file C Offenses: example.rb:1:1: C: [Corrected] Style/EvalWithLocation: Pass __FILE__ and __LINE__ to instance_eval. instance_eval('@foo = foo') ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 file inspected, 1 offense detected, 1 offense corrected ``` ## Before ```console % cat example.rb instance_eval('@foo = foo'), __FILE__, __LINE__ % ruby -c example.rb example.rb:1: syntax error, unexpected ',', expecting end-of-input instance_eval('@foo = foo'), __FILE__, __LINE__ ``` ## After ```console % cat example.rb instance_eval('@foo = foo', __FILE__, __LINE__) % ruby -c example.rb Syntax OK ``` --- ...orrect_autocorrect_for_style_eval_with_location.md | 1 + lib/rubocop/cop/style/eval_with_location.rb | 2 +- spec/rubocop/cop/style/eval_with_location_spec.rb | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_incorrect_autocorrect_for_style_eval_with_location.md diff --git a/changelog/fix_incorrect_autocorrect_for_style_eval_with_location.md b/changelog/fix_incorrect_autocorrect_for_style_eval_with_location.md new file mode 100644 index 00000000000..01afda5966f --- /dev/null +++ b/changelog/fix_incorrect_autocorrect_for_style_eval_with_location.md @@ -0,0 +1 @@ +* [#9616](https://github.com/rubocop/rubocop/pull/9616): Fix an incorrect auto-correct for `Style/EvalWithLocation` when using `#instance_eval` with a string argument in parentheses. ([@koic][]) diff --git a/lib/rubocop/cop/style/eval_with_location.rb b/lib/rubocop/cop/style/eval_with_location.rb index 2ccbd5d3ff7..3d91bde864f 100644 --- a/lib/rubocop/cop/style/eval_with_location.rb +++ b/lib/rubocop/cop/style/eval_with_location.rb @@ -224,7 +224,7 @@ def add_offense_for_missing_location(node, code) register_offense(node) do |corrector| line_str = missing_line(node, code) - corrector.insert_after(node.loc.expression.end, ", __FILE__, #{line_str}") + corrector.insert_after(node.last_argument.source_range.end, ", __FILE__, #{line_str}") end end diff --git a/spec/rubocop/cop/style/eval_with_location_spec.rb b/spec/rubocop/cop/style/eval_with_location_spec.rb index 72194c16a64..d797351b23a 100644 --- a/spec/rubocop/cop/style/eval_with_location_spec.rb +++ b/spec/rubocop/cop/style/eval_with_location_spec.rb @@ -159,6 +159,17 @@ RUBY end + it 'registers an offense when using `#instance_eval` with a string argument in parentheses' do + expect_offense(<<~RUBY) + instance_eval('@foo = foo') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Pass `__FILE__` and `__LINE__` to `instance_eval`. + RUBY + + expect_correction(<<~RUBY) + instance_eval('@foo = foo', __FILE__, __LINE__) + RUBY + end + it 'registers an offense when using `#class_eval` with an incorrect lineno' do expect_offense(<<~RUBY) C.class_eval <<-CODE, __FILE__, __LINE__