Class: Ace::TestRunner::Molecules::DeprecationFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/test_runner/molecules/deprecation_fixer.rb

Overview

Identifies and suggests fixes for deprecated code patterns

Constant Summary collapse

DEPRECATION_PATTERNS =
[
  {
    pattern: /must_equal/,
    replacement: "must_be :==",
    description: "Replace must_equal with must_be :=="
  },
  {
    pattern: /wont_equal/,
    replacement: "wont_be :==",
    description: "Replace wont_equal with wont_be :=="
  },
  {
    pattern: /must_be_nil/,
    replacement: "must_be_nil",
    description: "Use must_be_nil (no change needed)"
  },
  {
    pattern: /\.must_be\s+:([<>]=?)/,
    replacement: 'must_be :\1',
    description: "Comparison operators should use symbols"
  },
  {
    pattern: /assert_equal\s+nil,/,
    replacement: "assert_nil",
    description: "Use assert_nil instead of assert_equal nil"
  },
  {
    pattern: /refute_equal\s+nil,/,
    replacement: "refute_nil",
    description: "Use refute_nil instead of refute_equal nil"
  }
].freeze

Instance Method Summary collapse

Instance Method Details

#find_deprecations(content) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ace/test_runner/molecules/deprecation_fixer.rb', line 41

def find_deprecations(content)
  deprecations = []

  content.lines.each_with_index do |line, index|
    DEPRECATION_PATTERNS.each do |pattern_info|
      if line.match?(pattern_info[:pattern])
        deprecations << {
          line_number: index + 1,
          line_content: line.strip,
          pattern: pattern_info[:pattern].source,
          suggestion: pattern_info[:description],
          replacement: pattern_info[:replacement]
        }
      end
    end
  end

  deprecations
end

#fix_deprecations_in_output(test_output) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ace/test_runner/molecules/deprecation_fixer.rb', line 89

def fix_deprecations_in_output(test_output)
  fixes = []

  # Look for deprecation warnings in test output
  test_output.scan(/DEPRECATION WARNING: (.+)/) do |warning|
    fix = analyze_warning(warning.first)
    fixes << fix if fix
  end

  fixes.uniq
end

#fix_file(file_path, dry_run: false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ace/test_runner/molecules/deprecation_fixer.rb', line 61

def fix_file(file_path, dry_run: false)
  unless File.exist?(file_path)
    return {success: false, error: "File not found: #{file_path}"}
  end

  original_content = File.read(file_path)
  fixed_content = apply_fixes(original_content)

  if original_content == fixed_content
    return {
      success: true,
      changes: 0,
      message: "No deprecations found"
    }
  end

  unless dry_run
    File.write(file_path, fixed_content)
  end

  {
    success: true,
    changes: count_changes(original_content, fixed_content),
    message: dry_run ? "Would fix deprecations (dry run)" : "Fixed deprecations",
    diff: generate_diff(original_content, fixed_content)
  }
end

#generate_fix_report(deprecations) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ace/test_runner/molecules/deprecation_fixer.rb', line 101

def generate_fix_report(deprecations)
  return "No deprecations found." if deprecations.empty?

  lines = ["# Deprecation Fixes Required", ""]

  grouped = deprecations.group_by { |d| d[:file] || "Unknown" }

  grouped.each do |file, file_deprecations|
    lines << "## File: #{file}"
    lines << ""

    file_deprecations.each do |dep|
      lines << "- Line #{dep[:line_number]}: #{dep[:suggestion]}"
      if dep[:replacement]
        lines << "  Replace: `#{dep[:line_content]}`"
        lines << "  With: `#{apply_single_fix(dep[:line_content], dep)}`"
      end
    end

    lines << ""
  end

  lines.join("\n")
end