diff --git a/lib/rubocop.rb b/lib/rubocop.rb index 9266444bce03..a352060317c1 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -140,6 +140,7 @@ require_relative 'rubocop/cop/mixin/unused_argument' require_relative 'rubocop/cop/correctors/alignment_corrector' +require_relative 'rubocop/cop/correctors/condition_corrector' require_relative 'rubocop/cop/correctors/empty_line_corrector' require_relative 'rubocop/cop/correctors/multiline_literal_brace_corrector' require_relative 'rubocop/cop/correctors/ordered_gem_corrector' diff --git a/lib/rubocop/cop/correctors/condition_corrector.rb b/lib/rubocop/cop/correctors/condition_corrector.rb new file mode 100644 index 000000000000..7b35969e3d45 --- /dev/null +++ b/lib/rubocop/cop/correctors/condition_corrector.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + # This class does condition auto-correction + class ConditionCorrector + class << self + def correct_negative_condition(node) + condition = negated_condition(node) + + lambda do |corrector| + corrector.replace(node.loc.keyword, node.inverse_keyword) + corrector.replace(condition.source_range, + condition.children.first.source) + end + end + + private + + def negated_condition(node) + condition = node.condition + condition = condition.children.first while condition.begin_type? + condition + end + end + end + end +end diff --git a/lib/rubocop/cop/mixin/negative_conditional.rb b/lib/rubocop/cop/mixin/negative_conditional.rb index 6bd77bfb9f61..8f667aa04c61 100644 --- a/lib/rubocop/cop/mixin/negative_conditional.rb +++ b/lib/rubocop/cop/mixin/negative_conditional.rb @@ -27,22 +27,6 @@ def check_negative_conditional(node) add_offense(node) end - - def negative_conditional_corrector(node) - condition = negated_condition(node) - - lambda do |corrector| - corrector.replace(node.loc.keyword, node.inverse_keyword) - corrector.replace(condition.source_range, - condition.children.first.source) - end - end - - def negated_condition(node) - condition = node.condition - condition = condition.children.first while condition.begin_type? - condition - end end end end diff --git a/lib/rubocop/cop/style/negated_if.rb b/lib/rubocop/cop/style/negated_if.rb index 5e6889bd1f42..fa8276abb508 100644 --- a/lib/rubocop/cop/style/negated_if.rb +++ b/lib/rubocop/cop/style/negated_if.rb @@ -80,7 +80,7 @@ def on_if(node) end def autocorrect(node) - negative_conditional_corrector(node) + ConditionCorrector.correct_negative_condition(node) end private diff --git a/lib/rubocop/cop/style/negated_while.rb b/lib/rubocop/cop/style/negated_while.rb index dd70cbc8ea17..24fa58924c8b 100644 --- a/lib/rubocop/cop/style/negated_while.rb +++ b/lib/rubocop/cop/style/negated_while.rb @@ -15,12 +15,14 @@ def on_until(node) check_negative_conditional(node) end - def message(node) - format(MSG, inverse: node.inverse_keyword, current: node.keyword) + def autocorrect(node) + ConditionCorrector.correct_negative_condition(node) end - def autocorrect(node) - negative_conditional_corrector(node) + private + + def message(node) + format(MSG, inverse: node.inverse_keyword, current: node.keyword) end end end