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.



17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
# File 'lib/prism/node.rb', line 17526

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



17616
17617
17618
# File 'lib/prism/node.rb', line 17616

def body
  @body
end

#expressionObject (readonly)

attr_reader expression: Prism::node



17613
17614
17615
# File 'lib/prism/node.rb', line 17613

def expression
  @expression
end

#localsObject (readonly)

attr_reader locals: Array



17584
17585
17586
# File 'lib/prism/node.rb', line 17584

def locals
  @locals
end

Class Method Details

.typeObject

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



17657
17658
17659
# File 'lib/prism/node.rb', line 17657

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.



17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
# File 'lib/prism/node.rb', line 17663

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



17540
17541
17542
# File 'lib/prism/node.rb', line 17540

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



17545
17546
17547
# File 'lib/prism/node.rb', line 17545

def child_nodes
  [expression, body]
end

#class_keywordObject

def class_keyword: () -> String



17632
17633
17634
# File 'lib/prism/node.rb', line 17632

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

attr_reader class_keyword_loc: Location



17587
17588
17589
17590
17591
# File 'lib/prism/node.rb', line 17587

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]



17566
17567
17568
# File 'lib/prism/node.rb', line 17566

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



17558
17559
17560
17561
17562
17563
# File 'lib/prism/node.rb', line 17558

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



17571
17572
17573
# File 'lib/prism/node.rb', line 17571

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 }



17579
17580
17581
# File 'lib/prism/node.rb', line 17579

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

#each_child_node {|expression| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



17550
17551
17552
17553
17554
17555
# File 'lib/prism/node.rb', line 17550

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield expression
  yield body if body
end

#end_keywordObject

def end_keyword: () -> String



17642
17643
17644
# File 'lib/prism/node.rb', line 17642

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



17619
17620
17621
17622
17623
# File 'lib/prism/node.rb', line 17619

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



17647
17648
17649
# File 'lib/prism/node.rb', line 17647

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



17637
17638
17639
# File 'lib/prism/node.rb', line 17637

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



17600
17601
17602
17603
17604
# File 'lib/prism/node.rb', line 17600

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.



17595
17596
17597
# File 'lib/prism/node.rb', line 17595

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.



17627
17628
17629
# File 'lib/prism/node.rb', line 17627

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.



17608
17609
17610
# File 'lib/prism/node.rb', line 17608

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

#typeObject

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



17652
17653
17654
# File 'lib/prism/node.rb', line 17652

def type
  :singleton_class_node
end