lib/rubocop/cop/style/and_or.rb in rubocop-0.10.0 vs lib/rubocop/cop/style/and_or.rb in rubocop-0.11.0

- old
+ new

@@ -30,13 +30,25 @@ do_autocorrect(node) end end def autocorrect_action(node) - @corrections << lambda do |corrector| + correction = lambda do |corrector| replacement = (node.type == :and ? '&&' : '||') corrector.replace(node.loc.operator, replacement) end + + new_source = rewrite_node(node, correction) + + # Make the correction only if it doesn't change the AST. + if node == SourceParser.parse(new_source).ast + @corrections << correction + end + end + + def rewrite_node(node, correction) + processed_source = SourceParser.parse(node.loc.expression.source) + Corrector.new(processed_source.buffer, [correction]).rewrite end end end end end