1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
# frozen_string_literal: true
module Unparser
module AST
# Calculated local variable scope for a given node
class LocalVariableScope
include Enumerable, Adamantium, Concord.new(:node)
# Initialize object
#
# @param [Parser::AST::Node] node
#
# @return [undefined]
#
# @api private
#
def initialize(node)
items = []
LocalVariableScopeEnumerator.each(node) do |*scope|
items << scope
end
@items = items
super(node)
end
# Test if local variable was first at given assignment
#
# @param [Parser::AST::Node] node
#
# @return [Boolean]
#
# @api private
#
def first_assignment?(node)
name = node.children.first
match(node) do |current, before|
current.include?(name) && !before.include?(name)
end
end
# Test if local variable is defined for given node
#
# @param [Parser::AST::Node] node
# @param [Symbol] name
#
# @return [Boolean]
#
# @api private
#
def local_variable_defined_for_node?(node, name)
match(node) do |current|
current.include?(name)
end
end
# Test if local variables where first assigned in body and read by conditional
#
# @param [Parser::AST::Node] body
# @param [Parser::AST::Node] condition
#
# @api private
#
def first_assignment_in?(left, right)
condition_reads = AST.local_variable_reads(right)
candidates = AST.local_variable_assignments(left).select do |node|
condition_reads.include?(node.children.first)
end
candidates.any?(&public_method(:first_assignment?))
end
private
def match(needle)
@items.each do |node, current, before|
return yield(current, before) if node.equal?(needle)
end
end
end # LocalVariableScope
# Local variable scope enumerator
class LocalVariableScopeEnumerator
include Enumerable
# Initialize object
#
# @return [undefined]
#
# @api private
#
def initialize
@stack = [Set.new]
end
# Enumerate each node with its local variable scope
#
# @param [Parser::AST::Node] node
#
# @return [self]
#
# @api private
#
def self.each(node, &block)
new.each(node, &block)
self
end
# Enumerate local variable scope scope
#
# @return [self]
# if block given
#
# @return [Enumerator<Array<Symbol>>>]
# otherwise
#
# @api private
#
def each(node, &block)
visit(node, &block)
end
private
def current
@stack.last
end
def visit(node, &block)
before = current.dup
enter(node)
yield node, current.dup, before
node.children.each do |child|
visit(child, &block) if child.is_a?(Parser::AST::Node)
end
leave(node)
end
def enter(node)
case node.type
when *RESET_NODES
push_reset
when ASSIGN_NODES
define(node.children.first)
when *INHERIT_NODES
push_inherit
end
end
def leave(node)
pop if CLOSE_NODES.include?(node.type)
end
def define(name)
current << name
end
def push_reset
@stack << Set.new
end
def push_inherit
@stack << current.dup
end
def pop
@stack.pop
end
end # LocalVariableScopeEnumerator
end # AST
end # Unparser
|