class Prism::ClassNode

Represents a class declaration involving the class keyword.

class Foo end
^^^^^^^^^^^^^

Attributes

body[R]

Represents the body of the class.

class Foo
  foo
  ^^^
constant_path[R]
locals[R]

attr_reader locals: Array

name[R]

The name of the class.

class Foo end # name `:Foo`
superclass[R]

Represents the superclass of the class.

class Foo < Bar
            ^^^

Public Class Methods

new(source, node_id, location, flags, locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name) click to toggle source

Initialize a new ClassNode node.

# File lib/prism/node.rb, line 4068
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
type() click to toggle source

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

# File lib/prism/node.rb, line 4233
def self.type
  :class_node
end

Public Instance Methods

===(other) click to toggle source

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.

# File lib/prism/node.rb, line 4239
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) click to toggle source

def accept: (Visitor visitor) -> void

# File lib/prism/node.rb, line 4084
def accept(visitor)
  visitor.visit_class_node(self)
end
child_nodes() click to toggle source

def child_nodes: () -> Array

# File lib/prism/node.rb, line 4089
def child_nodes
  [constant_path, superclass, body]
end
Also aliased as: deconstruct
class_keyword() click to toggle source

def class_keyword: () -> String

# File lib/prism/node.rb, line 4208
def class_keyword
  class_keyword_loc.slice
end
class_keyword_loc() click to toggle source

Represents the location of the class keyword.

class Foo end
^^^^^
# File lib/prism/node.rb, line 4136
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_targets() click to toggle source

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

# File lib/prism/node.rb, line 4112
def comment_targets
  [class_keyword_loc, constant_path, *inheritance_operator_loc, *superclass, *body, end_keyword_loc] #: Array[Prism::node | Location]
end
compact_child_nodes() click to toggle source

def compact_child_nodes: () -> Array

# File lib/prism/node.rb, line 4103
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) click to toggle source

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?locals: Array, ?class_keyword_loc: Location, ?constant_path: ConstantReadNode | ConstantPathNode | CallNode, ?inheritance_operator_loc: Location?, ?superclass: Prism::node?, ?body: StatementsNode | BeginNode | nil, ?end_keyword_loc: Location, ?name: Symbol) -> ClassNode

# File lib/prism/node.rb, line 4117
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()

def deconstruct: () -> Array

Alias for: child_nodes
deconstruct_keys(keys) click to toggle source

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

# File lib/prism/node.rb, line 4125
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
each_child_node() { |constant_path| ... } click to toggle source

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

# File lib/prism/node.rb, line 4094
def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield constant_path
  yield superclass if superclass
  yield body if body
end
end_keyword() click to toggle source

def end_keyword: () -> String

# File lib/prism/node.rb, line 4218
def end_keyword
  end_keyword_loc.slice
end
end_keyword_loc() click to toggle source

Represents the location of the end keyword.

class Foo end
          ^^^
# File lib/prism/node.rb, line 4190
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_operator() click to toggle source

def inheritance_operator: () -> String?

# File lib/prism/node.rb, line 4213
def inheritance_operator
  inheritance_operator_loc&.slice
end
inheritance_operator_loc() click to toggle source

Represents the location of the < operator.

class Foo < Bar
          ^
# File lib/prism/node.rb, line 4155
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
inspect() click to toggle source

def inspect -> String

# File lib/prism/node.rb, line 4223
def inspect
  InspectVisitor.compose(self)
end
save_class_keyword_loc(repository) click to toggle source

Save the class_keyword_loc location using the given saved source so that it can be retrieved later.

# File lib/prism/node.rb, line 4144
def save_class_keyword_loc(repository)
  repository.enter(node_id, :class_keyword_loc)
end
save_end_keyword_loc(repository) click to toggle source

Save the end_keyword_loc location using the given saved source so that it can be retrieved later.

# File lib/prism/node.rb, line 4198
def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc)
end
save_inheritance_operator_loc(repository) click to toggle source

Save the inheritance_operator_loc location using the given saved source so that it can be retrieved later.

# File lib/prism/node.rb, line 4169
def save_inheritance_operator_loc(repository)
  repository.enter(node_id, :inheritance_operator_loc) unless @inheritance_operator_loc.nil?
end
type() click to toggle source

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

# File lib/prism/node.rb, line 4228
def type
  :class_node
end