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

Instance Method Summary collapse

Constructor Details

#initialize(locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name, location) ⇒ ClassNode

def initialize: (locals: Array, class_keyword_loc: Location, constant_path: Node, inheritance_operator_loc: Location?, superclass: Node?, body: Node?, end_keyword_loc: Location, name: Symbol, location: Location) -> void



2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
# File 'lib/prism/node.rb', line 2728

def initialize(locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name, 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
  @location = location
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



2719
2720
2721
# File 'lib/prism/node.rb', line 2719

def body
  @body
end

#class_keyword_locObject (readonly)

attr_reader class_keyword_loc: Location



2707
2708
2709
# File 'lib/prism/node.rb', line 2707

def class_keyword_loc
  @class_keyword_loc
end

#constant_pathObject (readonly)

attr_reader constant_path: Node



2710
2711
2712
# File 'lib/prism/node.rb', line 2710

def constant_path
  @constant_path
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



2722
2723
2724
# File 'lib/prism/node.rb', line 2722

def end_keyword_loc
  @end_keyword_loc
end

#inheritance_operator_locObject (readonly)

attr_reader inheritance_operator_loc: Location?



2713
2714
2715
# File 'lib/prism/node.rb', line 2713

def inheritance_operator_loc
  @inheritance_operator_loc
end

#localsObject (readonly)

attr_reader locals: Array



2704
2705
2706
# File 'lib/prism/node.rb', line 2704

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



2725
2726
2727
# File 'lib/prism/node.rb', line 2725

def name
  @name
end

#superclassObject (readonly)

attr_reader superclass: Node?



2716
2717
2718
# File 'lib/prism/node.rb', line 2716

def superclass
  @superclass
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



2741
2742
2743
# File 'lib/prism/node.rb', line 2741

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

#child_nodesObject Also known as: deconstruct

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



2746
2747
2748
# File 'lib/prism/node.rb', line 2746

def child_nodes
  [constant_path, superclass, body]
end

#class_keywordObject

def class_keyword: () -> String



2788
2789
2790
# File 'lib/prism/node.rb', line 2788

def class_keyword
  class_keyword_loc.slice
end

#comment_targetsObject

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



2760
2761
2762
# File 'lib/prism/node.rb', line 2760

def comment_targets
  [class_keyword_loc, constant_path, *inheritance_operator_loc, *superclass, *body, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



2751
2752
2753
2754
2755
2756
2757
# File 'lib/prism/node.rb', line 2751

def compact_child_nodes
  compact = []
  compact << constant_path
  compact << superclass if superclass
  compact << body if body
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> ClassNode



2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
# File 'lib/prism/node.rb', line 2765

def copy(**params)
  ClassNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:class_keyword_loc) { class_keyword_loc },
    params.fetch(:constant_path) { constant_path },
    params.fetch(:inheritance_operator_loc) { inheritance_operator_loc },
    params.fetch(:superclass) { superclass },
    params.fetch(:body) { body },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:name) { name },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



2783
2784
2785
# File 'lib/prism/node.rb', line 2783

def deconstruct_keys(keys)
  { 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, location: location }
end

#end_keywordObject

def end_keyword: () -> String



2798
2799
2800
# File 'lib/prism/node.rb', line 2798

def end_keyword
  end_keyword_loc.slice
end

#inheritance_operatorObject

def inheritance_operator: () -> String?



2793
2794
2795
# File 'lib/prism/node.rb', line 2793

def inheritance_operator
  inheritance_operator_loc&.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object



2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
# File 'lib/prism/node.rb', line 2802

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── locals: #{locals.inspect}\n"
  inspector << "├── class_keyword_loc: #{inspector.location(class_keyword_loc)}\n"
  inspector << "├── constant_path:\n"
  inspector << inspector.child_node(constant_path, "")
  inspector << "├── inheritance_operator_loc: #{inspector.location(inheritance_operator_loc)}\n"
  if (superclass = self.superclass).nil?
    inspector << "├── superclass: ∅\n"
  else
    inspector << "├── superclass:\n"
    inspector << superclass.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (body = self.body).nil?
    inspector << "├── body: ∅\n"
  else
    inspector << "├── body:\n"
    inspector << body.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector << "└── name: #{name.inspect}\n"
  inspector.to_str
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



2840
2841
2842
# File 'lib/prism/node.rb', line 2840

def type
  :class_node
end