Class: Prism::SingletonClassNode

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

Overview

Represents a singleton class declaration involving the ‘class` keyword.

class << self end
^^^^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc) ⇒ SingletonClassNode

Initialize a new SingletonClassNode node.



16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
# File 'lib/prism/node.rb', line 16534

def initialize(source, node_id, location, flags, locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @locals = locals
  @class_keyword_loc = class_keyword_loc
  @operator_loc = operator_loc
  @expression = expression
  @body = body
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: StatementsNode | BeginNode | nil



16616
16617
16618
# File 'lib/prism/node.rb', line 16616

def body
  @body
end

#expressionObject (readonly)

attr_reader expression: Prism::node



16613
16614
16615
# File 'lib/prism/node.rb', line 16613

def expression
  @expression
end

#localsObject (readonly)

attr_reader locals: Array



16584
16585
16586
# File 'lib/prism/node.rb', line 16584

def locals
  @locals
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See ‘Node::type`.



16657
16658
16659
# File 'lib/prism/node.rb', line 16657

def self.type
  :singleton_class_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
# File 'lib/prism/node.rb', line 16663

def ===(other)
  other.is_a?(SingletonClassNode) &&
    (locals.length == other.locals.length) &&
    locals.zip(other.locals).all? { |left, right| left === right } &&
    (class_keyword_loc.nil? == other.class_keyword_loc.nil?) &&
    (operator_loc.nil? == other.operator_loc.nil?) &&
    (expression === other.expression) &&
    (body === other.body) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



16548
16549
16550
# File 'lib/prism/node.rb', line 16548

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



16553
16554
16555
# File 'lib/prism/node.rb', line 16553

def child_nodes
  [expression, body]
end

#class_keywordObject

def class_keyword: () -> String



16632
16633
16634
# File 'lib/prism/node.rb', line 16632

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

attr_reader class_keyword_loc: Location



16587
16588
16589
16590
16591
# File 'lib/prism/node.rb', line 16587

def class_keyword_loc
  location = @class_keyword_loc
  return location if location.is_a?(Location)
  @class_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#comment_targetsObject

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



16566
16567
16568
# File 'lib/prism/node.rb', line 16566

def comment_targets
  [class_keyword_loc, operator_loc, expression, *body, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



16558
16559
16560
16561
16562
16563
# File 'lib/prism/node.rb', line 16558

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << expression
  compact << body if body
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, class_keyword_loc: self.class_keyword_loc, operator_loc: self.operator_loc, expression: self.expression, body: self.body, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?locals: Array, ?class_keyword_loc: Location, ?operator_loc: Location, ?expression: Prism::node, ?body: StatementsNode | BeginNode | nil, ?end_keyword_loc: Location) -> SingletonClassNode



16571
16572
16573
# File 'lib/prism/node.rb', line 16571

def copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, class_keyword_loc: self.class_keyword_loc, operator_loc: self.operator_loc, expression: self.expression, body: self.body, end_keyword_loc: self.end_keyword_loc)
  SingletonClassNode.new(source, node_id, location, flags, locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, locals: Array, class_keyword_loc: Location, operator_loc: Location, expression: Prism::node, body: StatementsNode | BeginNode | nil, end_keyword_loc: Location }



16579
16580
16581
# File 'lib/prism/node.rb', line 16579

def deconstruct_keys(keys)
  { node_id: node_id, location: location, locals: locals, class_keyword_loc: class_keyword_loc, operator_loc: operator_loc, expression: expression, body: body, end_keyword_loc: end_keyword_loc }
end

#end_keywordObject

def end_keyword: () -> String



16642
16643
16644
# File 'lib/prism/node.rb', line 16642

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



16619
16620
16621
16622
16623
# File 'lib/prism/node.rb', line 16619

def end_keyword_loc
  location = @end_keyword_loc
  return location if location.is_a?(Location)
  @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#inspectObject

def inspect -> String



16647
16648
16649
# File 'lib/prism/node.rb', line 16647

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



16637
16638
16639
# File 'lib/prism/node.rb', line 16637

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



16600
16601
16602
16603
16604
# File 'lib/prism/node.rb', line 16600

def operator_loc
  location = @operator_loc
  return location if location.is_a?(Location)
  @operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

#save_class_keyword_loc(repository) ⇒ Object

Save the class_keyword_loc location using the given saved source so that it can be retrieved later.



16595
16596
16597
# File 'lib/prism/node.rb', line 16595

def save_class_keyword_loc(repository)
  repository.enter(node_id, :class_keyword_loc)
end

#save_end_keyword_loc(repository) ⇒ Object

Save the end_keyword_loc location using the given saved source so that it can be retrieved later.



16627
16628
16629
# File 'lib/prism/node.rb', line 16627

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc)
end

#save_operator_loc(repository) ⇒ Object

Save the operator_loc location using the given saved source so that it can be retrieved later.



16608
16609
16610
# File 'lib/prism/node.rb', line 16608

def save_operator_loc(repository)
  repository.enter(node_id, :operator_loc)
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



16652
16653
16654
# File 'lib/prism/node.rb', line 16652

def type
  :singleton_class_node
end