Class: Ruby::Reforge::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/reforge/rewriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ Rewriter

Returns a new instance of Rewriter.



8
9
10
# File 'lib/ruby/reforge/rewriter.rb', line 8

def initialize(root_path)
  @root_path = root_path
end

Instance Method Details

#rewrite(issues, interactive: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby/reforge/rewriter.rb', line 12

def rewrite(issues, interactive: false)
  grouped_issues = issues.group_by(&:file)
  fixed_count = 0

  grouped_issues.each do |file_path, file_issues|
    next unless File.exist?(file_path)

    content = File.read(file_path)
    lines = content.lines
    modified = false

    # Sort issues by line number (descending) to avoid offset issues
    file_issues.sort_by { |i| -i.line }.each do |issue|
      case issue.type
      when :deprecated_method, :deprecated_pattern
        if interactive
          next unless confirm_fix?(issue)
        end

        line_index = issue.line - 1
        if line_index < lines.length
          old_line = lines[line_index]
          new_line = apply_fix(old_line, issue)
          if old_line != new_line
            lines[line_index] = new_line
            modified = true
            fixed_count += 1
          end
        end
      end
    end

    if modified
      File.write(file_path, lines.join)
      say "✓ Fixed #{file_issues.size} issue(s) in #{File.basename(file_path)}", :green
    end
  end

  fixed_count
end