Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an incorrect auto-correct for Style/EvalWithLocation #9616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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