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, options = { recursive: true }, &block) ⇒ WithinScope

Initialize a scope

Parameters:



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

def initialize(instance, rules, options = { recursive: true }, &block)
  @instance = instance
  @rules = rules
  @options = options
  @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.



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

def process
  current_node = @instance.current_node
  return unless current_node

  matching_nodes = find_matching_nodes(current_node)
  @instance.process_with_node current_node do
    matching_nodes.each do |matching_node|
      @instance.process_with_node matching_node do
        @instance.instance_eval(&@block)
      end
    end
  end
end