From 8fd210a5b11fda9506c01e155e595c90ba3e296e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 2 May 2020 00:31:51 +0900 Subject: [PATCH] Support autocorrection for `Layout/ConditionPosition` This PR supports autocorrection for `Layout/ConditionPosition`. --- CHANGELOG.md | 1 + config/default.yml | 1 + lib/rubocop/cop/layout/condition_position.rb | 14 ++++++++++++-- manual/cops_layout.md | 2 +- .../cop/layout/condition_position_spec.rb | 17 +++++++++++++++-- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54df5bcf232..07ef2ff04e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.yml b/config/default.yml index 510d43a67e4..dc73d7fc4b8 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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.' diff --git a/lib/rubocop/cop/layout/condition_position.rb b/lib/rubocop/cop/layout/condition_position.rb index c977ae65f96..29f17db3f99 100644 --- a/lib/rubocop/cop/layout/condition_position.rb +++ b/lib/rubocop/cop/layout/condition_position.rb @@ -23,6 +23,8 @@ module Layout # do_something # end class ConditionPosition < Cop + include RangeHelp + MSG = 'Place the condition on the same line as `%s`.' def on_if(node) @@ -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 diff --git a/manual/cops_layout.md b/manual/cops_layout.md index cbe4e33e2d1..1cd976e9397 100644 --- a/manual/cops_layout.md +++ b/manual/cops_layout.md @@ -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. diff --git a/spec/rubocop/cop/layout/condition_position_spec.rb b/spec/rubocop/cop/layout/condition_position_spec.rb index 17dba5551f4..e3e2071cf30 100644 --- a/spec/rubocop/cop/layout/condition_position_spec.rb +++ b/spec/rubocop/cop/layout/condition_position_spec.rb @@ -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 @@ -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 @@ -39,6 +44,14 @@ test end RUBY + + expect_correction(<<~RUBY) + if something + test + elsif something + test + end + RUBY end it 'handles ternary ops' do