Class: Audition::Rewriters::WriteOnce::Variables

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/audition/rewriters.rb

Overview

Collects either global or class variable operations. Globals group process-wide; class variables group per namespace so a name reused in two classes never merges. assignable marks writes at a scope where a constant assignment would be legal (no surrounding def or block).

Constant Summary collapse

SKIP_GLOBALS =
(
  Static::Checks::GlobalVariables::SAFE_READS +
  Static::Checks::GlobalVariables::SAFE_WRITES +
  Static::Checks::GlobalVariables::LOAD_PATH_GLOBALS
).freeze
GVAR_NODES =
{
  visit_global_variable_read_node: :read,
  visit_global_variable_write_node: :write,
  visit_global_variable_operator_write_node: :other,
  visit_global_variable_or_write_node: :other,
  visit_global_variable_and_write_node: :other,
  visit_global_variable_target_node: :other
}.freeze
CVAR_NODES =
{
  visit_class_variable_read_node: :read,
  visit_class_variable_write_node: :write,
  visit_class_variable_operator_write_node: :other,
  visit_class_variable_or_write_node: :other,
  visit_class_variable_and_write_node: :other,
  visit_class_variable_target_node: :other
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ Variables

Returns a new instance of Variables.



654
655
656
657
658
659
660
661
662
663
# File 'lib/audition/rewriters.rb', line 654

def initialize(mode)
  @mode = mode
  @groups = Hash.new { |h, k| h[k] = [] }
  @taken_constants = []
  @receiver_reads = {}
  @namespace = []
  @def_depth = 0
  @block_depth = 0
  super()
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



652
653
654
# File 'lib/audition/rewriters.rb', line 652

def groups
  @groups
end

#receiver_readsObject (readonly)

Returns the value of attribute receiver_reads.



652
653
654
# File 'lib/audition/rewriters.rb', line 652

def receiver_reads
  @receiver_reads
end

#taken_constantsObject (readonly)

Returns the value of attribute taken_constants.



652
653
654
# File 'lib/audition/rewriters.rb', line 652

def taken_constants
  @taken_constants
end

Instance Method Details

#visit_block_node(node) ⇒ Object



695
696
697
698
699
700
# File 'lib/audition/rewriters.rb', line 695

def visit_block_node(node)
  @block_depth += 1
  super
ensure
  @block_depth -= 1
end

#visit_call_node(node) ⇒ Object



665
666
667
668
669
670
671
672
# File 'lib/audition/rewriters.rb', line 665

def visit_call_node(node)
  receiver = node.receiver
  if receiver.is_a?(Prism::GlobalVariableReadNode) ||
      receiver.is_a?(Prism::ClassVariableReadNode)
    @receiver_reads[receiver.object_id] = true
  end
  super
end

#visit_class_node(node) ⇒ Object



674
675
676
677
678
679
# File 'lib/audition/rewriters.rb', line 674

def visit_class_node(node)
  @namespace.push(node.constant_path.location.slice)
  super
ensure
  @namespace.pop
end

#visit_constant_read_node(node) ⇒ Object



714
715
716
717
# File 'lib/audition/rewriters.rb', line 714

def visit_constant_read_node(node)
  @taken_constants << node.name.to_s
  super
end

#visit_constant_write_node(node) ⇒ Object



709
710
711
712
# File 'lib/audition/rewriters.rb', line 709

def visit_constant_write_node(node)
  @taken_constants << node.name.to_s
  super
end

#visit_def_node(node) ⇒ Object



688
689
690
691
692
693
# File 'lib/audition/rewriters.rb', line 688

def visit_def_node(node)
  @def_depth += 1
  super
ensure
  @def_depth -= 1
end

#visit_lambda_node(node) ⇒ Object



702
703
704
705
706
707
# File 'lib/audition/rewriters.rb', line 702

def visit_lambda_node(node)
  @block_depth += 1
  super
ensure
  @block_depth -= 1
end

#visit_module_node(node) ⇒ Object



681
682
683
684
685
686
# File 'lib/audition/rewriters.rb', line 681

def visit_module_node(node)
  @namespace.push(node.constant_path.location.slice)
  super
ensure
  @namespace.pop
end