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 Style/IfWithSemicolon #7937

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 @@ -9,6 +9,7 @@
* [#7917](https://github.com/rubocop-hq/rubocop/pull/7917): Support autocorrection for `Lint/UselessAccessModifier`. ([@koic][])
* [#595](https://github.com/rubocop-hq/rubocop/issues/595): Add ERB pre-processing for configuration files. ([@jonas054][])
* [#7918](https://github.com/rubocop-hq/rubocop/pull/7918): Support autocorrection for `Lint/AmbiguousOperator`. ([@koic][])
* [#7937](https://github.com/rubocop-hq/rubocop/pull/7937): Support autocorrection for `Style/IfWithSemicolon`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -2951,6 +2951,7 @@ Style/IfWithSemicolon:
StyleGuide: '#no-semicolon-ifs'
Enabled: true
VersionAdded: '0.9'
VersionChanged: '0.83'

Style/ImplicitRuntimeError:
Description: >-
Expand Down
16 changes: 16 additions & 0 deletions lib/rubocop/cop/style/if_with_semicolon.rb
Expand Up @@ -19,11 +19,27 @@ class IfWithSemicolon < Cop
MSG = 'Do not use if x; Use the ternary operator instead.'

def on_normal_if_unless(node)
return unless node.else_branch

beginning = node.loc.begin
return unless beginning&.is?(';')

add_offense(node)
end

def autocorrect(node)
lambda do |corrector|
corrector.replace(node, correct_to_ternary(node))
end
end

private

def correct_to_ternary(node)
else_code = node.else_branch ? node.else_branch.source : 'nil'

"#{node.condition.source} ? #{node.if_branch.source} : #{else_code}"
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_style.md
Expand Up @@ -2986,7 +2986,7 @@ end

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.9 | -
Enabled | Yes | Yes | 0.9 | 0.83

Checks for uses of semicolon in if statements.

Expand Down
14 changes: 11 additions & 3 deletions spec/rubocop/cop/style/if_with_semicolon_spec.rb
Expand Up @@ -3,15 +3,23 @@
RSpec.describe RuboCop::Cop::Style::IfWithSemicolon do
subject(:cop) { described_class.new }

it 'registers an offense for one line if/;/end' do
it 'registers an offense and corrects for one line if/;/end' do
expect_offense(<<~RUBY)
if cond; run else dont end
^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use if x; Use the ternary operator instead.
RUBY

expect_correction(<<~RUBY)
cond ? run : dont
RUBY
end

it 'accepts one line if/then/end' do
expect_no_offenses('if cond then run else dont end')
it 'accepts without `else` branch' do
# This case is corrected to a modifier form by `Style/IfUnlessModifier` cop.
# Therefore, this cop does not handle it.
expect_no_offenses(<<~RUBY)
if cond; run end
RUBY
end

it 'can handle modifier conditionals' do
Expand Down