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.



16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
# File 'lib/prism/node.rb', line 16497

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



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

def body
  @body
end

#expressionObject (readonly)

attr_reader expression: Prism::node



16576
16577
16578
# File 'lib/prism/node.rb', line 16576

def expression
  @expression
end

#localsObject (readonly)

attr_reader locals: Array



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

def locals
  @locals
end

Class Method Details

.typeObject

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



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

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.



16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
# File 'lib/prism/node.rb', line 16626

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



16511
16512
16513
# File 'lib/prism/node.rb', line 16511

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



16516
16517
16518
# File 'lib/prism/node.rb', line 16516

def child_nodes
  [expression, body]
end

#class_keywordObject

def class_keyword: () -> String



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

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

attr_reader class_keyword_loc: Location



16550
16551
16552
16553
16554
# File 'lib/prism/node.rb', line 16550

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]



16529
16530
16531
# File 'lib/prism/node.rb', line 16529

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



16521
16522
16523
16524
16525
16526
# File 'lib/prism/node.rb', line 16521

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



16534
16535
16536
# File 'lib/prism/node.rb', line 16534

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 }



16542
16543
16544
# File 'lib/prism/node.rb', line 16542

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



16605
16606
16607
# File 'lib/prism/node.rb', line 16605

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



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

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



16610
16611
16612
# File 'lib/prism/node.rb', line 16610

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



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

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



16563
16564
16565
16566
16567
# File 'lib/prism/node.rb', line 16563

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.



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

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.



16590
16591
16592
# File 'lib/prism/node.rb', line 16590

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.



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

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

#typeObject

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



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

def type
  :singleton_class_node
end