Skip to content

Commit

Permalink
Merge pull request #7925 from koic/support_autocorrection_for_layout_…
Browse files Browse the repository at this point in the history
…condition_position]

Support autocorrection for `Layout/ConditionPosition`
  • Loading branch information
koic committed May 10, 2020
2 parents c1e55dd + 8fd210a commit 42a607f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#7937](https://github.com/rubocop-hq/rubocop/pull/7937): Support autocorrection for `Style/IfWithSemicolon`. ([@koic][])
* [#3696](https://github.com/rubocop-hq/rubocop/issues/3696): Add `AllowComments` option to `Lint/EmptyWhen` cop. ([@koic][])
* [#7910](https://github.com/rubocop-hq/rubocop/pull/7910): Support autocorrection for `Lint/ParenthesesAsGroupedExpression`. ([@koic][])
* [#7925](https://github.com/rubocop-hq/rubocop/pull/7925): Support autocorrection for `Layout/ConditionPosition`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -376,6 +376,7 @@ Layout/ConditionPosition:
StyleGuide: '#same-line-condition'
Enabled: true
VersionAdded: '0.53'
VersionChanged: '0.83'

Layout/DefEndAlignment:
Description: 'Align ends corresponding to defs correctly.'
Expand Down
14 changes: 12 additions & 2 deletions lib/rubocop/cop/layout/condition_position.rb
Expand Up @@ -23,6 +23,8 @@ module Layout
# do_something
# end
class ConditionPosition < Cop
include RangeHelp

MSG = 'Place the condition on the same line as `%<keyword>s`.'

def on_if(node)
Expand All @@ -34,9 +36,17 @@ def on_if(node)
def on_while(node)
check(node)
end
alias on_until on_while

def on_until(node)
check(node)
def autocorrect(node)
lambda do |corrector|
range = range_by_whole_lines(
node.source_range, include_final_newline: true
)

corrector.insert_after(node.parent.loc.keyword, " #{node.source}")
corrector.remove(range)
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_layout.md
Expand Up @@ -713,7 +713,7 @@ end

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

This cop checks for conditions that are not on the same line as
if/while/until.
Expand Down
17 changes: 15 additions & 2 deletions spec/rubocop/cop/layout/condition_position_spec.rb
Expand Up @@ -4,13 +4,18 @@
subject(:cop) { described_class.new }

%w[if unless while until].each do |keyword|
it 'registers an offense for condition on the next line' do
it 'registers an offense and corrects for condition on the next line' do
expect_offense(<<~RUBY)
#{keyword}
x == 10
^^^^^^^ Place the condition on the same line as `#{keyword}`.
end
RUBY

expect_correction(<<~RUBY)
#{keyword} x == 10
end
RUBY
end

it 'accepts condition on the same line' do
Expand All @@ -29,7 +34,7 @@
end
end

it 'registers an offense for elsif condition on the next line' do
it 'registers an offense and corrects for elsif condition on the next line' do
expect_offense(<<~RUBY)
if something
test
Expand All @@ -39,6 +44,14 @@
test
end
RUBY

expect_correction(<<~RUBY)
if something
test
elsif something
test
end
RUBY
end

it 'handles ternary ops' do
Expand Down

0 comments on commit 42a607f

Please sign in to comment.