Class: Mutante::LineAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/mutante/line_analyzer.rb

Overview

Walks a file line-by-line, yielding an opportunity to comment each eligible line out. The file is rewritten for each trial and restored afterwards — callers get a block that runs while the file is mutated.

Constant Summary collapse

SKIP_EXACT =

Lines that are pure structural keywords would break the syntax the moment they are commented out, so we skip them up front.

%w[
  end else elsif ensure rescue begin do then
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ LineAnalyzer

Returns a new instance of LineAnalyzer.



12
13
14
15
# File 'lib/mutante/line_analyzer.rb', line 12

def initialize(path)
  @path          = path
  @original_lines = File.readlines(path)
end

Instance Method Details

#each_candidateObject

Yields |line_number, line_text| for each line worth trying. Inside the block, the file on disk has that line commented out. After the block returns, the file is restored to its original form.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mutante/line_analyzer.rb', line 20

def each_candidate
  @original_lines.each_with_index do |line, index|
    next unless candidate?(line)

    commented = commented_version(line)
    next unless still_valid_ruby?(index, commented)

    write_with_substitution(index, commented)
    begin
      yield(index + 1, line.chomp)
    ensure
      restore_original
    end
  end
end