Class: Synvert::Core::Rewriter::WithinScope

Inherits:
Scope
  • Object
show all
Defined in:
lib/synvert/core/rewriter/scope/within_scope.rb

Overview

WithinScope finds out nodes which match rules, then change its scope to matching node.

Instance Method Summary collapse

Constructor Details

#initialize(instance, rules, &block) ⇒ WithinScope

Initialize a scope

Parameters:


11
12
13
14
15
# File 'lib/synvert/core/rewriter/scope/within_scope.rb', line 11

def initialize(instance, rules, &block)
  @instance = instance
  @rules = rules
  @block = block
end

Instance Method Details

#processObject

Find out the matching nodes. It checks the current node and iterates all child nodes, then run the block code with each matching node.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/synvert/core/rewriter/scope/within_scope.rb', line 19

def process
  current_node = @instance.current_node
  return unless current_node
  @instance.process_with_node current_node do
    matching_nodes = []
    matching_nodes << current_node if current_node.match? @rules
    current_node.recursive_children do |child_node|
      matching_nodes << child_node if child_node.match? @rules
    end
    matching_nodes.each do |matching_node|
      @instance.process_with_node matching_node do
        @instance.instance_eval &@block
      end
    end
  end
end