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.



338
339
340
341
342
343
344
345
346
347
# File 'lib/audition/rewriters.rb', line 338

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.



336
337
338
# File 'lib/audition/rewriters.rb', line 336

def groups
  @groups
end

#receiver_readsObject (readonly)

Returns the value of attribute receiver_reads.



336
337
338
# File 'lib/audition/rewriters.rb', line 336

def receiver_reads
  @receiver_reads
end

#taken_constantsObject (readonly)

Returns the value of attribute taken_constants.



336
337
338
# File 'lib/audition/rewriters.rb', line 336

def taken_constants
  @taken_constants
end

Instance Method Details

#visit_block_node(node) ⇒ Object



379
380
381
382
383
384
# File 'lib/audition/rewriters.rb', line 379

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

#visit_call_node(node) ⇒ Object



349
350
351
352
353
354
355
356
# File 'lib/audition/rewriters.rb', line 349

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



358
359
360
361
362
363
# File 'lib/audition/rewriters.rb', line 358

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

#visit_constant_read_node(node) ⇒ Object



398
399
400
401
# File 'lib/audition/rewriters.rb', line 398

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

#visit_constant_write_node(node) ⇒ Object



393
394
395
396
# File 'lib/audition/rewriters.rb', line 393

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

#visit_def_node(node) ⇒ Object



372
373
374
375
376
377
# File 'lib/audition/rewriters.rb', line 372

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

#visit_lambda_node(node) ⇒ Object



386
387
388
389
390
391
# File 'lib/audition/rewriters.rb', line 386

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

#visit_module_node(node) ⇒ Object



365
366
367
368
369
370
# File 'lib/audition/rewriters.rb', line 365

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