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.



3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
# File 'lib/prism/node.rb', line 3814

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: StatementsNode | BeginNode | nil



3908
3909
3910
# File 'lib/prism/node.rb', line 3908

def body
  @body
end

#constant_pathObject (readonly)

attr_reader constant_path: ConstantReadNode | ConstantPathNode | CallNode



3883
3884
3885
# File 'lib/prism/node.rb', line 3883

def constant_path
  @constant_path
end

#localsObject (readonly)

attr_reader locals: Array



3867
3868
3869
# File 'lib/prism/node.rb', line 3867

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



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

def name
  @name
end

#superclassObject (readonly)

attr_reader superclass: Prism::node?



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

def superclass
  @superclass
end

Class Method Details

.typeObject

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



3952
3953
3954
# File 'lib/prism/node.rb', line 3952

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.



3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
# File 'lib/prism/node.rb', line 3958

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



3830
3831
3832
# File 'lib/prism/node.rb', line 3830

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

#child_nodesObject Also known as: deconstruct

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



3835
3836
3837
# File 'lib/prism/node.rb', line 3835

def child_nodes
  [constant_path, superclass, body]
end

#class_keywordObject

def class_keyword: () -> String



3927
3928
3929
# File 'lib/prism/node.rb', line 3927

def class_keyword
  class_keyword_loc.slice
end

#class_keyword_locObject

attr_reader class_keyword_loc: Location



3870
3871
3872
3873
3874
# File 'lib/prism/node.rb', line 3870

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]



3849
3850
3851
# File 'lib/prism/node.rb', line 3849

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



3840
3841
3842
3843
3844
3845
3846
# File 'lib/prism/node.rb', line 3840

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



3854
3855
3856
# File 'lib/prism/node.rb', line 3854

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 }



3862
3863
3864
# File 'lib/prism/node.rb', line 3862

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



3937
3938
3939
# File 'lib/prism/node.rb', line 3937

def end_keyword
  end_keyword_loc.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location



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

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?



3932
3933
3934
# File 'lib/prism/node.rb', line 3932

def inheritance_operator
  inheritance_operator_loc&.slice
end

#inheritance_operator_locObject

attr_reader inheritance_operator_loc: Location?



3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
# File 'lib/prism/node.rb', line 3886

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



3942
3943
3944
# File 'lib/prism/node.rb', line 3942

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.



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

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.



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

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.



3900
3901
3902
# File 'lib/prism/node.rb', line 3900

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



3947
3948
3949
# File 'lib/prism/node.rb', line 3947

def type
  :class_node
end