Class: Fast::Rewriter
- Inherits:
-
Parser::TreeRewriter
- Object
- Parser::TreeRewriter
- Fast::Rewriter
- Defined in:
- lib/fast/rewriter.rb
Overview
the standalone class needs to combines #replace_on to properly generate the ‘on_<node-type>` methods depending on the expression being used.
Rewriter encapsulates #match_index to allow ExperimentFile#partial_replace in a ExperimentFile.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#ast ⇒ Object
Returns the value of attribute ast.
-
#match_index ⇒ Integer
readonly
With occurrence index.
-
#replacement ⇒ Object
Returns the value of attribute replacement.
-
#search ⇒ Object
Returns the value of attribute search.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #buffer ⇒ Object
-
#execute_replacement(node, captures) ⇒ Object
Execute #replacement block.
-
#initialize(*_args) ⇒ Rewriter
constructor
A new instance of Rewriter.
- #match?(node) ⇒ Boolean
-
#replace_on(*types) ⇒ Object
Generate methods for all affected types.
- #rewrite! ⇒ Object
-
#types ⇒ Array<Symbol>
With all types that matches.
Constructor Details
#initialize(*_args) ⇒ Rewriter
Returns a new instance of Rewriter.
61 62 63 64 |
# File 'lib/fast/rewriter.rb', line 61 def initialize(*_args) super() @match_index = 0 end |
Instance Attribute Details
#ast ⇒ Object
Returns the value of attribute ast.
60 61 62 |
# File 'lib/fast/rewriter.rb', line 60 def ast @ast end |
#match_index ⇒ Integer (readonly)
Returns with occurrence index.
59 60 61 |
# File 'lib/fast/rewriter.rb', line 59 def match_index @match_index end |
#replacement ⇒ Object
Returns the value of attribute replacement.
60 61 62 |
# File 'lib/fast/rewriter.rb', line 60 def replacement @replacement end |
#search ⇒ Object
Returns the value of attribute search.
60 61 62 |
# File 'lib/fast/rewriter.rb', line 60 def search @search end |
#source ⇒ Object
Returns the value of attribute source.
60 61 62 |
# File 'lib/fast/rewriter.rb', line 60 def source @source end |
Instance Method Details
#buffer ⇒ Object
71 72 73 74 75 |
# File 'lib/fast/rewriter.rb', line 71 def buffer buffer = Parser::Source::Buffer.new('replacement') buffer.source = source || ast.loc.expression.source buffer end |
#execute_replacement(node, captures) ⇒ Object
Execute #replacement block
103 104 105 106 107 108 109 |
# File 'lib/fast/rewriter.rb', line 103 def execute_replacement(node, captures) if replacement.parameters.length == 1 instance_exec node, &replacement else instance_exec node, captures, &replacement end end |
#match?(node) ⇒ Boolean
82 83 84 |
# File 'lib/fast/rewriter.rb', line 82 def match?(node) Fast.match?(search, node) end |
#replace_on(*types) ⇒ Object
Generate methods for all affected types.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fast/rewriter.rb', line 88 def replace_on(*types) types.map do |type| self.class.send :define_method, "on_#{type}" do |node| if captures = match?(node) # rubocop:disable Lint/AssignmentInCondition @match_index += 1 execute_replacement(node, captures) end super(node) end end end |
#rewrite! ⇒ Object
66 67 68 69 |
# File 'lib/fast/rewriter.rb', line 66 def rewrite! replace_on(*types) rewrite(buffer, ast) end |