Class: Hecks::Codemod::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/hecks/codemod.rb

Overview

THE REUSABLE RUNNER — every real bug fix this module carries lives here, not in a caller's own script. A caller supplies:

find_candidates: ->(registry) { [...] }
Given a booted registry, return every CANDIDATE this rule
could migrate. A candidate is whatever shape the caller wants
— `apply_candidate` is the only other thing that reads it.

apply_candidate: ->(text, candidate) { [new_text, changed_bool] }
Given one file's current text and one candidate, return the
edited text and whether a match was actually found/removed —
`false` (text unchanged) when the candidate doesn't apply to
THIS file, which is how the meta-domain's multi-file search
below finds the right one without the caller needing to know
which file a candidate lives in ahead of time.

label: ->(candidate) { "..." }
One-line description for the results report.

Instance Method Summary collapse

Constructor Details

#initialize(find_candidates:, apply_candidate:, label:) ⇒ Runner

Returns a new instance of Runner.



188
189
190
191
192
# File 'lib/hecks/codemod.rb', line 188

def initialize(find_candidates:, apply_candidate:, label:)
  @find_candidates = find_candidates
  @apply_candidate = apply_candidate
  @label = label
end

Instance Method Details

#report(results, dry_run:) ⇒ Object



201
202
203
204
205
206
# File 'lib/hecks/codemod.rb', line 201

def report(results, dry_run:)
  puts "== results (#{dry_run ? 'DRY RUN — nothing written' : 'applied'}) =="
  puts "clean (no candidates): #{results[:clean].join(', ')}" unless results[:clean].empty?
  results[:applied].each { |r| puts "APPLIED  #{r[:file]}: #{r[:candidates].join(', ')}" }
  results[:skipped].each { |r| puts "SKIPPED  #{r[:file]} (#{r[:reason]}): #{Array(r[:candidates]).join(', ')}" }
end

#run(dry_run: false) ⇒ Object



194
195
196
197
198
199
# File 'lib/hecks/codemod.rb', line 194

def run(dry_run: false)
  results = { applied: [], skipped: [], clean: [] }
  run_example_domains(results, dry_run)
  run_meta_domain(results, dry_run)
  results
end