Skip to content

Commit

Permalink
[rubocop#3558] ConditionCorrector extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
garettarrowood committed Dec 17, 2017
1 parent 7fc023e commit 69e3382
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
28 changes: 28 additions & 0 deletions lib/rubocop/cop/correctors/condition_corrector.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 0 additions & 16 deletions lib/rubocop/cop/mixin/negative_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/negated_if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def on_if(node)
end

def autocorrect(node)
negative_conditional_corrector(node)
ConditionCorrector.correct_negative_condition(node)
end

private
Expand Down
10 changes: 6 additions & 4 deletions lib/rubocop/cop/style/negated_while.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69e3382

Please sign in to comment.