Class: SyntaxTree::ClassDeclaration

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Class represents defining a class using the class keyword.

class Container
end

Classes can have path names as their class name in case it’s being nested under a namespace, as in:

class Namespace::Container
end

Classes can also be defined as a top-level path, in the case that it’s already in a namespace but you want to define it at the top-level instead, as in:

module OtherNamespace
  class ::Namespace::Container
  end
end

All of these declarations can also have an optional superclass reference, as in:

class Child < Parent
end

That superclass can actually be any Ruby expression, it doesn’t necessarily need to be a constant, as in:

class Child < method
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration



2946
2947
2948
2949
2950
2951
2952
# File 'lib/syntax_tree/node.rb', line 2946

def initialize(constant:, superclass:, bodystmt:, location:, comments: [])
  @constant = constant
  @superclass = superclass
  @bodystmt = bodystmt
  @location = location
  @comments = comments
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to execute within the context of the class



2941
2942
2943
# File 'lib/syntax_tree/node.rb', line 2941

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2944
2945
2946
# File 'lib/syntax_tree/node.rb', line 2944

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined



2935
2936
2937
# File 'lib/syntax_tree/node.rb', line 2935

def constant
  @constant
end

#superclassObject (readonly)

nil | untyped

the optional superclass declaration



2938
2939
2940
# File 'lib/syntax_tree/node.rb', line 2938

def superclass
  @superclass
end

Instance Method Details

#accept(visitor) ⇒ Object



2954
2955
2956
# File 'lib/syntax_tree/node.rb', line 2954

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

#child_nodesObject Also known as: deconstruct



2958
2959
2960
# File 'lib/syntax_tree/node.rb', line 2958

def child_nodes
  [constant, superclass, bodystmt]
end

#deconstruct_keys(_keys) ⇒ Object



2964
2965
2966
2967
2968
2969
2970
2971
2972
# File 'lib/syntax_tree/node.rb', line 2964

def deconstruct_keys(_keys)
  {
    constant: constant,
    superclass: superclass,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
# File 'lib/syntax_tree/node.rb', line 2974

def format(q)
  if bodystmt.empty?
    q.group do
      format_declaration(q)
      q.breakable_force
      q.text("end")
    end
  else
    q.group do
      format_declaration(q)

      q.indent do
        q.breakable_force
        q.format(bodystmt)
      end

      q.breakable_force
      q.text("end")
    end
  end
end