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
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
Returns a new instance of ClassDeclaration.
2835 2836 2837 2838 2839 2840 2841 |
# File 'lib/syntax_tree/node.rb', line 2835 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
2830 2831 2832 |
# File 'lib/syntax_tree/node.rb', line 2830 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2833 2834 2835 |
# File 'lib/syntax_tree/node.rb', line 2833 def comments @comments end |
#constant ⇒ Object (readonly)
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being
defined
2824 2825 2826 |
# File 'lib/syntax_tree/node.rb', line 2824 def constant @constant end |
#superclass ⇒ Object (readonly)
- nil | untyped
-
the optional superclass declaration
2827 2828 2829 |
# File 'lib/syntax_tree/node.rb', line 2827 def superclass @superclass end |
Instance Method Details
#accept(visitor) ⇒ Object
2843 2844 2845 |
# File 'lib/syntax_tree/node.rb', line 2843 def accept(visitor) visitor.visit_class(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
2847 2848 2849 |
# File 'lib/syntax_tree/node.rb', line 2847 def child_nodes [constant, superclass, bodystmt] end |
#deconstruct_keys(_keys) ⇒ Object
2853 2854 2855 2856 2857 2858 2859 2860 2861 |
# File 'lib/syntax_tree/node.rb', line 2853 def deconstruct_keys(_keys) { constant: constant, superclass: superclass, bodystmt: bodystmt, location: location, comments: comments } end |
#format(q) ⇒ Object
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 |
# File 'lib/syntax_tree/node.rb', line 2863 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 |