Sha256: 9cd8ee95ad07a9f02d8a788517e7fc59bbd46e5218885a1ed293760279378994

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# encoding: utf-8

module Synvert::Core
  # Scope finds the child nodes which match rules.
  class Rewriter::Scope
    # Initialize a scope
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param rules [Hash]
    # @param block [Block]
    def initialize(instance, rules, &block)
      @instance = instance
      @rules = rules
      @block = block
    end

    # Find the matching nodes. It checks the current node and iterates all child nodes,
    # then run the block code for each matching node.
    def process
      current_node = @instance.current_node
      return unless current_node
      process_with_node current_node do
        matching_nodes = []
        matching_nodes << current_node if current_node.match? @instance, @rules
        current_node.recursive_children do |child_node|
          matching_nodes << child_node if child_node.match? @instance, @rules
        end
        matching_nodes.each do |matching_node|
          process_with_node matching_node do
            @instance.instance_eval &@block
          end
        end
      end
    end

  private

    # Set instance current node properly and process.
    # @param node [Parser::AST::Node]
    def process_with_node(node)
      @instance.current_node = node
      yield
      @instance.current_node = node
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
synvert-core-0.2.0 lib/synvert/core/rewriter/scope.rb
synvert-core-0.1.1 lib/synvert/core/rewriter/scope.rb
synvert-core-0.1.0 lib/synvert/core/rewriter/scope.rb