Skip to content

Commit

Permalink
[Fix rubocop#9032] Fix an error for `Style/DocumentDynamicEvalDefinit…
Browse files Browse the repository at this point in the history
…ion`

Fixes rubocop#9032.

This PR fixes an error for `Style/DocumentDynamicEvalDefinition` when using
eval-type method with interpolated string that is not heredoc without comment doc.

It may be better to provide an option to accept non-heredoc interpolated-string in future.
  • Loading branch information
koic committed Nov 12, 2020
1 parent aba0e36 commit 67e889e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9032](https://github.com/rubocop-hq/rubocop/issues/9032): Fix an error for `Style/DocumentDynamicEvalDefinition` when using eval-type method with interpolated string that is not heredoc without comment doc. ([@koic][])
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,12 @@ class_eval(
end
EOT
)
# bad - interpolated string without comment
class_eval("def #{unsafe_method}!(*params); end")
# good - with inline comment or replace it with block comment using heredoc
class_eval("def #{unsafe_method}!(*params); end # def capitalize!(*params); end")
----

=== References
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/document_dynamic_eval_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ module Style
# end
# EOT
# )
#
# # bad - interpolated string without comment
# class_eval("def #{unsafe_method}!(*params); end")
#
# # good - with inline comment or replace it with block comment using heredoc
# class_eval("def #{unsafe_method}!(*params); end # def capitalize!(*params); end")
class DocumentDynamicEvalDefinition < Base
BLOCK_COMMENT_REGEXP = /^\s*#(?!{)/.freeze
COMMENT_REGEXP = /\s*#(?!{).*/.freeze
Expand All @@ -79,7 +85,8 @@ def on_send(node)
arg_node = node.first_argument

return unless arg_node&.dstr_type? && interpolated?(arg_node)
return if inline_comment_docs?(arg_node) || comment_block_docs?(arg_node)
return if inline_comment_docs?(arg_node) ||
arg_node.heredoc? && comment_block_docs?(arg_node)

add_offense(node.loc.selector)
end
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/document_dynamic_eval_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ def \#{unsafe_method}(*params, &block) # def capitalize(*params, &block)
RUBY
end

it 'registers an offense when using eval-type method with interpolated string ' \
'that is not heredoc without comment doc' do
expect_offense(<<~'RUBY')
stringio.instance_eval("def original_filename; 'stringio#{n}.txt'; end")
^^^^^^^^^^^^^ Add a comment block showing its appearance if interpolated.
RUBY
end

it 'does not register an offense when using eval-type method with interpolated string ' \
'that is not heredoc with comment doc' do
expect_no_offenses(<<~'RUBY')
stringio.instance_eval("def original_filename; 'stringio#{n}.txt'; end # def original_filename; 'stringiofoo.txt'; end")
RUBY
end

context 'block comment in heredoc' do
it 'does not register an offense for a matching block comment' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 67e889e

Please sign in to comment.