Class: Prism::ConstantPathAndWriteNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents the use of the ‘&&=` operator for assignment to a constant path.

Parent::Child &&= value
^^^^^^^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, operator_loc, value, location) ⇒ ConstantPathAndWriteNode

def initialize: (target: ConstantPathNode, operator_loc: Location, value: Node, location: Location) -> void



3705
3706
3707
3708
3709
3710
# File 'lib/prism/node.rb', line 3705

def initialize(target, operator_loc, value, location)
  @target = target
  @operator_loc = operator_loc
  @value = value
  @location = location
end

Instance Attribute Details

#operator_locObject (readonly)

attr_reader operator_loc: Location



3699
3700
3701
# File 'lib/prism/node.rb', line 3699

def operator_loc
  @operator_loc
end

#targetObject (readonly)

attr_reader target: ConstantPathNode



3696
3697
3698
# File 'lib/prism/node.rb', line 3696

def target
  @target
end

#valueObject (readonly)

attr_reader value: Node



3702
3703
3704
# File 'lib/prism/node.rb', line 3702

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



3713
3714
3715
# File 'lib/prism/node.rb', line 3713

def accept(visitor)
  visitor.visit_constant_path_and_write_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



3718
3719
3720
# File 'lib/prism/node.rb', line 3718

def child_nodes
  [target, value]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



3728
3729
3730
# File 'lib/prism/node.rb', line 3728

def comment_targets
  [target, operator_loc, value]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3723
3724
3725
# File 'lib/prism/node.rb', line 3723

def compact_child_nodes
  [target, value]
end

#copy(**params) ⇒ Object

def copy: (**params) -> ConstantPathAndWriteNode



3733
3734
3735
3736
3737
3738
3739
3740
# File 'lib/prism/node.rb', line 3733

def copy(**params)
  ConstantPathAndWriteNode.new(
    params.fetch(:target) { target },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:value) { value },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



3746
3747
3748
# File 'lib/prism/node.rb', line 3746

def deconstruct_keys(keys)
  { target: target, operator_loc: operator_loc, value: value, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



3755
3756
3757
3758
3759
3760
3761
3762
3763
# File 'lib/prism/node.rb', line 3755

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── target:\n"
  inspector << inspector.child_node(target, "│   ")
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "└── value:\n"
  inspector << inspector.child_node(value, "    ")
  inspector.to_str
end

#operatorObject

def operator: () -> String



3751
3752
3753
# File 'lib/prism/node.rb', line 3751

def operator
  operator_loc.slice
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



3779
3780
3781
# File 'lib/prism/node.rb', line 3779

def type
  :constant_path_and_write_node
end