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(locals, class_keyword_loc, operator_loc, expression, body, end_keyword_loc, location) ⇒ SingletonClassNode

def initialize: (locals: Array, class_keyword_loc: Location, operator_loc: Location, expression: Node, body: Node?, end_keyword_loc: Location, location: Location) -> void



15302
15303
15304
15305
15306
15307
15308
15309
15310
# File 'lib/prism/node.rb', line 15302

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

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



15296
15297
15298
# File 'lib/prism/node.rb', line 15296

def body
  @body
end

#class_keyword_locObject (readonly)

attr_reader class_keyword_loc: Location



15287
15288
15289
# File 'lib/prism/node.rb', line 15287

def class_keyword_loc
  @class_keyword_loc
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



15299
15300
15301
# File 'lib/prism/node.rb', line 15299

def end_keyword_loc
  @end_keyword_loc
end

#expressionObject (readonly)

attr_reader expression: Node



15293
15294
15295
# File 'lib/prism/node.rb', line 15293

def expression
  @expression
end

#localsObject (readonly)

attr_reader locals: Array



15284
15285
15286
# File 'lib/prism/node.rb', line 15284

def locals
  @locals
end

#operator_locObject (readonly)

attr_reader operator_loc: Location



15290
15291
15292
# File 'lib/prism/node.rb', line 15290

def operator_loc
  @operator_loc
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



15413
15414
15415
# File 'lib/prism/node.rb', line 15413

def self.type
  :singleton_class_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



15313
15314
15315
# File 'lib/prism/node.rb', line 15313

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

#child_nodesObject Also known as: deconstruct

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



15318
15319
15320
# File 'lib/prism/node.rb', line 15318

def child_nodes
  [expression, body]
end

#class_keywordObject

def class_keyword: () -> String



15357
15358
15359
# File 'lib/prism/node.rb', line 15357

def class_keyword
  class_keyword_loc.slice
end

#comment_targetsObject

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



15331
15332
15333
# File 'lib/prism/node.rb', line 15331

def comment_targets
  [class_keyword_loc, operator_loc, expression, *body, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



15323
15324
15325
15326
15327
15328
# File 'lib/prism/node.rb', line 15323

def compact_child_nodes
  compact = []
  compact << expression
  compact << body if body
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> SingletonClassNode



15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
# File 'lib/prism/node.rb', line 15336

def copy(**params)
  SingletonClassNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:class_keyword_loc) { class_keyword_loc },
    params.fetch(:operator_loc) { operator_loc },
    params.fetch(:expression) { expression },
    params.fetch(:body) { body },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

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



15352
15353
15354
# File 'lib/prism/node.rb', line 15352

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

#end_keywordObject

def end_keyword: () -> String



15367
15368
15369
# File 'lib/prism/node.rb', line 15367

def end_keyword
  end_keyword_loc.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
# File 'lib/prism/node.rb', line 15372

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── locals: #{locals.inspect}\n"
  inspector << "├── class_keyword_loc: #{inspector.location(class_keyword_loc)}\n"
  inspector << "├── operator_loc: #{inspector.location(operator_loc)}\n"
  inspector << "├── expression:\n"
  inspector << inspector.child_node(expression, "")
  if (body = self.body).nil?
    inspector << "├── body: ∅\n"
  else
    inspector << "├── body:\n"
    inspector << body.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "└── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector.to_str
end

#operatorObject

def operator: () -> String



15362
15363
15364
# File 'lib/prism/node.rb', line 15362

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



15403
15404
15405
# File 'lib/prism/node.rb', line 15403

def type
  :singleton_class_node
end