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.



16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
# File 'lib/prism/node.rb', line 16326

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



16408
16409
16410
# File 'lib/prism/node.rb', line 16408

def body
  @body
end

#expressionObject (readonly)

attr_reader expression: Prism::node



16405
16406
16407
# File 'lib/prism/node.rb', line 16405

def expression
  @expression
end

#localsObject (readonly)

attr_reader locals: Array



16376
16377
16378
# File 'lib/prism/node.rb', line 16376

def locals
  @locals
end

Class Method Details

.typeObject

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



16449
16450
16451
# File 'lib/prism/node.rb', line 16449

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.



16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
# File 'lib/prism/node.rb', line 16455

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



16340
16341
16342
# File 'lib/prism/node.rb', line 16340

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

#child_nodesObject Also known as: deconstruct

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



16345
16346
16347
# File 'lib/prism/node.rb', line 16345

def child_nodes
  [expression, body]
end

#class_keywordObject

def class_keyword: () -> String



16424
16425
16426
# File 'lib/prism/node.rb', line 16424

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

attr_reader class_keyword_loc: Location



16379
16380
16381
16382
16383
# File 'lib/prism/node.rb', line 16379

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]



16358
16359
16360
# File 'lib/prism/node.rb', line 16358

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



16350
16351
16352
16353
16354
16355
# File 'lib/prism/node.rb', line 16350

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



16363
16364
16365
# File 'lib/prism/node.rb', line 16363

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 }



16371
16372
16373
# File 'lib/prism/node.rb', line 16371

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



16434
16435
16436
# File 'lib/prism/node.rb', line 16434

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



16411
16412
16413
16414
16415
# File 'lib/prism/node.rb', line 16411

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



16439
16440
16441
# File 'lib/prism/node.rb', line 16439

def inspect
  InspectVisitor.compose(self)
end

#operatorObject

def operator: () -> String



16429
16430
16431
# File 'lib/prism/node.rb', line 16429

def operator
  operator_loc.slice
end

#operator_locObject

attr_reader operator_loc: Location



16392
16393
16394
16395
16396
# File 'lib/prism/node.rb', line 16392

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.



16387
16388
16389
# File 'lib/prism/node.rb', line 16387

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.



16419
16420
16421
# File 'lib/prism/node.rb', line 16419

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.



16400
16401
16402
# File 'lib/prism/node.rb', line 16400

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

#typeObject

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



16444
16445
16446
# File 'lib/prism/node.rb', line 16444

def type
  :singleton_class_node
end