Skip to content

Commit

Permalink
Support autocorrection for Lint/InterpolationCheck
Browse files Browse the repository at this point in the history
This PR supports autocorrection for `Lint/InterpolationCheck` and
marks it unsafe because autocorrected code is incompatible.
  • Loading branch information
koic committed Jun 27, 2020
1 parent 3d9dbfa commit fba655f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
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

0 comments on commit fba655f

Please sign in to comment.