Sha256: baa43241f7f0f6ead49d44a7eb860263d2adc7a934a44500da3ccb2afcf381dc

Contents?: true

Size: 1.85 KB

Versions: 22

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
        elsif current_node.match? @rules
          matching_nodes << current_node
        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

22 entries across 22 versions & 1 rubygems

Version Path
synvert-core-0.35.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.34.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.33.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.33.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.32.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.31.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.30.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.29.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.28.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.28.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.27.4 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.27.3 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.27.2 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.27.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.27.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.26.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.26.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.25.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.25.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-0.24.0 lib/synvert/core/rewriter/scope/within_scope.rb