Skip to content

Commit

Permalink
Merge pull request #387 from Earlopain/false-negative-string-identifi…
Browse files Browse the repository at this point in the history
…er-argument

Make `Performance/StringIdentifierArgument` aware of string interpolation
  • Loading branch information
koic authored Nov 21, 2023
2 parents 73c77a6 + 1d7be50 commit 8232082
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#386](https://github.com/rubocop/rubocop-performance/issues/386): Fix a false negative for `Performance/StringIdentifierArgument` when using string interpolation. ([@earlopain][])
18 changes: 16 additions & 2 deletions lib/rubocop/cop/performance/string_identifier_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ module Performance
# send('do_something')
# attr_accessor 'do_something'
# instance_variable_get('@ivar')
# const_get("string_#{interpolation}")
#
# # good
# send(:do_something)
# attr_accessor :do_something
# instance_variable_get(:@ivar)
# const_get(:"string_#{interpolation}")
#
class StringIdentifierArgument < Base
extend AutoCorrector
Expand All @@ -45,22 +47,34 @@ class StringIdentifierArgument < Base
respond_to? send singleton_method __send__
] + COMMAND_METHODS).freeze

# rubocop:disable Metrics/CyclomaticComplexity
def on_send(node)
return if COMMAND_METHODS.include?(node.method_name) && node.receiver
return unless (first_argument = node.first_argument)
return unless first_argument.str_type?
return unless first_argument.str_type? || first_argument.dstr_type?

first_argument_value = first_argument.value
return if first_argument_value.include?(' ') || first_argument_value.include?('::')

replacement = first_argument_value.to_sym.inspect
replacement = argument_replacement(first_argument, first_argument_value)

message = format(MSG, symbol_arg: replacement, string_arg: first_argument.source)

add_offense(first_argument, message: message) do |corrector|
corrector.replace(first_argument, replacement)
end
end
# rubocop:enable Metrics/CyclomaticComplexity

private

def argument_replacement(node, value)
if node.str_type?
value.to_sym.inspect
else
":\"#{value.to_sym}\""
end
end
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions spec/rubocop/cop/performance/string_identifier_argument_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
RUBY
end

it 'does not register an offense when using interpolated string argument' do
expect_no_offenses(<<~'RUBY')
send("do_something_#{var}")
it 'registers an offense when using interpolated string argument' do
expect_offense(<<~RUBY, method: method)
#{method}("do_something_\#{var}")
_{method} ^^^^^^^^^^^^^^^^^^^^^ Use `:"do_something_\#{var}"` instead of `"do_something_\#{var}"`.
RUBY

expect_correction(<<~RUBY)
#{method}(:"do_something_\#{var}")
RUBY
end
end
Expand Down

0 comments on commit 8232082

Please sign in to comment.