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 nql or rules, then changes its scope to matching node.

Instance Method Summary collapse

Constructor Details

#initialize(instance, nql_or_rules, options = {}) { ... } ⇒ WithinScope

Initialize a WithinScope.

Parameters:

Yields:

  • run on all matching nodes

Raises:

  • (Synvert::Core::NodeQuery::Compiler::ParseError)

    if the query string is invalid.



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

def initialize(instance, nql_or_rules, options = {}, &block)
  super(instance, &block)

  @options = { including_self: true, stop_at_first_match: false, recursive: true }.merge(options)
  @node_query = NodeQuery.new(nql_or_rules, adapter: instance.current_parser)
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 on each matching node.



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

def process
  current_node = @instance.current_node
  return unless current_node

  matching_nodes = @node_query.query_nodes(current_node, @options)
  @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