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