class RSpectre::AutoCorrector

Public Class Methods

new(filename, nodes) click to toggle source
Calls superclass method
# File lib/rspectre/auto_corrector.rb, line 7
def initialize(filename, nodes)
  buffer = Parser::Source::Buffer.new("(#{filename})")
  buffer.source = File.read(filename)

  super(filename, nodes, buffer)
end

Public Instance Methods

correct() click to toggle source
# File lib/rspectre/auto_corrector.rb, line 14
def correct
  File.open(filename, 'w') do |file|
    file.write(rewrite(buffer, Parser::CurrentRuby.new.parse(buffer)))
  end
end
on_block(node) click to toggle source
Calls superclass method
# File lib/rspectre/auto_corrector.rb, line 20
def on_block(node)
  remove(removal_range(node)) if nodes.any? do |offense_node|
    node == offense_node && node.location.line == offense_node.location.line
  end

  super
end

Private Instance Methods

range_end(node) click to toggle source
# File lib/rspectre/auto_corrector.rb, line 40
def range_end(node)
  location = node.location.expression

  last_line    = location.last_line
  end_location = location.end

  walk(node) do |child|
    child_location = child.location

    next unless child_location.respond_to?(:heredoc_end)

    heredoc_end = child_location.heredoc_end

    if heredoc_end.last_line > last_line
      last_line    = heredoc_end.last_line
      end_location = heredoc_end
    end
  end

  end_location.end_pos
end
removal_range(node) click to toggle source

This corrects for cases which contains heredocs which do not get removed in all cases if we just use the `expression` range.

# File lib/rspectre/auto_corrector.rb, line 32
def removal_range(node)
  Parser::Source::Range.new(
    buffer,
    node.location.expression.begin_pos,
    range_end(node)
  )
end
walk(node) { |node| ... } click to toggle source
# File lib/rspectre/auto_corrector.rb, line 62
def walk(node, &block)
  yield node

  node.children.each do |child|
    next unless child.is_a?(::Parser::AST::Node)

    walk(child, &block)
  end
end