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

Support autocorrection for Lint/InterpolationCheck #8164

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7868](https://github.com/rubocop-hq/rubocop/pull/7868): `Cop::Base` is the new recommended base class for cops. ([@marcandre][])
* [#8213](https://github.com/rubocop-hq/rubocop/pull/8213): Permit to specify TargetRubyVersion 2.8 (experimental). ([@koic][])
* [#8164](https://github.com/rubocop-hq/rubocop/pull/8164): Support auto-correction for `Lint/InterpolationCheck`. ([@koic][])

### Bug fixes

Expand Down
2 changes: 2 additions & 0 deletions config/default.yml
Expand Up @@ -1508,7 +1508,9 @@ Lint/InheritException:
Lint/InterpolationCheck:
Description: 'Raise warning for interpolation in single q strs.'
Enabled: true
SafeAutoCorrect: false
VersionAdded: '0.50'
VersionChanged: '0.87'

Lint/LiteralAsCondition:
Description: 'Checks of literals used in conditions.'
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -1437,9 +1437,9 @@ C = Class.new(StandardError)

| Enabled
| Yes
| No
| Yes (Unsafe)
| 0.50
| -
| 0.87
|===

This cop checks for interpolation in a single quoted string.
Expand Down
13 changes: 13 additions & 0 deletions lib/rubocop/cop/lint/interpolation_check.rb
Expand Up @@ -30,6 +30,19 @@ def on_str(node)
add_offense(node)
end

def autocorrect(node)
lambda do |corrector|
starting_token, ending_token = if node.source.include?('"')
['%{', '}']
else
['"', '"']
end

corrector.replace(node.loc.begin, starting_token)
corrector.replace(node.loc.end, ending_token)
end
end

def heredoc?(node)
node.loc.is_a?(Parser::Source::Map::Heredoc) ||
(node.parent && heredoc?(node.parent))
Expand Down
17 changes: 16 additions & 1 deletion spec/rubocop/cop/lint/interpolation_check_spec.rb
Expand Up @@ -3,11 +3,26 @@
RSpec.describe RuboCop::Cop::Lint::InterpolationCheck do
subject(:cop) { described_class.new }

it 'registers an offense for interpolation in single quoted string' do
it 'registers an offense and corrects for interpolation in single quoted string' do
expect_offense(<<~'RUBY')
'foo #{bar}'
^^^^^^^^^^^^ Interpolation in single quoted string detected. Use double quoted strings if you need interpolation.
RUBY

expect_correction(<<~'RUBY')
"foo #{bar}"
RUBY
end

it 'registers an offense and corrects when including interpolation and double quoted string in single quoted string' do
expect_offense(<<~'RUBY')
'foo "#{bar}"'
^^^^^^^^^^^^^^ Interpolation in single quoted string detected. Use double quoted strings if you need interpolation.
RUBY

expect_correction(<<~'RUBY')
%{foo "#{bar}"}
RUBY
end

it 'does not register an offense for properly interpolation strings' do
Expand Down