Skip to content

Commit

Permalink
Support autocorrection for Style/IfWithSemicolon
Browse files Browse the repository at this point in the history
This PR supports autocorrection for `Style/IfWithSemicolon` cop.
And it change to accept the following case that without `else` branch.

```ruby
if cond; run end
```

That case is corrected to a modifier form by `Style/IfUnlessModifier` cop.

```ruby
run if cond
```

Therefore, this cop does not handle it.
  • Loading branch information
koic authored and bbatsov committed May 10, 2020
1 parent e95daf9 commit ff3d4ea
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
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 @@ -2952,6 +2952,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 @@ -2990,7 +2990,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

0 comments on commit ff3d4ea

Please sign in to comment.