Sha256: b11992ba7a68326e0d88afd98ff4e38c00174f990969c28ee619a5bffc7e0d2e

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # WithinScope finds out nodes which match rules, then change its scope to matching node.
  class Rewriter::WithinScope < Rewriter::Scope
    # Initialize a scope
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param rules [Hash]
    # @param options [Hash]
    # @param block [Block]
    def initialize(instance, rules, options = { recursive: true }, &block)
      @instance = instance
      @rules = rules
      @options = options
      @block = block
    end

    # Find out the matching nodes. It checks the current node and iterates all child nodes,
    # then run the block code with each matching node.
    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

    private

    def find_matching_nodes(current_node)
      matching_nodes = []
      if @options[:recursive]
        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
      elsif current_node.is_a?(Parser::AST::Node)
        if current_node.type == :begin
          current_node.children.each do |child_node|
            matching_nodes << child_node if child_node.match? @rules
          end
        else
          matching_nodes << current_node if current_node.match? @rules
        end
      else
        current_node.each do |child_node|
          matching_nodes << child_node if child_node.match? @rules
        end
      end
      matching_nodes
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
synvert-core-0.21.2 lib/synvert/core/rewriter/scope/within_scope.rb