Sha256: 7987db5bf83c4c4205b32efbd321cb906ec78e81f91da0abfd626a4e1d79ad8d

Contents?: true

Size: 1.29 KB

Versions: 44

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # WithinScope finds out nodes which match nql or rules, then changes its scope to matching node.
  class Rewriter::WithinScope < Rewriter::Scope
    # Initialize a WithinScope.
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param nql_or_rules [String|Hash]
    # @param options [Hash]
    # @yield run on all matching nodes
    # @raise [Synvert::Core::NodeQuery::Compiler::ParseError] if the query string is invalid.
    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)
    end

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

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
synvert-core-1.29.4 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.29.3 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.29.2 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.29.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.29.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.28.4 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.28.3 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.28.2 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.28.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.28.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.27.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.27.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.26.3 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.26.2 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.26.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.26.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.25.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.24.0 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.23.1 lib/synvert/core/rewriter/scope/within_scope.rb
synvert-core-1.23.0 lib/synvert/core/rewriter/scope/within_scope.rb