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.



4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
# 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

Instance Attribute Details

#bodyObject (readonly)

Represents the body of the class.

class Foo
  foo
  ^^^


4184
4185
4186
# File 'lib/prism/node.rb', line 4184

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: ConstantReadNode | ConstantPathNode | CallNode



4149
4150
4151
# File 'lib/prism/node.rb', line 4149

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



4130
4131
4132
# File 'lib/prism/node.rb', line 4130

def locals
  @locals
end

#nameObject (readonly)

The name of the class.

class Foo end # name `:Foo`


4205
4206
4207
# File 'lib/prism/node.rb', line 4205

def name
  @name
end

#superclassObject (readonly)

Represents the superclass of the class.

class Foo < Bar
            ^^^


4177
4178
4179
# File 'lib/prism/node.rb', line 4177

def superclass
  @superclass
end

Class Method Details

.typeObject

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



4233
4234
4235
# File 'lib/prism/node.rb', line 4233

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.



4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
# 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) ⇒ Object

def accept: (Visitor visitor) -> void



4084
4085
4086
# File 'lib/prism/node.rb', line 4084

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



4089
4090
4091
# File 'lib/prism/node.rb', line 4089

def child_nodes
  [constant_path, superclass, body]
end

#class_keywordObject

def class_keyword: () -> String



4208
4209
4210
# File 'lib/prism/node.rb', line 4208

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

Represents the location of the class keyword.

class Foo end
^^^^^


4136
4137
4138
4139
4140
# 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_targetsObject

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



4112
4113
4114
# 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_nodesObject

def compact_child_nodes: () -> Array



4103
4104
4105
4106
4107
4108
4109
# 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) ⇒ Object

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



4117
4118
4119
# 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_keys(keys) ⇒ Object

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 }



4125
4126
4127
# 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| ... } ⇒ Object

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

Yields:



4094
4095
4096
4097
4098
4099
4100
# 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_keywordObject

def end_keyword: () -> String



4218
4219
4220
# File 'lib/prism/node.rb', line 4218

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

Represents the location of the end keyword.

class Foo end
          ^^^


4190
4191
4192
4193
4194
# 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_operatorObject

def inheritance_operator: () -> String?



4213
4214
4215
# File 'lib/prism/node.rb', line 4213

def inheritance_operator
  inheritance_operator_loc&.slice
end

#inheritance_operator_locObject

Represents the location of the ‘<` operator.

class Foo < Bar
          ^


4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
# 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

#inspectObject

def inspect -> String



4223
4224
4225
# File 'lib/prism/node.rb', line 4223

def inspect
  InspectVisitor.compose(self)
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.



4144
4145
4146
# 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) ⇒ Object

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



4198
4199
4200
# 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) ⇒ Object

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



4169
4170
4171
# 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

#typeObject

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



4228
4229
4230
# File 'lib/prism/node.rb', line 4228

def type
  :class_node
end