Class: Prism::ClassNode

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

Overview

Represents a class declaration involving the ‘class` keyword.

class Foo 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, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name) ⇒ ClassNode

Initialize a new ClassNode node.



3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
# File 'lib/prism/node.rb', line 3202

def initialize(source, node_id, location, flags, locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @locals = locals
  @class_keyword_loc = class_keyword_loc
  @constant_path = constant_path
  @inheritance_operator_loc = inheritance_operator_loc
  @superclass = superclass
  @body = body
  @end_keyword_loc = end_keyword_loc
  @name = name
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Prism::node?



3284
3285
3286
# File 'lib/prism/node.rb', line 3284

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: Prism::node



3265
3266
3267
# File 'lib/prism/node.rb', line 3265

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



3255
3256
3257
# File 'lib/prism/node.rb', line 3255

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



3294
3295
3296
# File 'lib/prism/node.rb', line 3294

def name
  @name
end

#superclassObject (readonly)

attr_reader superclass: Prism::node?



3281
3282
3283
# File 'lib/prism/node.rb', line 3281

def superclass
  @superclass
end

Class Method Details

.typeObject

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



3322
3323
3324
# File 'lib/prism/node.rb', line 3322

def self.type
  :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.



3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
# File 'lib/prism/node.rb', line 3328

def ===(other)
  other.is_a?(ClassNode) &&
    (locals.length == other.locals.length) &&
    locals.zip(other.locals).all? { |left, right| left === right } &&
    (class_keyword_loc.nil? == other.class_keyword_loc.nil?) &&
    (constant_path === other.constant_path) &&
    (inheritance_operator_loc.nil? == other.inheritance_operator_loc.nil?) &&
    (superclass === other.superclass) &&
    (body === other.body) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?) &&
    (name === other.name)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



3218
3219
3220
# File 'lib/prism/node.rb', line 3218

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

#child_nodesObject Also known as: deconstruct

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



3223
3224
3225
# File 'lib/prism/node.rb', line 3223

def child_nodes
  [constant_path, superclass, body]
end

#class_keywordObject

def class_keyword: () -> String



3297
3298
3299
# File 'lib/prism/node.rb', line 3297

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

attr_reader class_keyword_loc: Location



3258
3259
3260
3261
3262
# File 'lib/prism/node.rb', line 3258

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]



3237
3238
3239
# File 'lib/prism/node.rb', line 3237

def comment_targets
  [class_keyword_loc, constant_path, *inheritance_operator_loc, *superclass, *body, end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3228
3229
3230
3231
3232
3233
3234
# File 'lib/prism/node.rb', line 3228

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << constant_path
  compact << superclass if superclass
  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, constant_path: self.constant_path, inheritance_operator_loc: self.inheritance_operator_loc, superclass: self.superclass, body: self.body, end_keyword_loc: self.end_keyword_loc, name: self.name) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?locals: Array, ?class_keyword_loc: Location, ?constant_path: Prism::node, ?inheritance_operator_loc: Location?, ?superclass: Prism::node?, ?body: Prism::node?, ?end_keyword_loc: Location, ?name: Symbol) -> ClassNode



3242
3243
3244
# File 'lib/prism/node.rb', line 3242

def copy(node_id: self.node_id, location: self.location, flags: self.flags, locals: self.locals, class_keyword_loc: self.class_keyword_loc, constant_path: self.constant_path, inheritance_operator_loc: self.inheritance_operator_loc, superclass: self.superclass, body: self.body, end_keyword_loc: self.end_keyword_loc, name: self.name)
  ClassNode.new(source, node_id, location, flags, locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, locals: Array, class_keyword_loc: Location, constant_path: Prism::node, inheritance_operator_loc: Location?, superclass: Prism::node?, body: Prism::node?, end_keyword_loc: Location, name: Symbol }



3250
3251
3252
# File 'lib/prism/node.rb', line 3250

def deconstruct_keys(keys)
  { node_id: node_id, location: location, locals: locals, class_keyword_loc: class_keyword_loc, constant_path: constant_path, inheritance_operator_loc: inheritance_operator_loc, superclass: superclass, body: body, end_keyword_loc: end_keyword_loc, name: name }
end

#end_keywordObject

def end_keyword: () -> String



3307
3308
3309
# File 'lib/prism/node.rb', line 3307

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



3287
3288
3289
3290
3291
# File 'lib/prism/node.rb', line 3287

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

#inheritance_operatorObject

def inheritance_operator: () -> String?



3302
3303
3304
# File 'lib/prism/node.rb', line 3302

def inheritance_operator
  inheritance_operator_loc&.slice
end

#inheritance_operator_locObject

attr_reader inheritance_operator_loc: Location?



3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
# File 'lib/prism/node.rb', line 3268

def inheritance_operator_loc
  location = @inheritance_operator_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @inheritance_operator_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#inspectObject

def inspect -> String



3312
3313
3314
# File 'lib/prism/node.rb', line 3312

def inspect
  InspectVisitor.compose(self)
end

#typeObject

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



3317
3318
3319
# File 'lib/prism/node.rb', line 3317

def type
  :class_node
end