Class: Refactor::Rewriter

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

Overview

Full rewriter, typically used for processing multiple rules

Instance Method Summary collapse

Constructor Details

#initialize(rules: []) ⇒ Rewriter

Returns a new instance of Rewriter.



63
64
65
# File 'lib/refactor.rb', line 63

def initialize(rules: [])
  @rules = rules
end

Instance Method Details

#process(string) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/refactor.rb', line 67

def process(string)
  # No sense in processing anything if there's nothing to apply it to
  return string if @rules.empty?

  source = Util.processed_source_from(string)
  ast = source.ast

  source_buffer = source.buffer

  rewriter = Parser::Source::TreeRewriter.new(source_buffer)

  @rules.each do |rule_class|
    rule = rule_class.new(rewriter)
    ast.each_node { |node| rule.process(node) }
  end

  rewriter.process
end