Class: SyntaxTree::ClassDeclaration
- 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
-
#bodystmt ⇒ Object
readonly
- BodyStmt
-
the expressions to execute within the context of the class.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being defined.
-
#superclass ⇒ Object
readonly
- nil | untyped
-
the optional superclass declaration.
Attributes inherited from Node
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
constructor
A new instance of ClassDeclaration.
Methods inherited from Node
Constructor Details
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
Returns a new instance of ClassDeclaration.
2757 2758 2759 2760 2761 2762 2763 |
# File 'lib/syntax_tree/node.rb', line 2757 def initialize(constant:, superclass:, bodystmt:, location:, comments: []) @constant = constant @superclass = superclass @bodystmt = bodystmt @location = location @comments = comments end |
Instance Attribute Details
#bodystmt ⇒ Object (readonly)
- BodyStmt
-
the expressions to execute within the context of the class
2752 2753 2754 |
# File 'lib/syntax_tree/node.rb', line 2752 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2755 2756 2757 |
# File 'lib/syntax_tree/node.rb', line 2755 def comments @comments end |
#constant ⇒ Object (readonly)
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being
defined
2746 2747 2748 |
# File 'lib/syntax_tree/node.rb', line 2746 def constant @constant end |
#superclass ⇒ Object (readonly)
- nil | untyped
-
the optional superclass declaration
2749 2750 2751 |
# File 'lib/syntax_tree/node.rb', line 2749 def superclass @superclass end |
Instance Method Details
#accept(visitor) ⇒ Object
2765 2766 2767 |
# File 'lib/syntax_tree/node.rb', line 2765 def accept(visitor) visitor.visit_class(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
2769 2770 2771 |
# File 'lib/syntax_tree/node.rb', line 2769 def child_nodes [constant, superclass, bodystmt] end |
#deconstruct_keys(keys) ⇒ Object
2775 2776 2777 2778 2779 2780 2781 2782 2783 |
# File 'lib/syntax_tree/node.rb', line 2775 def deconstruct_keys(keys) { constant: constant, superclass: superclass, bodystmt: bodystmt, location: location, comments: comments } end |
#format(q) ⇒ Object
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 |
# File 'lib/syntax_tree/node.rb', line 2785 def format(q) declaration = -> do q.group do q.text("class ") q.format(constant) if superclass q.text(" < ") q.format(superclass) end end end if bodystmt.empty? q.group do declaration.call q.breakable(force: true) q.text("end") end else q.group do declaration.call q.indent do q.breakable(force: true) q.format(bodystmt) end q.breakable(force: true) q.text("end") end end end |