Skip to content

Commit

Permalink
Merge pull request #9616 from koic/fix_incorrect_autocorrect_for_styl…
Browse files Browse the repository at this point in the history
…e_eval_with_location

Fix an incorrect auto-correct for `Style/EvalWithLocation`
  • Loading branch information
koic committed Mar 18, 2021
2 parents 255692c + 10aa0cc commit b06ce2f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
@@ -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][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/eval_with_location.rb
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/eval_with_location_spec.rb
Expand Up @@ -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__
Expand Down

0 comments on commit b06ce2f

Please sign in to comment.