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.



3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
# File 'lib/prism/node.rb', line 3858

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
  ^^^


3965
3966
3967
# File 'lib/prism/node.rb', line 3965

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: ConstantReadNode | ConstantPathNode | CallNode



3930
3931
3932
# File 'lib/prism/node.rb', line 3930

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



3911
3912
3913
# File 'lib/prism/node.rb', line 3911

def locals
  @locals
end

#nameObject (readonly)

The name of the class.

class Foo end # name `:Foo`


3986
3987
3988
# File 'lib/prism/node.rb', line 3986

def name
  @name
end

#superclassObject (readonly)

Represents the superclass of the class.

class Foo < Bar
            ^^^


3958
3959
3960
# File 'lib/prism/node.rb', line 3958

def superclass
  @superclass
end

Class Method Details

.typeObject

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



4014
4015
4016
# File 'lib/prism/node.rb', line 4014

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.



4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
# File 'lib/prism/node.rb', line 4020

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



3874
3875
3876
# File 'lib/prism/node.rb', line 3874

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



3879
3880
3881
# File 'lib/prism/node.rb', line 3879

def child_nodes
  [constant_path, superclass, body]
end

#class_keywordObject

def class_keyword: () -> String



3989
3990
3991
# File 'lib/prism/node.rb', line 3989

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

Represents the location of the ‘class` keyword.

class Foo end
^^^^^


3917
3918
3919
3920
3921
# File 'lib/prism/node.rb', line 3917

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]



3893
3894
3895
# File 'lib/prism/node.rb', line 3893

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



3884
3885
3886
3887
3888
3889
3890
# File 'lib/prism/node.rb', line 3884

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



3898
3899
3900
# File 'lib/prism/node.rb', line 3898

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 }



3906
3907
3908
# File 'lib/prism/node.rb', line 3906

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



3999
4000
4001
# File 'lib/prism/node.rb', line 3999

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

Represents the location of the ‘end` keyword.

class Foo end
          ^^^


3971
3972
3973
3974
3975
# File 'lib/prism/node.rb', line 3971

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?



3994
3995
3996
# File 'lib/prism/node.rb', line 3994

def inheritance_operator
  inheritance_operator_loc&.slice
end

#inheritance_operator_locObject

Represents the location of the ‘<` operator.

class Foo < Bar
          ^


3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
# File 'lib/prism/node.rb', line 3936

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



4004
4005
4006
# File 'lib/prism/node.rb', line 4004

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.



3925
3926
3927
# File 'lib/prism/node.rb', line 3925

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.



3979
3980
3981
# File 'lib/prism/node.rb', line 3979

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.



3950
3951
3952
# File 'lib/prism/node.rb', line 3950

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`.



4009
4010
4011
# File 'lib/prism/node.rb', line 4009

def type
  :class_node
end