Module: WhyClasses::Correction
- Defined in:
- lib/why_classes/correction.rb
Overview
Applies correctable offenses to a file's source via Parser::Source::TreeRewriter, which shares the coordinate system used for detection. Every rewrite is re-parsed as a fail-safe: if the result no longer parses, it is discarded and the original source is kept (the offense is still reported as advice).
Defined Under Namespace
Classes: Result
Class Method Summary collapse
- .apply(source_file, correctable_offenses) ⇒ Object
-
.reparses?(source, _path) ⇒ Boolean
Strict validation: the detection parser is intentionally error-tolerant, so use native Prism (which reports errors) to reject any rewrite that produced syntactically invalid Ruby.
Class Method Details
.apply(source_file, correctable_offenses) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/why_classes/correction.rb', line 16 def apply(source_file, correctable_offenses) return Result.new(source: source_file.source, applied: 0) if correctable_offenses.empty? rewriter = ::Parser::Source::TreeRewriter.new(source_file.buffer) applied = 0 correctable_offenses.each do |offense| offense.corrector.call(rewriter) applied += 1 rescue ::Parser::ClobberingError # Overlapping edit -- skip this one, keep the rest. next end new_source = rewriter.process return Result.new(source: source_file.source, applied: 0) unless reparses?(new_source, source_file.path) Result.new(source: new_source, applied: applied) rescue ::Parser::ClobberingError Result.new(source: source_file.source, applied: 0) end |
.reparses?(source, _path) ⇒ Boolean
Strict validation: the detection parser is intentionally error-tolerant, so use native Prism (which reports errors) to reject any rewrite that produced syntactically invalid Ruby.
40 41 42 |
# File 'lib/why_classes/correction.rb', line 40 def reparses?(source, _path) ::Prism.parse(source).success? end |